Python – Modules

Python Module can be thought as package in Java.  A module allows you to logically organize your Python code. Consider a module to be the same as a code library. We can store Functions, Variables, Classes in Module.

Built-In Modules

Python have built-in module which we can use according to our needs. For example – 

import platform

a = platform.system()

Create a Module

To create a module just save the code you want in a file with the file extension

def greeting():
print("Hello from module")

person = {"name":"Huzaif","age":22,"country":"India"}

Above we have created a function and dictionary variable. Save the file in the same folder.

Import Statement

The import statement help to import or use the module which we have created. Use module file name. Example : mymodule

import mymodule

mymodule.greeting("name") # Accessing Function from
print(mymodule.person["age"]) # Accessing Variable from

Renaming Module name

You can rename module name by using as keyword

import mymodule as mm
a = mm.person1["age"]

Accessing Specific Part from Module

For Accessing Specific Part from Module we use from keyword

from mymodule import person

print(person["age"]) # we have accessed only person variable
Filed under: Python

No comment yet, add your voice below!

Leave a Reply