In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to create and call functions in python. It is very detailed and has a certain reference value. Friends who are interested must finish reading it.
Create and call functions
Let's open our Jupyter Notebook from the "python_bootcamp" folder. Once opened, create a new file and rename it "Week_05". Next, create a cell named "create & call function" for markdown. We will write code in this cell.
What is a function?
A function is a piece of code that runs only when it is called.
You can pass data (called parameters) to the function.
Function can return data as a result.
Programs often need to run the same code over and over again, and while loops help, we don't want to write the same loop multiple times throughout the program. The solution to this problem is to use a function. The code for the function runs only when it is called.
All functions are usually associated with a task or process. This makes it easier for us to split the program into functions. If you build a program that needs to print five lines of information repeatedly and need to output it in five different places, you need to write 25 lines of code. Using a function, you can store these five lines of code in a block and call the function when needed, resulting in five lines for outputting information and five lines for calling the function, with a total of 10 lines of code. This would be a more efficient program.
Function syntax
Like loops, the creation of functions follows a fixed structure. They all start with the keyword "def", followed by the function name. This name is anything except the Python keyword and the previously defined function. The function name is followed by parentheses, and these parentheses are parameters. We will discuss the parameters tomorrow, so just know that the parameters are optional, but the parentheses are required. Finally, we need a closing colon like other Python statements. For an example, see figure 5-1.
Figure 5-1
Write your first function
Now that we know what the syntax structure looks like, let's write our own function:
# write your first function
Def printInfo (): # define a function
Print ("Name: John Smith")
Print ("Age: 45")
PrintInfo () # call function
PrintInfo () # call the function again
Continue and run the cell. We define a function called printInfo that prints two lines of information each time it is called. Let's call the function twice, and it outputs the information twice. This may seem useless, but if you need to output information 20 times in a program, it is very concise and efficient.
The stage of the function
In Python, each function has two phases. The first stage is the function definition. This phase defines the name of the function, any parameters it should accept, and what it should do in the block of code associated with it. See figure 5-2.
Figure 5-2
The second phase is called function call. Functions never run until they are called, so you can define any number of functions, and if you never call one of them, nothing will happen. When you call a function, it runs the code in the function definition.
User-defined function VS built-in function
Before you know it, you've been using functions. Functions such as range, print, and len are called "built-in" functions. They are included in python because they have a specific purpose to help build applications. Now that we are learning about functions, we can start to create our own "user-defined functions".
Implement a calculator
Let's look at another example of a basic function, but this time not just print information inside the function:
# perform calculations within a function
Def calc ():
X, y = 5,10
Print (x + y)
Calc () # will run the calculation code and output more than 15 of the contents of the article "how to create and call functions in python". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.