# Define a list of students that can store more than one student '' ' Student = [' Zhao money, '' Lee Sun ',' Zhou Wu ',' Zheng Wang '] Print (Student [1]) # Sun Li student_info = [ 'Bo', 72, 'male', [ ' bubble 8', '9 drink']] # Bo all students interested Print (student_info [. 3]) # Bo is interested student second print (student_info [3] [ 1]) # 4, and the members in operation in Not Print ( 'Bo' in student_info) #True Print ( 'Bo' Not in student_info) #False #. 5, additionally student_info = [ 'Bo', 72, 'male', [ 'bubble 8', '9 drink']] student_info.append ( 'Anhui Academy cattle, HEFEI') Print (student_info) Print (student_info [-2]) # [ 'bubble 8', '9 drink' ] Print (student_info [0:. 4: 2]) # [ 'Bo','MALE'] Print ( 'Bo' in student_info) #True Print ( 'Bo' not in student_info) #False # 6, delete del student_info [2] Print (student_info) '' ' ' '' # to master student_info = [ '123', 18 , 'male', [ ' perception', 'call wheat'], 18] # 1 , index get a list of index values Print (student_info.index (18)) # 1 # 2, COUNT get a list of the number of a value of Print (student_info.count (18)) # 2 # 3, values, the default list last value, similar to delete # If the pop () brackets write the index, the index takes a value corresponding to student_info.pop () Print (student_info) Sex = student_info.pop (2) Print (Sex) Print (student_info ) # 4 removed student_info.remove (18 is) Print (student_info) # [ '123', [ 'perception', 'call wheat']] name = student_info.remove ( '123') print(name) #None print(student_info)''' #5, insert the value '' ' student_info = [' 123 ', 95' MALE ', [' embarrassed dance ',' call wheat '], 95] # in student_info, the index location 3 is inserted "Hefei" student_info .insert (3, 'Hefei') Print (student_info) '' ' # . 6, the merge list Extend ' '' student_info1 = [ '123', 18 is, 'MALE', [ 'perception 1', '2 wheat call' ], 18 is] student_info2 = [ '456', 95 'MALE', [ 'embarrassed dance', 'call wheat'], 95] student_info1.extend (student_info2) Print (student_info1) '' '
Tuple: in () may be any type storing a plurality of values, separated by commas.
Note: tuples and lists is not the same, only the initialization value is defined, it can not be modified.
Advantage: occupies memory resources smaller than list.
'''tuple1 = (1, 2, 3, '五', '六') print(tuple1)#(1, 2, 3, '五', '六') print(tuple1[2])#3 print(tuple1[0:5:3])# (1, '五') print(len(tuple1))#5 print(1 in tuple1)#True print(1 not in tuple1)#False for line in tuple1: print(line)'''
Immutable type:
#不可变类型 #int '''number = 100 print(id(number)) #1978694208 number = 111 print(id(number)) #1978694560 #float sal = 1.0 print(id(sal)) #2210740179472 sal = 2.0 print(id(sal)) #2210740179304 str1 = 'hello python!' print(id(str1)) #2210741626416 str2 = str1.replace('hello', 'like') print(id(str2)) #2210741626288'''
Variable type:
'''list1 = [1,2,3] list2 = list1 list1.append(4) print(id(list1)) print(id(list2)) print(list1) print(list2)'''
Dictionary type:
In the {}, separated by commas can store a plurality of values, key-value to access, fast speed value.
Definitions: key must be immutable type, value may be any type
'''dict1 = dict({'age':18,'name': 'nj'}) print(dict1) #{'age': 18, 'name': 'nj'} print(type(dict1)) #<class 'dict'> print(dict1['age']) #18 dict1['level'] = 9 print(dict1) #{'age': 18, 'name': 'nj', 'level': 9} print(dict1['name']) #nj print('name' in dict1) #True print('nj' in dict1) #False print('nj' not in dict1) #True del dict1['level'] print(dict1) #{'age': 18, 'name': 'nj'} print(dict1.keys()) #dict_keys(['age', 'name']) print(dict1.values()) #dict_values([18, 'nj']) print(dict1.items()) #dict_items([('age', 18), ('name', 'nj')]) for key in dict1: print(key) print(dict1[key]) dict1 = {'age': 18,'name': 'nj'} print(dict1.get('sex')) #None print(dict1.get('sex','male')) #male'''
cycle:
# The while loop '' ' str1 =' Tank ' the while True: name = the INPUT (' Please enter the characters guess: ') .strip () IF name ==' Tank ': Print ('! Tank Success ') BREAK Print (' Please re-enter ')! ' '' # limit cycles ' '' str1 = 'NJ' NUM = 0 the while NUM <. 3: name = iNPUT ( 'enter guess characters:.') Strip () IF name == 'NJ': Print ( '! NJ Success') BREAK Print ('! Please re-enter ') NUM = +. 1 ' ''
File handling:
'''with open('file.txt','w',encording='utf-8') as f: f.write('墨菲定律') with open('file.txt','r',encording='utf-8') as f: res = f.read() print(res) with open('file.txt','a',encording='utf-8') as f: f.write('围城')''' '''with open('cxk.jpg', 'rb') as f: res=f.read() print(res) jpg = res with open('cxk_copy.jpg','wb') as f_w: f_w.write(jpg) with open('cxk.jpg', 'rb') as f_r,open('cxk_copy.jpg','wb') as f_w: res = f_r.read() f_w.write(res)'''
function:
# 1, no function parameter '' ' DEF Login (): User = INPUT (' Enter a username ') .strip () pwd = INPUT (' Enter Password ') .strip () IF User ==' Tank ' == pwd and '123': Print ( '! Login successful') the else: Print ( 'Login error!') Print (Login) Login () '' ' # 2, has a function parameter ' '' DEF Login (username, password): the user the iNPUT = ( 'Please enter your user name') .strip () pwd = the iNPUT ( 'Please enter your password') .strip () IF the user == == username and password pwd: Print ( 'the Login successful!' ) the else: Print ( '! the Login error') the Login ( 'Tank ',' 123 ') ' '' # definition phase: position parameter '' 'FUNC DEF (X, Y): Print (X, Y) FUNC (10,100) DEF FUNC (X, Y): Print (X, Y) '' ' # nested function definition ' '' DEF func1 (): Print ( '... from func1') DEF func2 (): Print ( '... from func2') Print (func1) DEF F1 (): Pass DEF F2 (): Pass DIC1 = { '. 1': F1, ' 2 ': F2} choice = INPUT (' select numbers: ') IF == choice'. 1 ': Print (DIC1 [choice]) DIC1 [choice] () elif choice ==' 2 ': Print (DIC1 [ Choice]) DIC1 [Choice] () '' '