Learning Python- Intermediate course: Day 7, Making Python modules

We have learnt how to use predefined python modules. Today we will create our very own Python module. ? So put your seatbelts on for this thrilling Python journey?

Modules in Python

Creating a module in Python is a very easy ta…


This content originally appeared on DEV Community and was authored by Aatmaj

We have learnt how to use predefined python modules. Today we will create our very own Python module. ? So put your seatbelts on for this thrilling Python journey?

Modules in Python

Creating a module in Python is a very easy task. image

  • Step I Make a python file named mymodule.py (or any other name ending with .py) which contains various functions. Do this in Python IDLE or any other IDE. Here is a sample file-
def hi():
    print("hi")
def hello():
    print("hello")
def add(a,b):
    return a+b
def printer(a):
    print(a)    
  • Step II Save the python file (or copy-paste the file) in the Lib (not libs) folder of the Python source code. image

image

  • Step III In the file in which you want to use the module, write import mymodule (import the relevant module name) After that, just use the functions in the module as predefined functions. Simple !!

Here is the program which uses the functions in our module.

import mymodule
mymodule.printer("Welcome to Python intermediate course!")
print(mymodule.add(2,3))
mymodule.hello()

We now save it in a file named 'mymoduletester' and run it.
image

After we run the file, an output is generated in a command line window.
image

OUTPUT-

Python 3.10.0a2 (tags/v3.10.0a2:114ee5d, Nov  3 2020, 00:37:42) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
= RESTART: C:/Users/aatma/AppData/Local/Programs/Python/Python310/Lib/mymoduletester.py
Welcome to Python intermediate course!
5
hello

That's it! We have successfully made our own module and used it in our code!? Cool!?

I have used the IDLE for Python over here, but you are free to use any editor you want. We can have the same effect by using the online GDB compiler as shown below.
image

✌️So friends that's all for now. ? Hope you all are having fun.? Please let me know in the comment section below ?. And don't forget to like the post if you did. ? I am open to any suggestions or doubts. ? Just post in the comments below or gmail me. ?
Thank you all?

Also please visit the Learning-Python repo made especially for this course and don't forget to star it too


This content originally appeared on DEV Community and was authored by Aatmaj


Print Share Comment Cite Upload Translate Updates
APA

Aatmaj | Sciencx (2021-08-12T07:36:45+00:00) Learning Python- Intermediate course: Day 7, Making Python modules. Retrieved from https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/

MLA
" » Learning Python- Intermediate course: Day 7, Making Python modules." Aatmaj | Sciencx - Thursday August 12, 2021, https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/
HARVARD
Aatmaj | Sciencx Thursday August 12, 2021 » Learning Python- Intermediate course: Day 7, Making Python modules., viewed ,<https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/>
VANCOUVER
Aatmaj | Sciencx - » Learning Python- Intermediate course: Day 7, Making Python modules. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/
CHICAGO
" » Learning Python- Intermediate course: Day 7, Making Python modules." Aatmaj | Sciencx - Accessed . https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/
IEEE
" » Learning Python- Intermediate course: Day 7, Making Python modules." Aatmaj | Sciencx [Online]. Available: https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/. [Accessed: ]
rf:citation
» Learning Python- Intermediate course: Day 7, Making Python modules | Aatmaj | Sciencx | https://www.scien.cx/2021/08/12/learning-python-intermediate-course-day-7-making-python-modules/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.