Modules in Python
When we write a program in Python interpreter or Python shell and then exit from the shell, all the definitions that we had 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. Now if you are using the same function in different programs, you won’t have to define it again and again. You 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 modules and cover the following topics:
- What is a Python Module?
- Why do we use modules in Python?
- Importing a Module
- Built-in Modules
What is a Python module?
Before we start the discussion about Python modules, let me give you one practical example which will help you 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 will mess up everything.
To avoid this kind of scenarios, Leon decided to divide the project into different parts based on some features or components. Then he finished debugging without messing up the rest of the codes by performing debugging on a particular part of code one at a time.
Similarly, in Python, Modules are used to divide the code into smaller parts. In this, you can group similar data which makes the program easier to understand the code. The module is a simple Python file which can contain function, variables, classes, etc. 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 clients 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
Importing a Module
To import a module, import keyword is used.
print “Hello intellipaat”
save this file using py extension. We have saved the above script under the name hello.py. After saving this file you can import this file using import keyword as shown below.
- from…import is used to import a particular attribute from a module.
- To import whole module use following syntax
|ceil(n)||Returns the next integer number of the given number|
|sqrt(n)||Returns the Square root of the given number.|
|exp(n)||Returns the natural logarithm e raised to the given number|
|floor(n)||Returns the previous integer number of the given number.|
|log(n,baseto)||Returns the natural logarithm of the number.|
|pow(baseto, exp)||Returns base to raised to the exp power.|
|sin(n)||Returns sine of the given radian.|
|cos(n)||Returns cosine of the given radian.|
|tan(n)||Returns tangent of the given radian.|
Now that we have learned about the modules in Python, let us discuss some Python regular expressions as well.