×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python import Keyword

By IncludeHelp Last updated : December 07, 2024

Description and Usage

The import is a keyword (case-sensitive) in python, it is used in import statement to import module in the program.

Syntax

Syntax of import keyword:

import module_name

Sample Input/Output

import math
# this statement will import
# math module in the program

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!

Load comments ↻


Advertisement
Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.