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.
Watch this Python Projects Video for Beginners:
Looking for Python for 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?
- Why Do We Use Modules in Python?
- Importing Modules in Python
- Built-in Modules in Python
What Are Python Modules?
Before we answer the question “What is module in Python?”, let’s look into a practical example which 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 that 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 scenarios, 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>
def intellipaat(): 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:
import hello hello.intellipaat() Output: Hello intellipaat
- 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
|ceil(n)||Returns the next integer number of a given number|
|sqrt(n)||Returns the square root of a given number|
|exp(n)||Returns the natural logarithm e raised to a given number|
|floor(n)||Returns the previous integer number of a given number|
|log(n,baseto)||Returns the natural logarithm of a number|
|pow(baseto, exp)||Returns base raised to the exp power|
|sin(n)||Returns sine of a given radian|
|cos(n)||Returns cosine of a given radian|
|tan(n)||Returns tangent of a given radian|
With this we come to an end of this module in Python Tutorial. Here, we answered the question “What is a python module”, also we 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.