# # # # '' 'List' '' ## Definition: [], the plurality of values can be stored any data type, separated by commas # # Define a list of students, storing more than one student # List ([ 'dog is better than great,' 'than the Sun,' 'Wang Jie']) # Students = [ 'second child', 'baby'] # Print (students [1]) # doll # # Student_info = [ 'Yangmeikeng', 45 'MALE', [ 'drink', 'food'], 45] # Print (student_info [3]) # all interested # Print (student_info [3] [1]) # the second hobby # # #add to # Student_info.append ( 'Hefei') # print(student_info) # # # Delete # Of student_info [2] # print(student_info) # #I get a list of index values # print(student_info.index(45)) ## 2 Number obtain a list of values # print(student_info.count(45)) # # 3 value, default take the last value in the list, similar to the deleted # # If the pop () wrote in the index, then take the value of the index # student_info.pop() # print(student_info) # # Remove the list index value of 2, and assigned to the sex variable name # sex=student_info.pop(2) # print(sex) # print(student_info) # # 4 removed, the value of a first value in the list removed # student_info.remove(45) # print(student_info) # # Name = student_info.remove ( 'Yangmeikeng') # print(name) #None # print(student_info) # # # 5. Interpolated values # Student_info.insert (3, 'Hefei') # print(student_info) # # # 6.extend merge list # Student_info2 = [ 'dog than Wei', 46, 'female', [ 'drink 1', '2 gourmet']] # Student_info1 = [ 'Yin Haoqing', 46, 'female', [ 'drink', 'food']] # student_info1.extend(student_info2) # print(student_info1) # Tuple: #Define: # In () may be any type storing a plurality of values, separated by commas # Note: tuples and lists different, can only initialize the value at the time of definition, can not be modified # Advantage: occupies minimal memory resources to be smaller than the list # Tuple ((1,2,3, 'five', 'six')) # Tuple1 = (1,2,3, 'five', 'six') # print(tuple1) # # for line in tuple1: # print(line) # print(line,end='_') """"" ''' Dictionary type: effect: In the {}, separated by commas can store a plurality of values, Access to key-value, high value of speed. definition: key must be immutable type, value may be any type ''' # dict1 = dict({'age': 18, 'name': 'tank'}) # dict1 = {'age': 18, 'name': 'tank'} # print(dict1) # {'age': 18, 'name': 'tank'} # print(type(dict1)) # <class 'dict'> # Values, Dictionary name + [], brackets write value corresponding key # print(dict1['age']) # Master priority actions: # 1, the access key press value: deposit may be desirable # Deposit a level: 9 to the value of the dictionary dict1 # dict1['level'] = 9 # print(dict1) # {'age': 18, 'name': 'tank', 'level': 9} # print(dict1['name']) # tank # # # 2, the length len # # # 3, members and not in operation in only judge the dictionary key # print('name' in dict1) # True # print('tank' in dict1) # False # print('tank' not in dict1) # True # # # 4, delete # del dict1['level'] # print(dict1) # {'age': 18, 'name': 'tank'} # # # 5, key Keys (), the value of values (), on the key-value items () # # Get a dictionary of all key # print(dict1.keys()) # # Get all the values values in the dictionary # print(dict1.values()) # # Get all the items in the dictionary # print(dict1.items()) # 6, circulation # Loop through the dictionary all the key # for key in dict1: # print(key) # print(dict1[key]) # get dict1 = {'age': 18, 'name': 'tank'} # print(dict1.get('age')) # [] Values # print(dict1['sex']) # KeyError: 'sex' # Get values print(dict1.get('sex')) # None # If no sex, to set a default value print(dict1.get('sex', 'male')) ''' File handling: open() Write file wt: write text Reading file rt: Reading text Additional write file at: Append text Note: You must specify character encoding, in what way to write In what way will be open. Such as: utf-8 Python file execution process: 1. first start the python interpreter, loaded into memory. 2. Load written python files to the interpreter. 3. Detection python syntax to execute code. SyntaxError: syntax error! Open the file will have two resources: 1.python program 2. Open the file operating system ''' # Write text files Absolute path to the file: a parameter # Mode mode operation file: # Parameter Two # Three parameters: encoding specified character encoding # f = open('file.txt', mode='wt', encoding='utf-8') # f.write('tank') # F.close () # close the operating system files resources # Read a text file r == rt # f = open('file.txt', 'r', encoding='utf-8') # print(f.read()) # f.close() # # ## additional write text files # a = open('file.txt', 'a', encoding='utf-8') # A.write ( '\ n HEFEI') # a.close() ''' File processing of context management. with open() as f "句柄" ''' # Write # with open('file1.txt', 'w', encoding='utf-8') as f: # F.write ( 'Murphy's Law') # # # Reading # with open('file1.txt', 'r', encoding='utf-8') as f: # res = f.read() # print(res) # # # add to # with open('file1.txt', 'a', encoding='utf-8') as f: # F.write ( 'Siege') # # f.close() ''' For pictures, audio, video, reading and writing rb mode, read binary not necessary to specify the character encoding ''' # Read photo cxk.jpg with open('cxk.jpg', 'rb') as f: res = f.read() print(res) jpg = true # The cxk.jpg binary stream file written cxk_copy.jpg with open('cxk_copy1.jpg', 'wb') as f_w: f_w.write(jpg) ''' with managing multiple files ''' # To manage two f_r open file handles opened through with, f_w with open('cxk.jpg', 'rb') as f_r, open('cxk_copy2.jpg', 'wb') as f_w: # Handle to the picture by f_r binary stream read out res = f_r.read() # By f_w handle the flow picture binary file written cxk_copy.jpg f_w.write(res) ''' Three ways function declaration 1 no reference functions do not require the external input parameter There are 2 function parameters 3. Empty function ''' '''''' ''' def function name (parameter 1, parameter 2 ...): "" "Note: Statement function" "" Logic code return return value def: defind definition. Function name: must see its name knowing Italian. (): Receiving the incoming external parameters. Notes: used to declare the action function. return: the return value to the caller. ''' ''' Three forms defined functions: 1. The no-argument function Not need to receive incoming external parameters. 2. Reference function Need to receive external incoming parameters. 3. Empty function pass Function call: Function name + () call ''' # # 1 no-argument function # def login(): # User = input ( 'Please enter your user name') .strip () # Pwd = input ( 'Please enter your password') .strip () # # if user == 'tank' and pwd == '123': # print('login successful!') # # else: # print('login error!') # # # # Memory address of the function # print(login) # # # # Function call # login() # 2. Reference function # Username, password used to receive an external incoming value # def login(username, password): # User = input ( 'Please enter your user name') .strip () # Pwd = input ( 'Please enter your password') .strip () # # if user == username and pwd == password: # print('login successful!') # # else: # print('login error!') # # # # Function call If the function ## needs to receive parameters when defining a caller must pass through their participation # login('tank', '123') # 3 Empty function ''' ATM: 1. Log 2. Registration 3. Withdraw 4. Withdrawal 5. Transfer 6. Repayment ''' # # Log function # def login(): ## representatives to do nothing # pass # # # # Registration function # def register(): ## representatives to do nothing # pass # # # # Payment function # def repay(): # pass # ... ''' Function parameters: ''' During the definition phase #: x, y call parameter. # def func(x, y): # x, y # Print (x, y) # # # Call the stage: 10, 100 called the argument. # func(10, 100) # ''' # Positional parameters: Location parameter # Position # argument # Eleven must pass parameters by position. # ''' During the definition phase ##: position parameter # def func(x, y): # x, y # Print (x, y) # # # # # Call the stage: 10, 100, said location argument. # func(10, 100) # 10 100 # # ''' # Keyword arguments: # Keyword arguments # Pass parameters by keyword. # ''' ## position parameter x, y # def func(x, y): # Print (x, y) # # # Call stage: x = 10, y = 100 call key parameter. # func(y=111, x=10) # 10 111 # No less Biography # Func (y = 111) # error TypeError # No more pass (Y = 111, x = 222, z = '333') # func # error TypeError ''' Default parameters: During the definition phase, set a default value for the parameter ''' # def foo(x=10, y=20): # Print (x, y) # ## does not pass parameters, the default parameter # foo() # # # Pass parameters using the parameters passed # foo(200, 300) Nested function is defined: Defined function within the function. Function object: Memory address of a function called a constructor object. Function namespace: Built-in: python parser comes with both called "built-in namespace." Global: All wore head to write variables, functions ... are called "full name space." Local: Within the definition of the function are called "local name space." Namespace load order: Built----> Global ---> Local Namespace search order: Local ---> Global ---> Built ''' # Nested definitions function def func1(): print('from func1...') def func2(): print('from func2...') # Function object 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]() x = 10 # Namespaces # Nested definitions function def func1(): # x = 20 print('from func1...') print (x) # error x = 30 def func2(): print('from func2...') func1 ()