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/02 Report--
Today, I will talk to you about how to parse the basic functions in Python and their common usage, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
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 ('Program executes normally')
# call function, function name + ()
Func () # function is called first and then defined
# throw an 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 = 1x 1
Print ('I executed')
Return res
Print ('I won't execute')
Print ('Program executes normally')
The statement in the # function was indeed executed, and the print I executed
Func ()
# if you want to get the value of the function return, you need to find a new variable to receive it
# notice that it was printed again and I executed it because the function was called again
Res = func ()
Print (res) # does not write return returns None by default
Def func ():
Print ('I executed')
Res = func ()
Print (res)
Multiple return values
Multiple variables receive
Def func ():
Return 1,2,3
A _ func ()
Print (a _
The principle is the same:
A variable receiver is a tuple
Def func ():
Return 1,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 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) num3 (1))
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 and 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, 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 (2)) 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 before keyword, editor error
Print (sue_for_peace (1num1num2)) # does not allow two values to be passed to the same 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 right
Print (sue_for_peace ())
Print (sue_for_peace (1,2,3,4,5))
# if you already have a list
Li = [1,2,3,4,5]
# instead of passing the list as a parameter, it 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 (xrem ypender zrem argsje jungle kwargs):
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] = 666
Li = [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 ()
After reading the above, do you have any further understanding of how to parse the basic functions in Python and their common usage? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.