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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.