In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people don't understand the knowledge points of this article, so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article.
What is a function?
A function is an organized, reusable piece of code that implements a single, or related, function. Functions improve modularity of applications and code reuse. You already know Python provides many built-in functions, such as print(). But you can also create your own functions, which are called user-defined functions.
In short, functions are used a lot in our daily life, but most of them are officially defined functions. We can call them directly, such as input(),print(), etc., but we don't care how it is defined. If our code needs a lot of reuse of a series of complex code blocks, then we can define a function to represent this code block, and call it directly when needed!!
1. function call
A function consists of three parts: the function name, parameters, and return value.
The function name is the identifier of the function.
A function's arguments are the data that is supplied to the function when it is called.
name = input("Please enter your name: ")list = len(name)print(list)
Here, input, len, print is the name of the function, the function in parentheses is the parameter, and the left side of the equal sign is the return value.
Call function: generally add parentheses to the function name. Parentheses can be filled in with parameters that provide data for the function. Of course, some functions do not require parameters (list.clear()), and some functions must pass parameters (list.append()).
2. defined function
The def keyword is used to define a function, ending with a colon.
Functions must be defined before calling
def name(): print ('Su Liang') def QQ_num(): print('787991021')def Total(): name() QQ_num() Total()
Definition function:
Function header: keyword def + custom function name in parentheses, and finally with a colon. def name(),def QQ_num(),def Total()
Function Body: The function that needs to be implemented. That is, the body of the function must be indented 4 characters. A tab key.
Note: The execution of the function is top-down, that is, the function must be defined before calling.
3. function parameters
Function parameters allow us to define functions that are more flexible.
Note: If parameters are passed when defining a function, parameters must also be specified when calling it.
Parameters can be passed in one parameter or in multiple parameters.
#Pass in an argument def list(len): print ('+' * len)list(5)#pass multiple arguments def list2(num1 , num2): print(num2 * num1)list2('*',15)list2(5,10)
Function calls are given actual values (arguments), which assign values to defined parameters (parameters).
Note: When multiple parameters are passed in, pay attention to whether the number and order of parameters are correct, and the meaning of functions in different orders is different.
4. function return value
A function can return a single value or multiple values. Use return to return a value.
Note: The function ends when it reaches return. That is, the function body after return will not be executed again.
def num(age,sex): if age200: return else: return age,sexx = int(input ('input age:'))Sex = input ('input gender:') num ,sex = num(x,Sex)print(num,sex)
The function must receive as many values as it returns, otherwise an error will be reported. Individual values are returned separately.
A special case is when a variable is accepted, in which case the value returned is a tuple type!
result = num(x,Sex)print(result)
Summary: The function can return a single value or multiple values. When multiple values are returned, multiple variables need to be used to receive the value returned by the function. If only one value is received, a tuple type value is returned.
5. variable scope
The scope of a variable: that is, where the variable can be used. This involves both global and local variables.
Global variable: A variable defined outside a function. It can be used either inside or outside the function! ? Global variables can only be used in the function body and cannot be modified directly!
Local variables: variables defined within a function, variables defined within the function can only be used and modified within the function body, and calls outside the function are invalid. You can define a variable inside a function with the same name as outside the function, but their meaning is different!
a = 15 #where a is global def num(): a = 5 #where a is a local variable with the same name but different values print(a)num()print(a)
Results:
Here you can see that local variables are those that cannot modify the value of global variables.
a = 15 #where a is global def num(): #Global variables can be used in function bodies print(a) num() #Result 15print(a) #Result 15
Within the body of a function is the ability to use global variables.
a = 15 #where a is global def num(): global a #define global variables a = 5 print(a)num() #Result 5print(a) #Result 5
If you want to modify a global variable in the function body, you need to define the global variable before modifying it. At this time, the variable a in the function body is a global variable and is no longer a local variable defined in the function body.
The above is the content of this article about "Python basic syntax function application example analysis". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will be helpful to everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.
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.