Home »
Python
import keyword with example in Python
Python import keyword: Here, we are going to learn about the import keyword with example.
Submitted by IncludeHelp, on April 15, 2019
Python import keyword
import is a keyword (case-sensitive) in python, it is used in import statement to import module in the program.
Syntax of import keyword
import module_name
Example:
import math
# this statement will import
# math module in the program
Python examples of import keyword
Example 1: import math module and call factorial function to calculate the factorial of a given number.
# 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.
# 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