In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail what are the basic functions in Python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Function
explain
A function is a behavior taken to achieve a certain purpose. the function is reusable and is used to achieve a single function or a piece of code. to put it simply, it is a program paragraph composed of a series of program statements.
The meaning of the existence of the function:
1. Improve the reusability of code
two。 Simplify complex logic and make it functional.
Function definition:
Little knowledge: [] means optional representation must-have
It is best that the name of the function can directly represent the function of the function, with _ link between multiple words.
([parameter list]) # statement to be executed # if necessary, explicitly return or
Define a simple function:
Def func (): print ('I executed') print ('program executes normally')
At this time, the statements in the function are not executed, is it a little out of line with the top-to-bottom execution order of the program? That's definitely not true. A function has a characteristic that it can only be executed when it is called.
Def func (): print ('I executed') print ('normal execution of the program') # call function, function name + () func () # function is called first and then defined # throw exception SyntaxError: invalid syntax # func () def func (): print ('I executed') func ()
When the program is running, the code in the function is first loaded into memory and executed directly where there is a call. When the program is called first, the program has not yet read the function into memory, so an exception is thrown.
Def func (): print ('hi') print (func)
The direct print function is the memory address corresponding to the function.
The return value of the function
The function can return any type of data, which ends after the execution of the function to return, and the subsequent code is not executed.
Def func (): res = 1: 1 print ('I executed') return res print ('I won't execute') print ('normal execution of the program') # the statement in the function is indeed executed Print I executed func () # to get the value of the function return, I need to find a variable to receive it # notice that this is printed again because the function called again res = func () print (res) # does not write return defaults to return None def func (): print ('I executed') res = func () print (res)
Multiple return values
Multiple variables receive
Def func (): return 1, 2, 2, 3 a, func, b, C = func () print (a)
The principle is the same:
A variable receiver is a tuple
Def func (): return 1, func 2, 3 res = func () print (res)
Same
Parameters.
Suppose we want to calculate the sum of two numbers now.
Def sue_for_peace (): res = 3'3 return res print (sue_for_peace ())
If you want to calculate the sum of different numbers, you need to pass parameters.
Default transfer by location (location-to-location correspondence)
Def sue_for_peace (num1,num2): res = num1+num2 return resprint (sue_for_peace (3p3))
Def sue_for_peace (num1,num2): res = num1+num2 return res print (sue_for_peace (3p3))
Keyword parameter (corresponding to parameter name)
Def sue_for_peace (num1,num2): print (num1,num2) res = num1+num2 return res print (sue_for_peace (num2=3,num1 = 4))
Mixed use (first position parameter and then keyword parameter)
Def sue_for_peace (num1,num2,num3): print (num1,num2,num3) res = num1+num2+num3 return res print (sue_for_peace (3 Magneto num3 Min num2 = 2))
Default value (if you do not pass a parameter, you will use the default value, if you pass a parameter, you will use the passed value)
Def sue_for_peace (num1=1,num2=2): print (num1,num2) res = num1+num2 return res print (sue_for_peace ()) print (sue_for_peace (5) 10))
When mixed, the parameters are passed first by position, then by keyword, and finally by default
Error demonstration:
Def sue_for_peace (num1=1,num2,num3): # the first line is wrong here, and the default value can only be the last. The editor reports an error print (num1,num2,num3) res = num1+num2+num3 return res print (sue_for_peace (2Magol 3)) def sue_for_peace (num1,num2,num3 = 3): print (num1,num2,num3) res = num1+num2+num3 return res print (sue_for_peace (num2=2,3)) # position first and keyword later Editor error print (sue_for_peace (1 ~ (1) num1 ~ (2) # does not allow two values to be passed to the same shape parameter
Variable parameters (multiple uncertain parameters)
* list * * dictionary
It usually looks like this: * args,**kwargs
Def sue_for_peace (* args): res = 0 for i in args: res + = i return res # any 0 is also true print (sue_for_peace ()) print (sue_for_peace (1,2,3,4,5)) # if there is already a list li = [1,2,3,4,5] # this does not pass the list as a parameter Will be automatically interpreted as multiple parameters print (sue_for_peace (* li)) def func (* * kwargs): return kwargs print (func ()) print (func (name='sb',age=22)) print (func (* * {'name':'sb','age':22})
It usually looks like this:
Def func: print (x) print (y) print (z) print (args) print (kwargs) func (1, 2, 3, 4, 5, 6, aq11, baked 22)
Transfer of variable type parameters
Def func (li): li [0] = 666li = [1,2,3,4,5] func (li) print (li)
It can be found that when the parameter is passed to the parameter, the parameter changes, and the parameter changes accordingly.
Transmission of immutable types
Def func (str1): str1 = 'aaa' print (' in-function:', str1) str1 = 'hell0' func (str1) print (str1)
Don't be fooled by the same variable name.
Global and local variables
The local can use the global variable, and the global variable can be used anywhere in the module (that is, this file). The local variable is the amount of data declared and used within the function, which is born with the startup of the function and dies with the exit of the function. the scope is in the function.
Global variables can be used locally.
Temp = 'hello' def test (): print (temp)
Local variables cannot be used globally
Def test (): temp = 'hello' print (temp) # print (temp) # NameError: name' temp' is not definedtemp = 'Hello' def test (): temp = 'hello' print (temp) print (temp) temp =' Hello 'def test (): temp =' hello' print (temp) test () print (temp)
Look at the example to guess the answer, naughty, very happy.
Num = 1 def set_num (in_num): num = in_num pass set_num (11) print (num)
Declare to use global variables (global)
Num = 1 def set_num (in_num): global num num = in_num pass set_num (11) print (num)
Global variables, local variables with the same name, still need to use global variables.
Num = 11 def test (): num = 22 # this is a local num print (num) # print global num print (globals () ['num']) test () about "what are the basic functions in Python?" this article ends here. I hope the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good. Please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.