current position:Home>Python notes - Day8 - advanced function
Python notes - Day8 - advanced function
2022-05-15 06:03:11【Wandering 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
The sidebar is recommended
- Introduction to the differences between Python and Java
- Explain Python CONDA in detail
- The pycham downloaded by MAC reports an error as soon as it is opened. The downloaded Python interpreter is also the latest version
- From entry to mastery, python full stack engineers have personally taught Python core technology and practical combat for ten years
- Python is used to detect some problems of word frequency in English text.
- How to choose between excel, database and pandas (Python third-party library)?
- WxPython download has been reporting errors
- Pyside6 UIC and other tools cannot be found in the higher version of pyside6 (QT for Python 6). How to solve it?
- About Python Crawlers
- Successfully imported pandas, unable to use dataframe
guess what you like
How to extract some keywords in the path with Python
Python encountered a problem reading the file!
When Python is packaged into exe, an error is reported when opening assertionerror: C: \ users \ Acer \ appdata \ local \ temp\_ MEI105682\distutils\core. pyc
Eight practical "no code" features of Python
Python meets SQL, so a useful Python third-party library appears
100 Python algorithm super detailed explanation: a hundred dollars and a hundred chickens
[fundamentals of Python] Python code and so on
When Python uses probit regression, the program statement is deleted by mistake, and then it appears_ raise_ linalgerror_ Unrecognized error of singular
Python testing Nicholas theorem
Accelerating parallel computing based on python (BL) 136
Random recommended
- Python dynamic programming (knapsack problem and longest common substring)
- Django uses queryset filter save, and an 'queryset' object has no attribute 'Save' error occurs. Solution?
- Analysis of built-in functions in Python learning
- Python office automation - 90 - file automation management - cleaning up duplicate files and batch modifying file names
- Python office automation - 91 - word file Automation - word operation and reading word files
- After python, go also runs smoothly on the browser
- Self taught Python 26 method
- Summary of Python Tkinter component function examples (code + effect picture) (RadioButton | button | entry | menu | text)
- Python implementation of official selection sorting of Luogu question list
- Application of Django template
- Get project root path and other paths in Python project
- Get, rename, and delete file names in Python projects
- How to set the width and height of Python operation table
- Python string preceded by 'f' R 'B' U '
- JSON and other types convert to each other in Python
- Key value of key combination in pynput in Python
- Conversion of Python PDF file to word file
- Interface testing uses Python decorators
- Get the current time in Python
- Python course notes -- Python string, detailed explanation of related functions
- Python file and folder operations
- Python file content operation
- Three basic data quality evaluation methods and python implementation
- Python data structure and mathematical algorithm examples (continuously updating)
- GUI interface mail sending program based on SMTP protocol server and python language
- Python application tool development: mail sending GUI program
- Python application tool development: PIP instruction GUI program
- Application development based on Python and MySQL database: student information score management system version 1.0
- [Python] sort and sorted
- [Python] create a two-dimensional array with list, which is easy to step on
- Multiply [email protected]
- About creating in Python project folder__ init__. Understanding of PY
- Python, zsbd
- Smplify introduction and in python2 7 operation in environment
- Boost(2):boost. Python library introduction and simple examples
- Boost (3): encapsulate C + + classes into Python classes
- Boost (5): extract the type of C + + language and extract class from Python object
- Boost(6):Boost. How Python converts C + + parameter and return value types
- Boost(7):Boost. Python encapsulates overloaded functions and passes default parameters
- Boost(8):Boost. Python implements Python objects and various types of built-in operations and methods