Python导入关键字 (Python import keyword)
import is a keyword (case-sensitive) in python, it is used in import statement to import module in the program.
import是python中的一个关键字(区分大小写),在import语句中用于在程序中导入模块。
Syntax of import keyword
import关键字的语法
import module_name
Example:
例:
import math
# this statement will import
# math module in the program
导入关键字的Python示例 (Python examples of import keyword)
Example 1: import math module and call factorial function to calculate the factorial of a given number.
示例1:导入数学模块并调用阶乘函数以计算给定数字的阶乘。
# python code to demonstrate an example
# of import keyword
# import statement
import math
# number
num = 5
# finding factorial
result = math.factorial(num)
# printing factorial
print("Factorial of ", num, " is = ", result)
Output
输出量
Factorial of 5 is = 120
Example 2: import datetime module and call its functions to get the current day, month and year.
示例2:导入datetime模块并调用其函数以获取当前的日期,月份和年份。
# python code to demonstrate an example
# of import keyword
# import statement
import datetime
# creating date object
dt = datetime.datetime.now()
# printing day, month and year
print("Current day : ", dt.day)
print("Current month: ", dt.month)
print("Current year : ", dt.year)
Output
输出量
Current day : 15
Current month: 4
Current year : 2019
翻译自: https://www.includehelp.com/python/import-keyword-with-example.aspx