0
0
Pythonprogramming~5 mins

Import aliasing in Python

Choose your learning style9 modes available
Introduction

Import aliasing lets you use a shorter or easier name for a module or function you bring into your program. This makes your code cleaner and faster to write.

When a module name is very long and you want to type less.
When two modules have the same name and you want to avoid confusion.
When you want to give a module or function a name that fits your program better.
When you want to keep your code neat and easy to read.
Syntax
Python
import module_name as alias_name

# or for specific functions
from module_name import function_name as alias_name

You can rename whole modules or just specific functions.

The alias name is what you use in your code after importing.

Examples
Here, the long module name numpy is shortened to np for easier use.
Python
import numpy as np

# Now you can use np instead of numpy
array = np.array([1, 2, 3])
The function sqrt is renamed to square_root to make its purpose clearer.
Python
from math import sqrt as square_root

result = square_root(16)
Sample Program

This program uses math module with alias m to calculate the area of a circle.

Python
import math as m

radius = 5
area = m.pi * (radius ** 2)
print(f"Area of circle with radius {radius} is {area}")
OutputSuccess
Important Notes

Alias names should be easy to remember and not conflict with other names in your code.

Using aliasing can make your code shorter but always keep it clear for others reading your code.

Summary

Import aliasing helps you use shorter or clearer names for modules or functions.

You can alias whole modules or just parts of them.

This makes your code easier to write and read.