A Python program can invoke a basic set of functions (that is, built-in functions), such as print (), input (), and Len (). Python itself also contains a set of modules (the standard library). Each module is a Python program and contains a set of related functions that can be embedded into your program, for example, the math module contains functions related to mathematical operations, the random module contains functions that are related to stochastic numbers, and so on.
First, import statements
Before you start using a function in a module, you must import the module with an import statement.
Grammar:
import module1[, module2[,... Modulen]]
Instance:
1. Use the Random module ranint () function:
# printrandom.py Import Random for in range (5): Print(Random.randint (1, ten))# Result: 53649
Note: Because the Randint () function belongs to the random module, you must precede the function name with random, telling Python to look for this function in the random module.
2. Import multiple modules:
import Math, sys, random, OS
Second, from import statement
This is another form of the import module, which uses this form of import statement to invoke functions in the module without modulename . prefix. However, using the full name makes the code more readable, so it is best to use the normal form of the import statement.
Grammar:
import name1[, name2[, ... namen]]|*
Instance:
Import all functions under the random module:
from Import * for in range (5): print# There is no need for random. Prefix
Import the Randint under the random module, the random function:
from Import Randint, random
Python Import and from import use