Summary of 2010.4.10 S21 Day10 Notes
I. Content Review
-
Interview questions are relevant:
1.The difference between py2 and py3
2. Operator calculation: 3 or 9 and 8
3. String inversion
4. The difference between is and===
-
5.v1 = 1) V2 = 1 V3 = 1,) What's the difference?
v1, v2 are numbers, v3 is tuples
6. Online Operating System: centos
7. List the methods you understand for each data type
8. Deep and shallow copies
-
9. File operation, how to read large files (such as 50G log files)
v=open(……)
for line in v:
print(line)
v.close()
10. Write in one line: 9*9 multiplication table
git process
-
-
function
The method (data type) is similar to a function, but not a function.
Pass parameters strictly in sequence: position mode
Actual parameters can be of any type
When return has no return value, return None by default
The role of return: 1. Return value 2. Termination function execution
In the internal execution of a function, it terminates when it encounters return
Return can return any type
Special case: return 1,2,'alex'means that the tuple returned is the same as return(1,2,'alex').
-
II. Today's Contents
1. parameter
1.1 Basic parameter knowledge
The parameters can be any number.
-
It can be of any type
def func(a1,a2,a3,a4): print(a1,a2,a3,a4) func(2,'name',[1,2,3],False)
1.2 Location parameter (calling function and passing parameter) (execution)
Input parameters strictly in place order when calling/executing functions
def func(a1,a2,a3): print(a1,a2,a3) func(66,'alex',3)
1.3 Keyword Reference (Execution)
Keyword parameterization is the use of parameterization in arguments.
def func(a1,a2): print(a1,a2) func(a1=22,a2=8)
Keyword input and location reference can be mixed: location input parameters should be placed in front, keyword input should be placed behind, and finally equal to the total number of parameters.
def func(a1,a2,a3): print(a1,a2,a3) func(1,2,a3=3) func(1,a2=2,a3=3) func(a1=1,a2=2,a3=3) func(a1=1,2,3) # It's wrong.
def func(): the custom function open() are python's built-in functions
pass len()
1.4 Default parameter
def func(a1,a2,a3=9,a4=10): # The default parameters a3=9,a4=10 print(a1,a2,a3,a4) func(11,22) # If the values of A3 and A4 are not passed, then A3 and A4 are equal to the default parameters. func(11,22,10) func(11,22,10,100) func(11,22,10,a4=100) func(11,22,a3=10,a4=100) func(11,a2=22,a3=10,a4=100) func(a1=11,a2=22,a3=10,a4=100)
15 Universal Parameters (for Dispersion)
-
*args
It can accept any number of position parameters and convert them into tuples.
- Call function without*
def func(*args): print(*args) func(1,2) ==> (1,2) func(1,2,[12,3,4]) ==> (1,2,[12,3,4]) func((11,22,33)) ==> ((11,22,33)) # The parameter is a tuple, and the print effect is tuple set tuple.
The calling function has*
def func(*args): print(*args) func(*(11,22,33)) ==>(11,22,33) # * It is used to break up tuples, with each element in the tuple as a parameter. func(*[11,22,33]) ==>(11,22,33) # * It can be used to hash lists / tuples / dictionaries / collections, just the internal elements of loops
Reference can only be transmitted by location
def func(*args): print(args) func(1) func(1,2) # args=(1, 2) func((11,22,33,44,55)) # args=((11,22,33,44,55),) func(*(11,22,33,44,55)) # args=(11,22,33,44,55)
-
**kwargs
Can accept any number of keyword parameters, and see the parameters into a dictionary
Call function without*
def func(**kwargs): print(***kwargs) func(k=1) **kwargs = {'k':1} func(k1=1,k2=3) **kwargs = {'k1':1,'k2':3}
The calling function has*
def func(**kwargs): print(**kwargs) func(**{'k1':1,'k2':4,'k3':9}) **kwargs = {'k1':1,'k2':4,'k3':9}
Reference can only be transmitted by keywords
-
* Comprehensive use of args/**kwargs: invincible + invincible=> truly invincible
def func(*args,**kwargs): print(args,kwargs) func(1,2,3,4,5,k1=2,k5=9,k19=999) *arg = (1,2,3,4,5) **kwargs = {'k1':2,'k5':9,'k19':999} func(*[1,2,3],k1=2,k5=9,k19=999) *arg = (1,2,3) **kwargs = {'k1':2,'k5':9,'k19':999} func(*[1,2,3],**{'k1':1,'k2':3}) *arg = (1,2,3) **kwargs = {'k1':1,'k2':3} func(111,222,*[1,2,3],k11='alex',**{'k1':1,'k2':3}) *arg = (111,222,1,2,3) **kwargs = {'k11':'alex','k1':1,'k2':3}
1.6 Key Points Related to Parameters
-
Defined function
def func(a1,a2): pass def func(a1,a2=None): # For default values, the immutable type is written casually, if it is a variable type (pitted). pass def func(*args,**kwargs): pass
-
Calling function
Position parameters are in the first place and keyword parameters are in the second place.
2. scope of action
In the python file:
py file: global scope
-
Function: Local scope
a = 1 def s1(): x1 = 666 print(x1) print(a) print(b) b = 2 print(a) s1() a = 88888 def s2(): print(a,b) s1() s2()
-
Data in each scope can only be invoked by the scope itself. If the data invoked in the scope is not available, the global scope can be invoked.
Global scope can only call Global
Functions in global scope can call each other (calling existing ones), but they cannot call scopes directly in scope.
Conclusion:
1. A function is a scope.
-
2. Data search rules in scopes: first, find data in their own scopes, they did not go to the "parent" - > "parent" - > until the whole situation, the whole situation without error. (Scope nesting)
Note: What is the value in the parent scope?
x = 10 def func(): x = 9 print(x) func()
Exercises in class
x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) func() x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) x1() func() x = 10 def func(): x = 9 print(x) def x1(): x = 999 print(x) print(x) x1() func() x = 10 def func(): x = 8 print(x) def x1(): x = 999 print(x) x1() print(x) func() x = 10 def func(): x = 8 print(x) def x1(): print(x) x1() print(x) func() x = 10 def func(): x = 8 print(x) def x1(): print(x) x = 9 x1() x = 10 print(x) func() x = 10 def func(): x = 8 print(x) def x1(): print(x) x1() x = 9 x1() x = 10 print(x) func()
-
3. Values in the parent can only be found in the child scope, and cannot be reassigned to variables in the parent by default.
-
No assignment, only internal modification of variable types
# ##################### name = 'oldboy' def func(): name = 'alex' # Create another such value in your own scope. print(name) func() print(name) # ##################### name = [1,2,43] def func(): name.append(999) print(name) func() print(name)
-
If you have to assign global variables, you need to add global (mandatory assignment)
#Example 1 name = "Old boy " def func(): global name name = 'alex' func() print name # Example two name = ["Old boy",'alex'] def func(): global name name = 'I' func() print(name) # Example three name = "Old boy" def func(): name = 'alex' def inner(): global name name = 999 inner() print(name) func() print(name)
-
To assign a parent variable to a nonlocal value, first find the parent variable and then assign it (mandatory assignment)
name = "Old boy" def func(): name = 'alex' def inner(): nonlocal name # Find the name of the previous level name = 999 inner() print(name) func() print(name)
-
Supplement: Global variables must all be capitalized
USER_LIST = [11,22,3] def func(): name = 'asdf' USER_LIST.append(12) USER_LIST.append(name) func() print(USER_LIST)