1. Abnormal
Exception: When an error is detected, the Python interpreter cannot continue to execute. Instead, some error prompts appear. This is the so-called “exception”, which is what we often call BUG.
1.1 Catching Exceptions
Basic syntax:
try: error code may occur except: Code to be executed if an exception occurs
Code example:
# Try to open the file in r mode. If an error is reported, open it in w mode. try: open('quiz.txt', 'r') except: open('quiz.txt', 'w')
1.2 Capturing specified exceptions
If the exception type of the code you are trying to execute does not match the exception type you want to catch, the exception cannot be caught.
Generally, there is only one line of code trying to execute below the try.
Basic syntax:
try: error code may occur except NameError as e: Code to be executed if an exception occurs
Code example:
try: print(name) except NameError as e: print(e)
1.3 Catching multiple exceptions
Written in the form of tuples.
Basic syntax:
try: error code may occur except (exception, exception,...) as e: Code to be executed if an exception occurs
Code example:
try: 1/0 except (NameError,ZeroDivisionError) as e: print(e)
1.4 Catch all exceptions
Basic syntax:
try: error code may occur except Exception as e: Code to be executed if an exception occurs
Code example:
try: 1/0 print(name) except Exception as e: print(e)
1.5 exception else and finally syntax
1.5.1 abnormal else
else indicates that there is no code to be executed when an exception occurs.
Basic syntax:
try: error code may occur except Exception as e: Code to be executed if an exception occurs else: No code executed abnormally
Code example:
try: 1/0 except Exception as e: print(e) else: print("No exception")
1.5.2 Exception finally
finally represents code that must be executed regardless of whether an exception occurs.
Basic syntax:
try: error code may occur except Exception as e: Code to be executed if an exception occurs else: No code executed abnormally finally: Code that needs to be executed no matter what
Code example:
try: 1/0 except Exception as e: print(e) else: print("No exception") finally: print("Execute no matter what")
1.6 Exception transitivity
When an exception occurs in function func01 and the exception is not caught and handled, the exception will be passed to function func02. When func02 does not catch and handle the exception, the main function will catch the exception. This is the transitivity of the exception.
When all functions do not catch exceptions, the program will report an error.
Code example:
def func01(): 0/1 def fun02(): func01() def main(): fun02() main()
2. Module
A module is a Python file that can define functions, classes, and variables. The module can also contain executable code.
The role of modules: There are many different modules in Python. Each module can help us quickly implement some functions. For example, to implement time-related functions, we can use the time module. We can think of a module as a toolkit. There are various tools in a toolkit for us to use to achieve various functions.
2.1 module import method
2.1.1 import module name
Import a module.
Basic syntax:
import module name import module name 1, module name 2 module name.function name()
Code example:
# Import time module import time # Let the program sleep for 5 seconds time.sleep(5)
2.1.2 from module name import classes, variables, methods, etc.
Import the specified functionality of a module.
Basic syntax:
from module name import function name function name()
Code example:
# Import time module sleep from time import sleep # Let the program sleep for 5 seconds sleep(5)
2.1.3 from module name import *
All methods of the imported module.
Basic syntax:
from module name import * function name()
Code example:
# Import all methods of the module from time import * # Let the program sleep for 5 seconds sleep(5)
2.1.4 import module name as alias
Set an alias after importing the module.
Basic syntax:
import module name as alias alias.function()
Code example:
# Import the time module and give it an alias of t import time as t # Let the program sleep for 5 seconds t.sleep(5)
2.1.5 for module name import function name as alias
Specify the function of the imported module and take an alias.
Basic syntax:
from module name import function as alias alias()
Code example:
# Import the sleep function of the time module, and give it the alias s from time import sleep as s # Let the program sleep for 5 seconds s(5)
2.2 Customize modules and import
Python has helped us implement many modules. But sometimes we need some personalized modules, which can be achieved through custom modules, that is, making a module yourself.
Create a new Python file, name it my_module1.py, and define the test function.
__main__ and __all__ in modules
__main__ can be written in a custom module for testing, and the testing part will not affect the part that calls this module.
def add(a, b): print(a + b) if __name__ == '__main__': add(1, 2)
When there is an all variable in a module, using from module name import *
will only import the functions in the all list.
all('add1') def add1(a, b): print(a + b) def add2(a, b): print(a + b) if __name__ == '__main__': add1(1, 2)
Note:
- Each Python file can be used as a module, and the name of the module is the name of the file. In other words, the custom module name must comply with the identifier naming rules.
- When importing functions with the same name from different modules, the one imported later will overwrite the one imported first.
3. Package
3.1python package
- Physically, a package is a folder, which contains an __init__.py file (this file is a python package). This folder can be used to contain multiple module files.
- Logically, the essence of a package is still a module.
The role of packages: When we have more and more module files, packages can help us manage these modules.
Create a new package in pycharm: right-click the folder->New->Python Package
Three basic syntaxes for importing packages:
import package name.module name Package name.Module name.Function() from package name import module name modulename.function() from package name.module name import function Function()
3.2 Install third-party packages
3.2.1 cmd to install third-party packages
Open the command line and enter pip install package name
to install the third-party package.
pip install package name
Since pip is connected to foreign websites to download packages, sometimes the speed will be very slow.
You can use the following command to connect to a domestic website to install the package:
pip install -i website link package name pip install -i http://mirrors.aliyun.com/pypi/simple/ package name The following are links to some domestic package installation websites: Alibaba Cloud http://mirrors.aliyun.com/pypi/simple/ University of Science and Technology of China https://pypi.mirrors.ustc.edu.cn/simple/ Douban http://pypi.douban.com/simple/ Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple/ University of Science and Technology of China http://pypi.mirrors.ustc.edu.cn/simple/
3.2.2 pycharm installs third-party packages
Open pycharm and see the lower right corner.
Click python->Select Interpreter Settings….
After entering the page, you can see the installed packages. Select the + sign to open the interface for installing third-party packages.
The interface allows you to search, select the package version, and use domestic links to download and install.