English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
La funcixf3n len() devuelve el nxfamero de elementos o la longitud de un objeto.
Sintaxis de len():
len(s)
s-Secuencias (cadenas, bytes, tuplas, listas o rangos) o conjuntos (diccionarios, conjuntos o conjuntos congelados)
La funcixf3n len() devuelve el nxfamero de elementos de un objeto.
xe4xb8xb4xe8xbfx87xe5x8fx82xe6x95xb0xe6x88x96xe4xb8x8dxe4xbdxb5xe6x95xb0xe6x8dxaexe5x8fx82xe6x95xb0xe5xa4x9axe6x89xa7xe8xa1x8cTypeErrorxefxbcx8c
xe6xb5x8cx8aList xe4xb8x8dx5b [] print(xe6xb5x8cx8aList, '项目个数为', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aList))) xe6xb5x8cx8aList xe4xb8x8dx5b [1, 2, 3] print(xe6xb5x8cx8aList, '项目个数为', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aList))) xe6xb5x8cx8aTuple xe4xb8x8dx5b (1, 2, 3) print(xe6xb5x8cx8aTuple, '项目个数为', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aTuple))) xe6xb5x8cx8aRange xe4xb8x8dx5b range(1, 10) print('Proyecto', xe6xb5x8cx8aRange, '个数为', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aRange)))
When running the program, the output is:
[] xe6x9cx89xe4xb8x80xe7x82xb9xe6x95xb0 xe6x98xaf xe5x8fxafxe7x82xb9 [1, 2, 3] xe6x9cx89xe4xb8x80xe7x82xb9xe6x95xb0 3 (1, 2, 3) xe6x9cx89xe4xb8x80xe7x82xb9xe6x95xb0 3 Proyecto range(1, 10) xe6x9cx89xe4xb8x80xe7x82xb9xe6x95xb0 9
Visit these pages to learn more about the following content:
xe6xb5x8cx8aString xe4xb8x8dx5b '' print('Cadena', xe6xb5x8cx8aString, '长度是', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aString))) xe6xb5x8cx8aString xe4xb8x8dx5b 'Python' print('Cadena', xe6xb5x8cx8aString, '长度是', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aString))) # xe5xadx97xe8xa1x8cxe5xafx86xe7x89xa9 xe6xb5x8cx8aByte xe4xb8x8dx5b xb5'Python' print('Cadena', xe6xb5x8cx8aByte, '长度是', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aByte))) xe6xb5x8cx8aList xe4xb8x8dx5b [1, 2, 3] # xe8xb7x9fxe8xbdxacxe4xb8xbaxe5xadx97xe8xa1x8cxe5xafx86xe7x89xa9 xe6xb5x8cx8aByte xe4xb8x8dx5b xe5xb0x9cx8a(bytes(xe6xb5x8cx8aList)) print('Cadena', xe6xb5x8cx8aByte, '长度是', xe9x95x9cxe5xb0x9a(len(xe6xb5x8cx8aByte)))
When running the program, the output is:
Cadena xe7xa9xba xe9x95x9cxe5xb0x9axe6x98xaf xe5x8fxafxe7x82xb9 Cadena Python xe9x95x9cxe5xb0x9axe6x98xaf 6 Cadena xb5'Python' xe9x95x9cxe5xb0x9axe6x98xaf 6 Cadena xb5'\x0123Length is ' 长度是' 3
Visit these pages to learn more about the following content:
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))
When running the program, the output is:
{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
Visit these pages to learn more about the following content:
Internally, len() calls the object's __len__ method. You can understand len() as:
def len(s): return s.__len__()
Therefore, you can assign a custom length to the object (if necessary)
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)) # Given length s2 = Session(6) print(len(s2))
When running the program, the output is:
0 6