current position:Home>Python notes - Day8 - advanced function

Python notes - Day8 - advanced function

2022-05-15 06:03:11Wandering mage 12

Preface
python Grammar learning , Leave it to those who need it , Understand everything !!

# coding=utf8
# @time:2022/4/13 15:04
# Author  Haoyu 

#  One 、 Function advanced 
# 1. The parameters of the function 
# 1) Location parameters and keyword parameters 
'''''''''
 Arguments can be divided into location parameters and keyword parameters according to different chapter transmission methods ;
a、 Positional arguments 
 When you call a function , Let the arguments and formal parameters correspond one by one ( The first argument assigns a value to the first formal parameter , The second argument assigns a value to the second formal parameter ...)

def func1(x,y,z):
    print(f'x:{x},y:{y},z:{z}')
func1(1,2,3)

#  Output :x:1,y:2,z:3


b、 Key parameters 
 Let arguments and formal parameters pass through keywords ( The name of the parameter ) Corresponding ;
 Format : The name of the parameter 1= data 1, The name of the parameter 2= data 2,....

def func1(x,y,z):
    print(f'x:{x},y:{y},z:{z}')
func1(x=123,z=4,y=23)
#  Output results :x:123,y:23,z:4

 Be careful : Whatever the parameters , It must be ensured that all parameters have values !

c、 Keyword parameter and location parameter are mixed 
 When mixing , The location parameter must precede the keyword parameter 

def func1(x,y,z):
    print(f'x:{x},y:{y},z:{z}')
func1(23,z=40,y=22)
#  Output results :x:23,y:22,z:40
'''''''''

# 2. Parameter default 
'''''''''
 When defining a function , You can assign default values to nibbling , When the function is called, the default value of the parameter cannot be assigned 
 Case one :
def func2(x,y,z=100):
    print(f'x:{x},y:{y},z:{z}')
func2(1,2)
#  Output results :x:1,y:2,z:100
——————————————————————————————————
 The second case :
def func2(x,y,z=100):
    print(f'x:{x},y:{y},z:{z}')
func2(1,2,3)
#  Output results :x:1,y:2,z:3

 Be careful :
 Those with default values must be written at the end ;
'''''''''

# 3. Parameter type description 
'''''''''
 When defining a function , Parameter types can be described 
1) Assign default , What is the default value , Parameter is what type 

def func4(x=''):
    pass
func4()
2) The name of the parameter : data type 

def func5(x:list):
    pass
func5()
'''''''''

# 4. Indefinite length parameter 
'''''''''
 Add... Before the formal parameter * Or add **, You can turn this parameter into an indefinite length parameter ; Multiple arguments of indefinite length are acceptable 
1) belt * Indefinite length parameter of 
 belt * The parameter of will become a tuple , The element in the tuple is the corresponding argument 
#  Example :
def func6(*x):
    print(f'x:{x}')
func6()
func6(11)
func6(11,12)
#  Output results :x:() x:(11,) x:(11, 12)

 Be careful :a. A function can exist simultaneously with * And without * Parameters of , If not * In band * Behind , No * The parameter of must use the keyword parameter 
     b. belt * The parameter of must use the position parameter 
2) belt ** Indefinite length parameter of  -    It can only be transmitted 【 Key parameters 】
 belt ** The variable length parameter of will become a dictionary , When calling, use keyword parameters to pass parameters , Every keyword is a dictionary key, The data behind the keyword is the dictionary value

 Be careful :a. When defining, the fixed length parameter must be placed in ** In front of the indefinite length parameter 
     b. belt * And belt ** The indefinite length parameters of can exist at the same time , however * Must be in ** In front of .( If they exist at the same time , It can make the function more flexible when calling )
 #  Example : 
def func10(x,**y):
    print(f'x"{x},y:{y}')

func10(10,a=20,c=30,b=90)
func10(a=20,c=30,b=90,x=100)

#  Example :
def func11(*x,**y):
    print(f'x:{x},y:{y}')
func11(1,2,3)   # x:(1, 2, 3),y:{}
func11(a=11,b=22)   # x:(),y:{'a': 11, 'b': 22}
'''''''''
# def func6(*x):
# print(f'x:{x}')
# func6()
# func6(11)
# func6(11,12)
#  Output results :x:() x:(11,) x:(11, 12)

#  practice : Define a function , You can find the sum of multiple numbers 
# sum1(10,20) sum1(10,20,30)



# 5. Return value 
'''''''''
1) What is the return value 
 The return value is the data passed from the inside of the function to the outside of the function .( If you implement the function of the function and generate new data , Generally, you need to return this new data through the return value )
2) How to determine the return value of a function 
 In the body of a function , adopt return Keyword to return the return value :   return  data 
 Be careful : In the same function , only one return It works .( Because when executing the function body, you only encounter return The function ends directly )
      If you want to return multiple data in a function , Just use a container that can hold multiple data . Common tuples :return  data 1, data 2, data n
3) How to get the return value of the function outside the function 
 To get the value of the function call expression is to get the return value of the function .( The data corresponding to the return value can do , Function call expressions can do )

'''''''''




# 6. Global and local variables 

# 7. Anonymous functions 
# 1) What is anonymous function 
'''''''''
 The essence of anonymous function is still function , But anonymous functions can only realize the function of a function through one statement 
a. grammar :
lambda  Function name   Parameter list : Return value 
 Give the anonymous function a name :
 Function name =lambda  Function name   Parameter list : Return value 

 amount to :
def  Function name ( Parameter list ):
    return  Return value 
'''''''''

#  give an example : Find the sum of two numbers 
#  Conventional methods :
# def sum1(num1,num2):
# return num1+num2
#  Using anonymous functions :
# sum1=lambda num1,num2:num1+num2
# print(sum1(10,20))


#  practice : Write an anonymous function func15, The function is to print the square of a number ?
# func15 = lambda num:print(num**2)
# func15(2) # 4

More secure sharing , Please pay attention to 【 Security info】 WeChat official account !

copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424639.html

Random recommended