Modules in Python
When we write a program in Python Interpreter or Python Shell and then exit from Shell, all definitions that we have included in our program get lost. We can’t use those definitions again. While practicing and learning Python, it may not seem as much of a problem, but in certain cases, for example, while working on a project that deals with various long programs, it’s better to just use a text editor and create scripts of py extension.
Looking for Python Data Science Course All-in-1 Combo Training? Enroll now!
Now, if we are using the same function in different programs, we won’t have to define it again and again. We can just create a script containing that function and import that script in every program that makes use of that function. These scripts are called modules in Python.
In this module, we will learn about Python modules and cover the following topics:
What Are Python Modules?
Before we answer the question “What is module in Python?”, let’s look into a practical example that will help us understand the use of modules in Python.
Say, Leon, a software developer was given a task to develop one software. Leon worked very hard for 4 days on a Python project. And finally, after completing the project, he realized that there are some issues with the coding. So now, he needs to do debugging.
Since he wrote the whole code in one file, he is now stressed out with the fact that the code file has become very big, and making changes in that file messes up everything.
To avoid this kind of scenario, Leon decides to divide the project into different parts based on some features or components. Thus, he finishes debugging without messing up the rest of the codes by performing it on a particular part of the code, one at a time.
Similarly, in Python, modules are used to divide the code into smaller parts. In this, we can group similar data which makes the program easier to understand. The module is a simple Python file which can contain (Python functions, python variables, python classes) . Modules are processed with two new statements and one important built-in function which are:
- Import: Lets a client obtain a module as a whole
- From: Permits a client to fetch particular names from a module
- Reload: Gives a way to reload a code of module without stopping Python
Why Do We Use Modules in Python?
- Code reusability
- System namespace partitioning
- Implementing shared services or data
Enrol yourself in Online Python Training in Sydney and give a head-start to your career in Python Programming!
Import Module in Python
To import module in Python, the import keyword is used.
import <file-name1, file-name2….file-namen>
print “Hello intellipaat”
We can save this file using py extension. We have saved the above script under the name hello.py. After saving this file, we can import it using the import keyword as shown below:
- To import a particular attribute from a module, from…import is used.
from module-name import atr1,atr2, …atrn
- To import the whole module, use following syntax.
from module-name import *
Built-in Modules in Python
||Returns the next integer number of a given number
||Returns the square root of a given number
||Returns the natural logarithm e raised to a given number
||Returns the previous integer number of a given number
||Returns the natural logarithm of a number
||Returns base raised to the exp power
||Returns sine of a given radian
||Returns cosine of a given radian
||Returns tangent of a given radian
With this, we come to the end of this module in Python Tutorial. Here, we answered the question “What is a python module”, also talked about why we need Python modules and the use of modules, and at the end, we highlighted some built-in modules in Python. Now, if you want to know why Python is used for Data Science, you can go through this blog on Python for Data Science.
Further, check out our offers for Python Course and also refer to the trending Python interview questions prepared by the industry experts.