[Python Learning]—Python Basic Grammar (7)

1. Abnormality

When an error is detected, the Python interpreter cannot continue execution and instead displays some prompt errors.
Insert image description here
Insert image description here
Basic syntax:

try:
   可能发生的错误
except:
   如果出现异常执行的代码

Insert image description here

2. Exception transmission

Insert image description here
Insert image description here

3. Module

Insert image description here
Insert image description here

import time
print("你好")
time.sleep(5)
print("你好")
from time import *
print("你好")
sleep(5)
print("你好")
#使用from 导入time的sleep功能(函数)
from time import sleep
print("你好")
sleep(5)
print("你好")

asdefine alias


Insert image description here

4. Custom modules

Insert image description here
Each Python file can be used as a module. The name of the module is the name of the file. That is, the custom module name must comply with the identifier naming rules. Insert image description here
Note: When importing multiple modules, and the modules have the same function, when this function is called, the function of the module imported later is called.
Insert image description here
Insert image description here

5. Python package

Physically, a package is a folder, which contains a file_init_.py. This file can be used to contain multiple module files. Logically, The essence of a package is still a module.
Insert image description here
Insert image description here
Insert image description here
Insert image description here

6. Third-party packages

Insert image description here

Install third-party packages—pip

The installation of third-party packages is very simple, we only need to use Python's built-in pip program.

Open the command prompt and enter

pip install 包名称

You can quickly install third-party packages through the network

Insert image description here

Network optimization for pip

Since pip is connected to foreign websites to download packages, sometimes the speed will be very slow.

We can use the following command to connect to domestic websites for package installation:
It is recommended to use (Tsinghua Mirror Source)

pip install - i https://pypi.tuna.tsinghua.edu.cn/simple 包名称

Install third-party packages—PyCharm

Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/m0_46374969/article/details/134099655