list
One data type built into Python is a list: list. A list is an ordered collection from which elements can be added and removed at any time.
Code:
print('------------------------------------------------------') #list classmates = ['Michael','Bob','Tracy','Jason'] print(classmates) print('classmates =',len(classmates)) #Get the length of the array print(classmates[0]) #The first element print(classmates[1]) #The second element print(classmates[2]) #The third element print('------------------------------------------------------') print(classmates[-1]) #The last element print(classmates[-2]) #The penultimate element print(classmates[-3]) #The third last element print('------------------------------------------------------') a = len(classmates)-1 # index of last element print(classmates[a]) # Equivalent to print(classmates[-1]) print('------------------------------------------------------') print(classmates) print(classmates[-1]) classmates.append('adm') #Insert an element at the end print(classmates) print(classmates[-1]) print('------------------------------------------------------') print(classmates) print(classmates[1]) classmates.insert(1,'Plada') #Insert an element at the specified position print(classmates) print(classmates[1]) print('------------------------------------------------------') print(classmates.pop()) #delete the element at the end of the list print(classmates) print('------------------------------------------------------') print(classmates) print('classmates.pop(-1) :',classmates.pop(-1)) #Delete the element at the specified position print(classmates) print('classmates.pop(1) :',classmates.pop(1)) print(classmates) print('------------------------------------------------------') print(classmates) a = classmates[1] = 'Jason' print('classmates[1] =',a) print(classmates) print('------------------------------------------------------') L = ['abc',123,True] print (len (L)) print(L[0]) print(L[1]) print(L[2]) print('------------------------------------------------------') s = ['python','java',['asp','php'],'c++'] p = ['asp','php'] s = ['python','java',p,'c++'] #two-dimensional array print('p =',p) print('s =',s) print('p[1] =',p[1]) print('s[2][1] =',s[2][1]) print('------------------------------------------------------') a = ['abcd','efg','hijk'] b = [a,'lmn','opq'] c = [b,'rst','uvw','xyz'] #Three-digit array print('c =',c) print('a[0] =',a[0]) print('b[0][0] =',b[0][0]) print('c[0][0][0] =',c[0][0][0]) print('------------------------------------------------------') L = [] print ('L =', len (L))
tuple
Another type of ordered list is called a tuple: tuple. Tuple and list are very similar, but once tuple is initialized, it cannot be modified. For example, it also lists the names of classmates:
Code:
print('------------------------------------------------------') #tuple: Once initialized it cannot be modified classmates = ('Michael','Bob','Tracy','Jason') print('classmates =',classmates) t = (1,2) print('t =',t) r = ('a',) print('r =',r) print('------------------------------------------------------') x = ('a','b',['A','B']) print('x =',x) x[2][0] = 'X' x[2][1] = 'Y' print('x =',x) print('------------------------------------------------------') x = ('a','b',('A','B')) print('x =',x) x[2][0] = 'X' #tuple array cannot be modified, an error will be reported x[2][1] = 'Y' print('x =',x)
TestCode:
# -*- coding: utf-8 -*- L = [ ['Apple','Google','Microsoft'], ['Java','Python','Ruby','PHP'], ['Adam','Bart','Lisa'] ] #print Apple: print(L[0][0]) #Print Python: print(L[1][1]) #Print Lisa: print(L[2][2])