First encounter function

Began to recognize functions

Study notes for learning python 2, study in the "Knowledge Lecture Hall" at station B

1. Simple understanding
What is a function: a combination of a series of Python statements that can be run one or more times in the program,
generally to complete specific independent functions.
Why use functions:
maximize code reuse and minimize redundancy Code, the overall code structure is clear, and the problem is localized.
2. Function definition
1. def function name ()
code block
# This completes the definition of the function.
Example:
def information():
print('Xiao Zhang's height is %f'% 1.73)
print('Xiao Zhang's weight is %f'% 160)
print('Xiao Zhang's hobby is %s'%'singing')
print('Xiao Zhang's height is %s'%'Computer Information Management')
pass #The
above is a defined function, which cannot be run Yes, you need to call the function

# Carry out function call
information()
Insert picture description here
#When we need to call multiple times, we only need to use the function multiple times.
#Sometimes we need to annotate the function to help us understand the function in the future. At this time, we need''' annotation.

2. The above function can only be used to print small sheets of information multiple times. In order to print the information of multiple people, the concept
of function parameters needs to be introduced. Example:
def printInfo(name,height,weight,hobby,pro): #in the brackets It is a formal parameter
# Function code block
print('%s' height is %f' %(name,height))
print('%s's weight is %f' %(name,weight))
print('%s' hobby It is %s' %(name,hobby))
print('%s' major is %s' %(name,pro))
pass #The
function is defined, the following is to call
printInfo('小李',189,200,'playing the game ','咨询师') # This parenthesis is the actual parameters
Insert picture description here
# It is also possible to print the information of multiple people at the same time. You only need to call this function again and change the actual parameters in the function parentheses.
In this way, the function definition in 1 can be perfected.

def function name (parameter list)
code block
#This completes the definition of the function

3. Parameters
Parameter: In fact, the function is to achieve a certain function, and then in order to obtain the data needed to realize the function, it is to obtain external data.
The classification of the parameters: mandatory parameters, default parameters [default parameters], optional parameters, keyword parameters
(1) mandatory function
# when calling the mandatory parameters must be assigned
def sum (a, b): #Formal parameter: It is just a kind of parameter in the sense, when it is defined, it is
sum=a+b that does not occupy the memory address.
print(sum)
pass
sum(20,15) #20 and 15 are actual parameters: actual parameters, actual The actual parameter is the actual memory address occupied
(2) Default parameter [default parameter]
def sum1(a,b=40,c=90):
print('Use of default parameter=%d'%(a+b ))
pass #default
parameter call
sum1(10)
#If no value is assigned when calling, the default value given when defining the function will be used sum1(2,56)#If a value is assigned when calling, follow the assigned value run value
Insert picture description here
(3) variable parameters
# used when the number of uncertain parameters, flexible
use defined *

Generally represented by *args

def getComputer(*args): #Variable length parameter
'''
Calculate cumulative sum
: param args: Variable length parameter type
: return:
'''
result=0
for item in args:
result+=item
pass
print(' result=%d'%result)
pass
#for loop belongs to the part of the program that defines the function
#The above is to define the function
getComputer getComputer(1)
getComputer(1,2)
getComputer(1,2,3)
getComputer(1,2, 3,4,5,6,7,8)
Insert picture description here
(4) Keyword variable function
# Need to use ** to define
# in the function body parameter keyword is a dictionary type key is a string

def keyFunc(**kwargs):
print(kwargs) #The function of the function is to print kwargs
pass #Function
call
dictA={"name":'Leo',"age":35}
keyFunc(**dictA) #Want to succeed To print it out, you need to add
#keyFunc(name='peter',age=26,) in front of dictA. This is also a way to declare directly in parentheses.
Insert picture description here
(5) Combination function
must be selected> default (default)> available Select>Keyword
def complexFunc(*args,**kwargs):
print(args) # Optional parameter: the accepted data is a tuple type
print(kwargs) #Keyword optional parameter: the accepted data is a dictionary type
pass
complexFunc(1,2,3,4,name='Andy Lau')
print('---------------Dividing line------------- ----')
complexFunc(age=36)
Insert picture description here
Note: The optional parameter must be placed before the keyword optional parameter, otherwise an error will be reported

3. The return value of the function

Concept: After the function is executed, it will return an object. If there is the return keyword inside the function, it can return the actual value, otherwise it will return None.

Type: Any type can be returned, and the return value type should depend on the type behind return

Purpose: return data to the caller

There can be multiple return values ​​in a function body: but there must be only one return

Note: If return is executed in a function body, it means that the function is executed and exited, and the code statement after return will not be executed

def Sum(a,b):
sum=a+b
return sum#Return the result of the calculation
pass
rs=Sum(10,30) #Assign the return value to other variables
print(rs) #The return value of the function returns to Call place

#You can also print directly(sum(10,30))
Insert picture description here

(1)、
def calComputer(num):
result=0
i=1
while i<=num:
result+=i
i+=1
pass
return result
pass
#开始调用函数
value=calComputer(10)
print(value)
print(type(value))
Insert picture description here

(2),
def calComputer(num):
li=[]
result=0
i=1
while i<=num:
result+=i
i+=1
pass
li.append(result)
return li
pass #call
function
value=calComputer(10 )
print(type(value)) #value type
print(value)
Insert picture description here
Note: As can be seen from the above two examples, return can return any type, and the return value type should depend on the type behind return

4. The nesting
of functions is to call a previously defined function in a function.

Classification of functions: According to the return value of the function and the parameters of the function, it can be divided into

Those with parameters and no return value Those
with parameters and return value

Guess you like

Origin blog.csdn.net/qq_44921056/article/details/108562780