Python is a multifunctional, high-quality and dynamic programming language. It offers many built-in modules and functions to perform different types of tasks. In addition, we can make our own modules with Python. The module is similar to a library in Java, C, C++ and C#. A module is usually a file with functions and instructions. The functions and positions of the modules offer a certain functionality. The Python module is saved with the .py extension. In this article we will learn how to make our own Python modules.

The module is generally used to separate large functions into small manageable files. We can implement our most commonly used functions in a separate module, and later we can call it up and use it anywhere. Creating a module promotes reuse and saves a lot of time.

Python Module Creation

To create a Python module, open the Python script, write some instructions and functions and save them with the .py extension. Later we will be able to call up these modules and use them throughout our program.

Let’s create a new module called Mathematical Operations. This module contains addition, subtraction, multiplication and division functions.

#Creation of the mathematical evaluation module

# The module provides addition, subtraction, multiplication and division functions.

# All functions take two digits as an argument

#Extra function

Standard addition (point 1, point 2) :

Return number 1+num2

#Subtraction function

Deduction of defects (number 1, number 2) :

Return number 1-num2

#creative multiplication function

Error multiplication (number 1, number 2) :

Return number1*number2

#Splatter function

Def department (number 1, number 2) :

Return number 1/num2

We can now access this module from anywhere with the import order, and we can also use these functions to perform related tasks. It is not necessary to write code over and over again to perform additions, subtractions, multiplications and divisions.

Call your module

Let’s call this module in our other Python script with the import command. See this article (https://linuxhint.com/python_import_command/) for more information about the Python import command.

The importance of mathematics

# Call the Add function of the MathOperation module.

# the function is called using the name of the module

printing (amount:,MathOperation.add(10,4)).

#Subtraction function

Print (difference: MathOperation.subtraction(100.34)).

# calling the multiplication function

print(Multiplication:, MathOperation.multiplication(4,3)).

#Mission call function

print(Result of the department:,MathOperation.division(200,5)).

Get off.

Calling variables from module

We can also declare variables in our own Python modules, assign values to these variables and invoke them in our Python script. The modules may also contain dictionaries, lists, etc.

#Main variables

Number 1 = 10

Number 2 = 20

#Make a list of students

Student? [John, Mark, Taylor, David]

#Creating a dictionary for the students #

std_dict = {name : Ali, 12 years old, e-mail [email protected]}

Now call variables and objects in another script in Python.

#Import modules

The importance of mathematics

#Name of variable num1

expression (value num1:,MathOperation.num1)

#Name of variable num2

expression (value num1:, MathOperation.num2)

# Call up the student list

print (value num1:, MathOperation.students)

# Calling up entries in the student list

print(MathOperation.student [0])

print(MathOperation.student[1])

print(MathOperation.student[2])

print(MathOperation.student[3])

# Printing the student dictionary

print(MathOperation.std_dict)

~ calling a student dictionary ~

print(MathOperation.std_dict[‘Name’]).

print(MathOperation.std_dict[‘Age’]).

print(MathOperation.std_dict[‘E-Mail’]).

Get off.

The results show that we were able to access the variables and functions of the MathOperation module.

List of all functions and variables of module.

Python offers a built-in dir() function that lists the names of all functions and variables that exist in a particular module. With the dir() function we display the names of the functions and variables of the MathOperation module.

These are our functions and variables created in our MathOperation module.

#Creation of the mathematical evaluation module

# The module provides addition, subtraction, multiplication and division functions.

# All functions take two digits as an argument

#Extra function

Standard addition (point 1, point 2) :

Return number 1+num2

#Subtraction function

Deduction of defects (number 1, number 2) :

Return number 1-num2

#creative multiplication function

Error multiplication (number 1, number 2) :

Return number1*number2

#Splatter function

Def department (number 1, number 2) :

Return number 1/num2

#Main variables

Number 1 = 10

Number 2 = 20

#Make a list of students

Student? [John, Mark, Taylor, David]

#Creating a dictionary for the students #

std_dict = {name : Ali, 12 years old, e-mail [email protected]}

Now let’s call the dir() function in our script under Python.

#Import modules

The importance of mathematics

# using the dir() function

print (dir (MathOperation))

Get off.

Conclusion

Although Python offers many built-in modules and functions to perform specific tasks, we can also create our own Python modules. The Python module contains functions and variables. Python modules are stored with the .py extension. This article uses simple examples to describe how to create your own Python modules.

how to create environment in linux,virtual environment in linux mint,setting up python on ubuntu,python on unix,virtualenvwrapper,set python environment variable in ubuntu,python3 setup.py install,python install module without pip,python install package locally,pip install os,where are python packages installed linux,python default distribution ide is called,get pip py,pip install invalid syntax,pip install python 3 mac,pip install local package,no module named pip,python packages list,compile python to binary linux,python import rpm,pyp2rpm,gui2exe,py_modules,how to create setup file for python script,1.Generally linux has drivers for everything known to man, but becuase of proprietary interference or hardware is not used anymore, you may still be able to …2.Message of the day if the screen you see when you log into the command line. Usually if s short and mundane, You may want to jazz it up a little bit with …3.After compiling a program that is a replacementment/upgrade version to existing software you might get a library conflict. That happened to me . Had a …,1.Generally linux has drivers for everything known to man, but becuase of proprietary interference or hardware is not used anymore, you may still be able to …,2.Message of the day if the screen you see when you log into the command line. Usually if s short and mundane, You may want to jazz it up a little bit with …,3.After compiling a program that is a replacementment/upgrade version to existing software you might get a library conflict. That happened to me . Had a …,virtualenv,create virtual environment python,python virtual environment,python venv,how to create a python project in linux,python package download

You May Also Like

The Easy Way to Send a Fax from Home

A fax machine is an excellent device for the secure transmission of…

10+ Best Free Sports Streaming Sites in 2020

Almost all providers of cable and satellite television offer sports programmes, but…

Vivo V20 and V20 Pro: Gorgeous Phones, but which one to go for?

Vivo Malaysia announced its new line of mid-market smartphones. This is the…

Fix: Front camera not working on Android

The biggest breakthrough in the history of smartphones is the addition of…