Python Tutorial Python Advanced Python References Python Libraries

Python as Keyword



The Python as keyword is used to create an alias while importing a module in the current session. Creating an alias means giving a user-defined name to the module while importing it. After creating an alias, the object of the module can be used in the current session with the user-defined name followed by a dot (.) and object name.

Please note that the modules which need to be imported must be installed on the local computer or a remote computer.

Syntax

#import module as alias name
import ModuleName as NewName

#use object of a module
NewName.ObjectName(parameters)

Example:

In the example below, the math module is imported in the current session with user-defined name called m. The math module contains an object called pi which is used in the current session to calculate the area of a circle.

import math as ma

radius = 1
pi = ma.pi
area = pi*(radius)**2

print(area)

The output of the above code will be:

3.141592653589793

❮ Python Keywords