list and tuples (tuple) Basic Operation

classmates = ['Michael', 'Bob', 'Tracy']
print(classmates)
print(len(classmates))
print(classmates[0])#Michael
print(classmates[-1])#Tracy
print(classmates[-3]) #Michael
#追加到末尾
classmates.append("Adam")
print(classmates)#['Michael', 'Bob', 'Tracy', 'Adam']
classmates.insert(1, 'Jack')
print(classmates)#['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
classmates.pop()
print(classmates)#['Michael', 'Jack', 'Bob', 'Tracy']
classmates.pop(1)
print(classmates)#['Michael', 'Bob', 'Tracy']
classmates[1] = 'Sarah'
print(classmates)#['Michael', 'Sarah', 'Tracy']
L = ['Apple', 123, True]
s = ['python', 'java', ['ASP ' , ' PHP ' ], ' scheme ' ] # two-digit groups 
Print (len (S)) # . 4 
Print (S [2] [0]) # ASP

 

# Another called an ordered list of tuples: tuple. tuple and the list is very similar, but the tuple Once the initialization can not modify 
Classmates = ( ' Michael ' , ' Bob ' , ' Tracy ' )
 # Classmates [1] = '12 is' 
# empty tuple 
T = ()
 # to define an only 1 tuple elements will be considered parentheses in a mathematical sense 
t = (1 )
 Print (t) # is a number 
t = 10 Print (t) # 10 
# properly defined method 
t = (1 ,) # tuple nesting list element can be modified 
T = ( ' A ' , ' B


' , [ ' A ' , ' B ' ]) 
T [ 2] [0] = ' X- ' 
T [ 2] [. 1] = ' the Y ' 
Print (T) # (' A ',' B ', [' the X-',' the Y-']) 
# exercise 
L = [ 
    [ ' the Apple ' , ' Google ' , ' in the Microsoft ' ], 
    [ ' the Java ' , ' Python ' , 'Ruby', ' The PHP ' ], 
    [ ' Adam ' , ' Bart ' , ' Lisa ' ] 
] 
# print the Apple: 
Print (L [0] [0])
 # Print the Python: 
Print (L [. 1] [. 1 ])
 # printing Lisa : 
Print (L [2] [2])

 

Guess you like

Origin www.cnblogs.com/-alvin/p/10953963.html