In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python function and module knowledge points". In daily operation, I believe many people have doubts about what problems Python function and module knowledge points have. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "Python function and module knowledge points"! Next, please follow the small series to learn together!
(1) What is a function?
A function is a reusable piece of code that implements a single, or related, function. Functions are a good way to partition and organize the execution logic of a program. By using functions, an otherwise complex and lengthy program can be divided into blocks that are called in sequence.
Functions improve modularity of applications and code reuse. From the previous study, you already know that Python provides many built-in functions, such as print(), input(), len(), max(), etc. But you can also create your own functions, which are called user-defined functions.
(2)defined function
In defining a function, we need to follow the following rules:
Function code blocks begin with the def keyword, followed by the function identifier name and parentheses ()
Any passed parameters must be enclosed in parentheses, which can be used to define parameters
The first line statement of the function optionally uses a document string to explain the function
Function content starts with a colon: and is indented to determine program structure
return [Expression] terminates the function, returning a value to the place called, return without an expression is equivalent to returning None.
For example, define a simple function that outputs the larger of two numbers:
def max(a, b):"""Output the greater of a,b"""if a > b:return aelse:return b(3) Call the function
Defining a function simply specifies what the function can do, but does not actually execute it. Only when the function is called can the function really play its role. Below, we simply call the above function:
#define function def max(a, b):"""outputs the larger of a,b""if a > b:return aelse:return b#call function print(" the larger of a,b is: ", max(6, 8))
Essentially, the entire program is executed from the last line. When writing projects, we are more accustomed to writing this way:
def max(a, b):"""Output the larger of a,b"""if a > b:return aelse:return bif __name__ == '__main__':print("the larger of a,b is: ", max(6, 8)) II. Module (1) What is a module
A module is a file containing all the functions and variables you define, with the suffix.py. Modules can be imported by other programs to use functions and other functions in the module.
One of the reasons Python works so well is that it has a wealth of standard modules that are powerful and easy to use. For example, there are os module, sys module, datetime module and so on.
This article mainly takes you to understand how to build your own module, and import the module, or import a function in the module.
(2)Import the entire module
Before importing a custom module, we need to build our own module, named mymath.py. To store some of the functions we define. For example:
def max(a, b):"""Output the larger of a,b""if a > b:return aelse:return bdef min(a, b):""Output the smaller of a,b""if a < b:return aelse:return bdef add(a, b):""Output the sum of a,b""return a + b
Next, we need to import this module in the main file main.py and call the function in it:
#Import module import mymath a = 6b = 8 #Call function print("Output the larger of a,b:", mymath.max(a, b))print("Output the smaller of a,b:", mymath.min(a, b))
When Python reads main.py, the line import mymath tells Python to open the file mymath.py and copy all its functions into the program. You can't see the copied code because Python copies it behind the scenes while the program is running. All you need to know is that at main.py you can use all the functions defined in mymath.py.
(3)Import functions from modules
Sometimes, in the main program, you don't need to use all the functions in mymath.py. At this time, you can import only the function you need, such as the add() function in mymath.py:
#import module from mymath import adda = 6b = 8 #call function print("output sum of a,b:", add(a, b))
Of course, it is also possible to import multiple functions from the mymath.py module simultaneously by:
from mymath import add, max, min
If it is all functions in the import module, you can directly replace it with *, as follows:
from mymath import *(4) Specify alias
Sometimes, the imported module name is too long, and it needs to be used frequently when writing the main program. For convenience, modules are often given a shorter name when imported, such as:
import tensorflow as tf
In this way, not only is tensorflow imported into the main program, but an alias tf is also named, so tf refers to tensorflow in the main program.
Similarly, you can use as to assign aliases to functions. For example:
from mymath import add as d
Thus, our main program can be abbreviated as follows:
from mymath import add as da = 6b = 8#Call function print("Output sum of a,b:", d(a, b))
When we need to do multiple add() operations, this shorthand method has obvious advantages!
At this point, the study of "Python functions and module knowledge points" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.