03. Initial knowledge of the function
def keyword space function name (same as variable setting): English colon
Function body
Execution function: function name + ()
The function is function-oriented,
What is None: It is an independent data type, '' () {} set() [] all point to None in memory
Ternary operation: def man(x,y): return x if x > y else y
return: 1. The return end function is encountered in the function. The following code is not executed.
2. Return the value in the function to the executor (caller) of the function.
The first case:
only return, if nothing is written after it, return none
Second case:
return None
Third case:
return a single value (the value, what data type is returned, is what data type.
) Four cases:
return returns multiple values (in the form of a tuple, returned to the function caller.)
function parameters
What are actual parameters
Positional parameters: one-to-one correspondence, one is indispensable.
Keyword parameters: one-to-one correspondence, the number of formal parameters and actual parameters must be the same. The order of the arguments is variable.
Mixed parameters: positional parameters must be in front, otherwise an error will be reported. (Keyword arguments must come after positional arguments)
What is a formal parameter
Positional parameters: one-to-one correspondence, one is indispensable
Default parameter: 1, which must be placed after the positional parameter of the formal parameter, 2 If the default parameter does not pass a value, it will be the default value, and if the value is passed, it will be overwritten.
Calculate the length of the string;
s1 = 'sdafasfasfsdfsadf' count = 0 for i in s1: count += 1 print(count)
li = [1,23,34,5,56,6,7,8,32,42,23,45,12,323,23,234,2134]
count = 0
for i in li:
count += 1
print(count)
def my_len(a):
count = 0
for i in a:
count += 1
return count
li = [1, 23, 34, 5, 56, 6, 7, 8, 32, 42, 23, 45, 12, 323, 23, 234, 2134]
print(my_len(li))
Four returns of return:
print (my_len(li)) # meaningless # def login(): # print(111) # print(222) # return None # print(333) # login()
#only return, if nothing is written after it, return none # def login(): # return # print(login())
# return a single value (this value, what data type is returned, is what data type.) # def login(): # a = 2 # b = 3 # return [1,2] # a1 = login() # a,b = a1 # print(type(a),b)
# return returns multiple values (as a tuple, to the caller of the function.) # def login(): # a = 2 # b = 3 # return 1,'alex',[1,2],{' name':'old boy'} # print(login())
function parameters
#Execute in sequence from top to bottom in the memory, execute my_len() to add to the memory, go from top to bottom, li is added to the memory, pass the parameter my_len() through the actual parameter # , the formal parameter a accepts parameters from top to bottom Execute sequentially, tooth count = 0, then for iin a: count += 1; return count def my_len(a): count = 0 for i in a: count += 1 return count li = [1,2,3,4,5,6,7,8,9 ] print (my_len(li))
Ternary operation
# Ternary operation def sds(a,b): return a if a > b else b print (sds(3000,2000))
keyword argument
def func(y,x): return type(x) print(func(x=333,y=222))
argument angle