DEV Community

importostar
importostar

Posted on

import keyword with example in Python

Python import keyword: so you can know about the import keyword with example.

Its used to get modules and a module is like a building block: it has many functions available. You can use pip to install modules.

Python import keyword

import is a python keyword used to import module in a script

Syntax of import keyword

    import module_name

Python Example:

    import math
    # this will load the math module    

Python examples of import keyword

Import the math module in this script and call a function

import math
num = 5

# call function in module
result = math.factorial(num)

# print output
print("The factorial of ", num, " is = ", result)

The program above outputs this:

The factorial of  5  is =  120

Example 2:

Module load datetime and name the present day, month and year

import datetime

# create date object
dt = datetime.datetime.now()

# get day, month and year
print("day  : ", dt.day)
print("month: ", dt.month)
print("year : ", dt.year)

The program above outputs this:

day  :  27
month:  7
year :  2020

You can use modules to add all kinds of functionality to your program. Using modules could create a canvas, gui with pyqt, bmi calculator, web app etc. You can create modules if you want.

Top comments (0)