Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

Generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python create module

What is module?

Look when we wrote a complex and huge code sometimes we forget which function we wrote for which work.

Let's say that we are working on a big project and there we have to use the max, min, and average functions a lot of times. Now if we write code each time for a min, max, and average function then it will take our time and will also make the code so complex because if we want to update or change anything on the code then it will be very difficult for us and suppose a new developer comes in your position and tries to continue the code then it will be very difficult for him also.

Now what can be the solution?
The solution is if we write a function or class for a min, max, or average in a different file, and in the main file if we just use the function or class name then it will be very helpful.
Suppose we created a function for the max function in a different file. To use that max function we are just writing the function name in the main file. So the module is same. The code is written in another file and we just use the function or class name to use that code in the main file.

Let's create a module

Suppose we have to write code for the addition, subtraction, and multiplication of two numbers. So what we will do is that we will just take two python files. Suppose one is main.py and the other is module.py. Now the main.py file is our main file and module.py is that file where we will write code to calculate addition, multiplication, and subtraction.

Example:Example code for add, sub and multi in module.py file:
Input
import statistics

def add(a,b):
   return a+b

def multi(a,b):
   return a*b

def sub(a,b):
   return a-b

Output

To use that code written in the module.py file, we have to import that file into the main file

To import write:
import file_name

In this case:
import module

Example:Example main code file where will we use module:
Input
import module

a=9
b=1
c=module.add(a,b)
d=module.sub(a,b)
e=module.multi(a,b)
print(c)
print(d)
print(e)
Output
10
8
9

look one thing. In the code(in the main.py file), before sub, add and multi-function name each time we wrote module. Suppose we don't want to write a module each time. We just want to write the function name.

To do this write like this while importing the file:
from file_name import *

In this case:
from module import *

Let's see in the code:

Let's create the module file:
Input
from module import *

a=9
b=1
c=add(a,b)
d=sub(a,b)
e=multi(a,b)
print(c)
print(d)
print(e)
Output
10
8
9

Ways to import module:

let's consider the previous example.

syntax:
import file_name
example:
import module

Suppose we want to get a particular function or class or method from the module file:
To do this we will write that function or class or method name after import.
syntax:
from file_name import method_or_class_or_function_name
example:
from module import add
By doing this we are only importing add function from the module file.

Suppose you want to get all the class or methods or function.
To do this we will use asterisk sign after import.
syntax:
from file_name import *
example:
from module import *

Let's see another example:

Suppose you want to do addition of three numbers. Here the user will give input values in the form of integer. So the user will give input three values. According to the user inputted value you will the addition, subtraction and multiplication will happen and then the user will be able to see the output. So the plan is at first we will take a python file where we will have our main code and will another python file where we will create our module. So the name that file on which we will write our main code will be main.py and that file where we will create our module will be module.py file. Now let's create the program.

Example:Example code for add, sub and multi in module.py file:
Input
import statistics

def add(a,b,c):
   return a+b+c

def multi(a,b):
   return a*b*c

def sub(a,b):
   return a-b-c

Output
Let's create the main file:
Input
import module

first=int(input("Enter first number:"))
second=int(input("Enter second number:"))
third=int(input("Enter third number:")) addition=module.add(first,second,third)
subtraction=module.sub(first,second,third)
multiplication=module.multi(first,second,third)
print(addition)
print(subtraction)
print(multiplication)
Output
10
8
9

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.