TOPIC: Python Modules and Uses
Modules
A module in Python is a file that contains code to perform a specific task or provide some
functionality. A module can have variables, functions, classes, and other objects that can be
imported and used in other Python programs. Modules help to organize the code and make it more
readable and reusable.
There are two types of modules in Python: built-in modules and user-defined modules. Built-in modules
are those that come with the Python standard library and provide common and useful features.
For example, the math module provides mathematical functions and constants, the os module provides
operating system functions, the random module provides random number generation, and so on.
User-defined modules are those that are created by the programmers
themselves for their own
purposes. For example, one can create a module named hello.py that contains a function to greet
someone.
To use a module in Python, one has to import it using the import keyword. For example, to import the
math module, one can write:
import math
And, for an example of user-defined module, we create a file called file.py and define a function in it. The name of the function, say, greet() can be called in another python file, say app.py. To do that we write
from file import greet
The full explanation of this implementation can be seen in the YouTube Video.
Code as discussed in the video:
# Python Modules - uses
'''Definition: A python file that contains some function definitions and statements '''
# from tut11 import factorial
# output = factorial(5)
# print("the factorial is", output)
# import random
# random_num = random.randint(10, 50)
# print(random_num)
# import calendar
# print(calendar.month(2025, 12))
from turtle import*
while True:
forward(100)
left(100)
You can copy and run this code