Python It is to organize and save program code through modules , A module file is a text file ( extension .py), It can be written with any text editor .
Python The core software contains a series of modules , among , Built into the interpreter _ build-ins _ Modules are called built-in modules , The built-in module uses C Written language , Contains built-in functions and constants ; Other modules are in the standard library , It's called the standard module .
Python The standard library contains hundreds of modules , for example math、string etc. , A list of complete modules can be viewed https://docs.python.org/3.8/py-modindex.html.
1. Import the entire module
import math # ( Module name )
math.sqrt(9)
When using this method to import a module , Add the module name when calling the function in the module , As shown above .
2. Import functions in the module
from math import sqrt, exp # Import functions in the module
sqrt(9)
When using this method to import a module , Functions can be called directly , meanwhile , You can import multiple functions at the same time by separating function names with commas .
3. Define aliases when importing modules or functions
from datetime import datetime as dt
print(dt.now())
If the name of the module or function to be imported is duplicate or the name is too long , You can specify aliases when importing .
Python Module search path :
Python The search path of the module includes the original search path and the new search path , The original lookup path is installed Python Software has been defined ; For newly installed modules , If there is no longer a search path , Can be in site-packages Folder ( Generally, under this path →D:\Python\Lib\site-packages) Add a path file to the ( text file ), Such as acrpy.pth( Attention must be paid to .pth For the suffix ), Then write the name of the directory where the module file is located in the file , Here's the picture :
( Of course , It is better to directly copy the installed modules or expansion packs to site-packages In the folder .)
In addition, you can also use the sys The module performs related operations , for example :
print(sys.path) # View the current lookup path
print(sys.modules) # View the currently imported module and its path
1. Code indentation
stay python In the code , A compound statement composed of multiple statements ( Also called code block , Such as if sentence ) It is controlled by code indentation .
The first line of the compound statement is marked with a colon (:) end , Here are one or more indented statements , There is no strict limit on the number of indented spaces , But be consistent , It's usually 4 A space .
2. Statement separation
python Generally, line breaks are used ( Except for line breaks in parentheses ) Represents the end of a statement .
3. code annotation
Use # The symbol represents a comment , from # Start , Up to the end of the line are comments .
stay pycharm You can also select a multiline statement in the editor and press “ CTRL+/ ” Shortcut key implementation annotation .
in addition , You can also use three single quotation marks at the beginning and end of the statement to be annotated ( Or double quotes ) Implement cross line annotation , A comment usually used for program headers .
…