The Len () function returns the length of a string, a list, a dictionary, a tuple, and so on
EG1: Calculates the length of the string:
>>>s= ' Hello good boy Doiido '
>>>len (s)
>>>21
EG2: Count the number of elements in the list:
name=[' Zhu ', ' Han ', ' shi '] >>>l=[' h ', ' e ', ' l ', ' l ', ' O ']
AGE=[21,22,23] >>>len (l)
For I in range (len (name)): 5
Print Name[i], ' is ', age[i], ' years '
Output:
Zhu is yaer old
Han is-yaer old
Shi is at Yaer old
EG3: Calculates the total length of the Dictionary: (calculates the total number of key-value pairs)
d={' x ': 1, ' Y ': 2, ' Z ': 3}
Len (d)
3
EG4: Calculating the number of tuple elements
t= (' g ', ' o ', ' o ', ' d ')
Len (t)
4
Range () function:
Eg1:range (1,5) #从1到5 (not including 5)
[1,2,3,4,5]
Eg2:range (1,5,2) #从1到5, Interval 2 (not including 5)
[1,3]
Eg3:range (5) #从0到5 (not including 5)
[0,1,2,3,4]
Operation of List:
array=[1,2,5,3,6,8,4]
#array的顺序标志是:
(0,1,2,3,4,5,6)
( -7,-6,-5,-4,-,-2,-1)
Then: >>>array[0:] #列出0以后得
[1,2,5,3,6,8,4]
>>>array[1:] #列出1以后得
[2,5,3,6,8,4]
>>>ARRAY[:-1] #列出-1 ago
[1,2,5,3,6,8]
>>>array[3:-3] #列出3到-3
[3]
Len (), range () function