Python Tutorials
- Python
- How to install Python?
- PIP
- How to Run Python Program
- Python Identifiers,Statement, Indentation and Comments
- Variable
- Data type
- Decision Making
- Python Loops
- Break,Continue, Pass
- Functions
- Predefine Functions
- Lambda Functions
- Variable Scope
- List
- Tuple
- Python Sets
- Python Dictionary
- Python String
- String Formating
- Input/Output
- File Handling (Input / Output)
- Iterators
- Python Modules
- Python Date
- Python JSON
- Classes and Objects
- Constructor
- Polymorphism
- Encapsulation
- inheritance
- Class or Static Variables in Python
- class method vs static method
- Abstraction
- Exception Handling
- MySql Python
- MySql Create Database
- MySql CRUD
- Django
What is Python Modules?
A python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module.
A file containing Python code, for example: example.py, is called a module
We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code.
A file containing a set of functions you want to include in your application.
How to create module in python?
To create a module just save the code you want in a file with the file extension .py:
In this example, we will create a module named as file.py which contains a function func that contains a code to print some message on the console.
File name : mymodule.py
#displayMsg prints a message to the name being passed.
def displayMsg(name)
print("Hi "+name);
Here, we need to include this module into our main module to call the method displayMsg() defined in the module named file.
How to Use a Module in python?
we can use the module by using the import statement
File name : index.py
We need to load the module in our python code to use its functionality. Python provides two types of statements as defined below.
The import statement
The from-import statement
The import statement
The import statement is used to import all the functionality of one module into another.
We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times, it has been imported into our file.
import module1,module2,........ module n
Import the module named mymodule, and call the displayMsg function:
File name : index.py
import mymodule
mymodule.displayMsg("Mahira Mahtab")
Example
File name : index.py
import mymodule;
name = input("Enter the name?")
mymodule.displayMsg(name)
Variables in Module
File name : mymodule.py
employee = {
"name": "Sana",
"age": 1,
"country": "India"
}
File name : index.py
import mymodule
emp_name = mymodule.employee["name"]
print(emp_name)
Re-naming a Module
You can create an alias when you import a module, by using the as keyword:
File name : index.py
import mymodule as mx
name = mx.employee["name"]
print(name)
File name : index.py
The from-import statement
Instead of importing the whole module into the namespace, python provides the flexibility to import only the specific attributes of a module. This can be done by using from? import statement.
File name : index.py
Syntax :
from < module-name> import <name 1>, <name 2>..,<name n>
File name : calculation.py:
#place the code in the calculation.py
def summation(a,b):
return a+b
def multiplication(a,b):
return a*b;
def divide(a,b):
return a/b;
Main File
File name : Main.py
from calculation import summation
#it will import only the summation() from calculation.py
a = int(input("Enter the first number"))
b = int(input("Enter the second number"))
print("Sum = ",summation(a,b)) #we do not need to specify the module name while accessing summation()
File name : index.py
The from...import statement is always better to use if we know the attributes to be imported from the module in advance. It doesn't let our code to be heavier. We can also import all the attributes from a module by using *.
Consider the following syntax.
from <module> import *
Renaming a module
Python provides us the flexibility to import some module with a specific name so that we can use this name to use that module in our python source file.
The syntax to rename a module is given below.
import <module-name> as <specific-name>
File name : index.py
#the module calculation of previous example is imported in this example as cal.
import calculation as cal;
a = int(input("Enter a?"));
b = int(input("Enter b?"));
print("Sum = ",cal.summation(a,b))
dir() function
The dir() function returns a sorted list of names defined in the passed module. This list contains all the sub-modules, variables and functions defined in this module.
File name : index.py
import json
List = dir(json)
print(List)
File name : index.py
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
File name : index.py
from mymodule import person1
print (person1["age"])