Hey how are you doing?? Hope you enjoyed my last lesson if you haven’t seen it yet, do check it out here PYTHON BEGINNER’S GUIDE | WHILE LOOP
Ok, let’s
talk about out today’s lesson, we will be leaning about modules in Python. But
first let’s understand why we use modules.
Suppose, you
have a really lengthy program. Now dividing this program makes it easier to
access code and navigate, these
divisions are known as modules.
To use a
module, we must import it in the following way:
import module_name
where,
‘import’ is the keyword and ‘module_name’ is the name of the module you want to
import.
Once we
import a module, we can use its variables and functions.
Python has
some built-in modules like, ‘math’ and ‘random’. These modules offer different functions
like ‘random.randint()’ that picks a random number from a series of integers .
Some examples of these modules and their functions are giving below :
CREATING YOUR OWN MODULE:
If you are not satisfied with the modules Python has to offer, you can create your own module. All you need to is :
- Create a new Python file
- Define a
function, ‘add’ that adds two integers and prints the sum
- Now save
that file in a new folder as 'mod1'
- Now create another Python file
- Now call
the first python file as a module using the command ‘import mod1’
- Now call
the ‘add’ function by using the command ‘mod1.add(2,6)’
- Save it as 'mod2' in the same folder you created in the step 3 and run 'mod2'
- Now Python will call that function ‘add’ and pass in the parameters ‘2’ and ‘6’.
- The function will return the sum of the two numbers.
USING PYTHON PIP:
You can use Python's pip application to work with third-party modules. By third-party modules I mean, modules that do not come pre-installed in Python and must be installed manually.
INSTALLING MODULES:
You can install modules in Windows it by opening cmd and executing the following command while connected to an internet connection:
python pip install <module_name>
if this
doesn’t work, and shows an error like ‘Couldn’t find ‘pip’ application’, try:
python –m pip install <module_name>
If you are
using Python 2, use:
python2 pip install <module_name>
or
python2 –m pip install <module_name>
VIEWING INSTALLED MODULES:
You can also see the list of modules you already have installed in your system by using:
python pip list
or
python -m pip list
or
python2 pip list
or
python2 -m pip list
UNINSTALLING MODULES:
You can also uninstall a module by executing the following command:
python pip uninstall <module_name>
or
python –m pip uninstall <module_name>
or
python2 pip uninstall <module_name>
or
python2 –m pip uninstall <module_name>
CONGRATULATIONS !!! You have learnt about modules in Python, how to create modules of your own and how to use python pip to work with third-party modules. Now you know how to use Python to execute a code from another program in the program you are currently working in. Trust me this knowledge will help you create programs that are a lot easier to understand and execute in a simpler way. Have fun with it !!! Create some cool programs. Good luck !!!!
This is the last beginner's lesson, if you are reading this and know all the topics covered before this. CONGRATULATIONS again because I personally promote you to the intermediate level. The lessons that we will be covering from next week will be focused on the intermediate level programmers. So I would recommend using this week to revise all the topics we have discussed before so that you are prepared for the next lessons.
I hope this article answered all of your questions and even helped you in becoming a better programmer. IF IT DID, leave a like AND FOLLOW THIS BLOG TO BECOME A PROFESSIONAL PYTHON PROGRAMMER FROM A TOTAL BEGINNER. IF IT DIDN'T feel free to ask any further queries in the comment section below.
2 Comments
Thanks for this post. This was really helpful.
ReplyDeleteI am really glad this helped you :)
DeleteWelcome to the comments section, this is where you can contact me personally for any doubts or feedback