In my python series till now we have practiced and understood the basic concept of how to get started to use python development. In my last series, we have completed the concept of variables, data types, conditional control and functional control. If you are serious to learn all those concepts then I would be recommended to please check all post about the basic of python in python category. Here is the link to python Category.
In this python series, we will learn about the python module and packages. Modules actually refer to a file containing Python statements and definitions. I mean If a file name is saved with my_module.py
then my_module
is the module of the python. So whenever we want to use this file function, statement or definition we can use by import
module statement.
In simple word, We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code. We can define our most used functions in a module and import it, instead of copying their definitions into different programs.
Let’s see this as an example to understand clearly. I am creating the python file name my_module.py and defined the function inside this file. Now I want to access this function to others file then I need to import this module to access this function.
my_module.py
def sayHello(): print('Hello')
Now I need to import this my_module inside the module.py which is another file.
module.py
import my_module print("Module imported") my_module.sayHello()
Output: Hello
Here Now we can use the sayHello() function inside the module.py file. Even we can modify the import module name as any specific name. Let’s see this as an example.
# or another way import my_module as m m.sayHello()
Similarly, Python is providing the internal existing API for import as a module to use in your file. Let’s see few examples of existing API (exp: MATH) to import as a module to use in the current file.
import math print("The value of pi is", math.pi) # or another word from math import pi print("The value of pi is", math.pi)
If we want to import everything from this API then we can use the * in end of the import statement.
from math import *
The similar concept of packages too. Generally, we are creating the package to make our project modular and structured. So that anybody can understand to see the structure what are the files are here and their uses. So here we can see few examples to understand how can we create the package and how can we import as a module in our current file.
Here I create the my_package.py file inside the com/sunil/python/package1.
my_package.py
def add_number(a, b): add = a + b print("Sum", add) return add
Now how can we import this into the module.py file as a module.
import com.sunil.python.package1.my_package as p p.add_number(2, 4) # or in another word from com.sunil.python.package1.my_package import * add_number(3, 8)
Output:
Sum 6
Sum 11
Now we have a good understanding of the module and packages and their uses in python development. In my next tutorial, we will learn about the Data Structure of Python. If you have any issue or doubt then please send me in the comment box, I will try to post the solution.
If you are wondering to learn Android then Please Learn from Android category and Wondering to lean Kotlin then Kotlin Category will help you. If you want to learn all the python articles, then learn from the python category.
Please do subscribe your email to get the newsletter on this blog on below and if you like this post then do not forget to share like and comment on the below section.
Happy coding 🙂
I am a very enthusiastic Android developer to build solid Android apps. I have a keen interest in developing for Android and have published apps to the Google Play Store. I always open to learning new technologies. For any help drop us a line anytime at contact@mobologicplus.com
Hi everyone, In this article, we are going to learn how to hide the production… Read More
Hello everyone, Today in this article, we are going to learn about localisation to support… Read More
Hello everyone, In this article, we are going to learn something to handle the callback… Read More
In this article, we are learning about the run time permissions for request permission launchers.… Read More
Hello everyone. In my last tutorial, we learned about the Jetpack Compose introduction and about applying the… Read More
Hello everyone, In this article, we are going to learn about the Jetpack Compose with… Read More