Python Modules are the libraries that includes a set of functions, variables etc. that are defined earlier. In other words, they are the files that includes ready to use functions, variables etc. There are well known Python modules while you can also create your own module. With module architecture, you do not need to write every function times and times. You can create one time and then you can use the module that this python function resides.
There are widely used modules in python. We can say these modules well-known modules. There are a lot of modules like this . Some of these well-known modules are given below:
The extension of module files is “.py”. Both the well-known modules and newly created modules have this extension.
So, how can we use an existing well known module? How can we create a new module in Python? How can we use just created module in python? In this lesson, we will focus on these questions.
You can also Download Python Cheat Sheet!
Table of Contents
To use the functions or variables in a well-known python module or a newly created module, firstly we should import the module that the function resides in. We use “import” keyword with the name of the module to add a module into the code.
For example, if we would like to the functions in math module, we use the below line:
You can also check Python Math Functions
You can also watch the video of this python lesson!
If we need only specific parts of a module, we can define it in the code. When we define this, the other parts of the module is not included. We can do this with “from” keyword.
Here, we get only sqrt function from the math module of python.
To create a module, the only thing we need to do is defining functions and then save it with “.py” extension. After that our module is ready to use. To use this module, we need to put this module in the same directory with the code file in which we will use this module. Or we an put the created module to the lib directory of Python (This is “C:\Users\asus\anaconda3\Lib” in my PC).
Let’s show this with an example. In the below example, firstly we will create a python module that includes two functions. After that we will use them in another coding example. To do this, we will import this module
We will record this file as “ourmodule.py”. Then, we will use this name in our other code file.
The output of this code will be:
We can also define different variables in a Python Module. And after that when we import this file to other file, we can use these variables easily. Modules is a very efficient mechanism also for such usages.
In the below example, we will define a list in a module and then use it with import keyword.
We will record this file as characters.py. And when we need to use them, we will add this moduel with import keyword.
The output will be:
As it is also defined in this new code file.
We can define aliases to the modules. To do this we use “as” keyword after import keyword and the name of the module.
We can use Python dir() function to list the functions in a Python module. Let’s use this with math module and list all the functions listed in this module.
The output will be a list of the defined funtions in math module.
This is basically module part of python course.
Leave a Reply