Hello Students welcome to our new blog. In this blog i am going to tell you about python module. Here we will Create python modules and import in python. So must read the full blog. In the middle of the article you will get the video link of live class.
Introduction
Every program that we create Python is treated as a module. It logically organizes Python functions in a single program file Grouping related code into a module makes the code easier to understand and use. In oth words a module can be defined as – “A module is a group of functions, classes and variable saved as a python program file (.py)”.
In the Python programming that we have learnt so far we have used two readymade Python modules (Math and Random) and the function defines in them. Now, in this blog we will learn to create your own python module or custom python module which we can import in our any program later when neede User defined modules can be A module can be defined as a file consisting of Python code created in the following three steps-
- Creating Module
- Importing Module
- Calling Module Object
1) Creating python modules
As defined earlier a module is just a Python’s program file consisting of functions, so, to create a new module open a new Python program file and define function(s) in it. Python module generally does not have global environment i.e. it does not need any main program. It can have only function definitions and no calling of function. But a module can have more than one function,
For example, type the following functions in a python program file and save it in the default Python directory by the name ‘mymod.py’. This program file will become a Python module created with the name ‘mymod’ having two function namely tarea() and carea().
def tarea(b, h):
ar=(b*h)/2
print(“Area of Triangle: “, ar)
def carea(r):
ar=3.14*r*r
print(“Area of Circle:”, ar)
Watch Our Full Video on Youtube
2) Importing Module
Once a module is created and saved it can be used in any program, any number time because it has become a permanent record. We can use our Python module by executing an import statement in some other Python source file. The ‘import’ has the following syntax:
Syntax
import module 1, module2,…
Example
import mymod
Python has few other ways to import a module in a program and use its function there. The first is Python’s ‘from…import’ statement. It lets us import specific function from a module into the current program file. The functions imported in this way can be called directly by their name without module name.
Syntax
from module_name import fname 1, fname2,…
Example
from mymod import tarea
tarea(4,3)
Python user defined functions with examples(2024)
3) Calling Module Objects
User defined module helps re-usability of codes defined in it as functions. So, once the module is created and functions defined we can call functions as follows-
Syntax
<module name>.<funct_name>(<list of values>)
mymod.tarea(5, 11)
Subscribe Our Youtube Channel By click on the link below
https://www.youtube.com/@olevelguruji
Python Module Example
Create a python module mymod.py which contain two functions namelu tarea and carea after that call this module from the other program
mymod.py
def tarea(b,h):
ar=(b*h)/2
print(“Area of triangle=”,ar)
def carea(r):
ar=3.14*r*r
print(“Area of circle=”,ar)
test.py
Note- In this file we will import our own create python module
from area import *
tarea(5,10)
carea(5)
Output
Area of triangle= 25.0
Area of circle= 78.5
Also Check Our Latest Uploads
Python library function examples