In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "python how to write and call functions", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to write python and call functions" article.
Why use functions?
As the project gets bigger and the number of lines of code increases, functions can be used to encapsulate some functions as a module, which makes the code clearer. In addition, encapsulating the repeated code helps to reduce the reuse of the code. Finally, the use of functions can be more convenient for code debugging.
How to write functions in python
Python uses def to define functions. The code structure is as follows:
Def function name (variable 1, variable 2 return...): # define a function using def, which can pass parameters (or no parameters), separated by commas # function internal code variable # you can choose to return a variable or not.
For example:
Def add (a, b): # defines a function named add, and passes in two parameters an and b c = axib # the parameters passed in can be operated. Here a variable c is defined to store the result of the operation return c # returns the value of c using return
How to call a function by python
After you have defined the functions, you can call them directly in the following code. Taking the above add () function as an example, the calling code is as follows:
Def add (a, b): # defines a function named add, and passes in two parameters, an and b c = axib #. The passed parameters can be calculated. A variable c is defined here to store the result of the operation return c # uses return to return the value of c x = 1y = 3add (XMagi y) # directly use the function (usually only functions with no return value will do so) z = add (XMague y) # directly use the function, because the function has a return value, so the formula will assign the return value of the function to zi= add (5Q 6) # all the above example functions pass in variables The value print (add (7)) # can be used in other functions (passing the return result of the function as a parameter to another function). The above is about "how to write and call a function by python". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, 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.