1. <source id="nejs2"></source>
  2. <video id="nejs2"></video>
    <source id="nejs2"></source>
          1. Python len()

            更新:

            內置函數len()用于返回對象的長度,即對象中的多個元素。這里的對象可以是字符串、數組、列表、元組、字典等。

             **len(object)** # object can be string,sets,tuples,dictionary etc
            

            len()參數:

            只接受一個參數。在這種情況下,序列可以是字符串、字節、元組、列表或范圍,集合可以是字典、集合或凍結集合。

            參數 描述 必需/可選
            目標 序列或集合 需要

            len()返回值

            如果缺少參數或傳遞了無效的參數,將引發類型錯誤異常。

            | 投入 | 返回值 | | 一個對象 | 對象的長度(整數值) |

            Python 中len()方法的示例

            示例len()如何處理元組、列表和范圍?

             testList = []
            print(testList, 'length is', len(testList))
            
            testList = [1, 2, 3]
            print(testList, 'length is', len(testList))
            
            testTuple = (1, 2, 3)
            print(testTuple, 'length is', len(testTuple))
            
            testRange = range(1, 10)
            print('Length of', testRange, 'is', len(testRange)) 
            

            輸出:

            [] length is 0
            [1, 2, 3] length is 3
            (1, 2, 3) length is 3
            Length of range(1, 10) is 9 

            示例len()如何處理字符串和字節?

             testString = ''
            print('Length of', testString, 'is', len(testString))
            
            testString = 'Python'
            print('Length of', testString, 'is', len(testString))
            
            # byte object
            testByte = b'Python'
            print('Length of', testByte, 'is', len(testByte))
            
            testList = [1, 2, 3]
            
            # converting to bytes object
            testByte = bytes(testList)
            print('Length of', testByte, 'is', len(testByte)) 
            

            輸出:

            Length of  is 0
            Length of Python is 6
            Length of b'Python' is 6
            Length of b'\x01\x02\x03' is 3 

            示例len()如何處理字典和集合?

             testSet = {1, 2, 3}
            print(testSet, 'length is', len(testSet))
            
            # Empty Set
            testSet = set()
            print(testSet, 'length is', len(testSet))
            
            testDict = {1: 'one', 2: 'two'}
            print(testDict, 'length is', len(testDict))
            
            testDict = {}
            print(testDict, 'length is', len(testDict))
            
            testSet = {1, 2}
            # frozenSet
            frozenTestSet = frozenset(testSet)
            print(frozenTestSet, 'length is', len(frozenTestSet)) 
            

            輸出:

            {1, 2, 3} length is 3
            set() length is 0
            {1: 'one', 2: 'two'} length is 2
            {} length is 0
            frozenset({1, 2}) length is 2 

            示例len()如何用于自定義對象?

             class Session:
                def __init__(self, number = 0):
                  self.number = number
            
                def __len__(self):
                  return self.number
            
            # default length is 0
            s1 = Session()
            print(len(s1))
            
            # giving custom length
            s2 = Session(6)
            print(len(s2)) 
            

            輸出:

            0
            6 
            頂部 久久久久99精品成人片毛片_黃色A片三級三級三級无码_日本不卡高清视频v中文字幕_高清欧美视频一区二区
            1. <source id="nejs2"></source>
            2. <video id="nejs2"></video>
              <source id="nejs2"></source>