- Introduction to Modules
- Creating Your Own Modules
- Importing and Using Modules
- Standard Library Modules and Their Usage
- Summary
- Tasks
A module is a file containing Python definitions and statements. Modules allow you to organize your code into separate files, making it easier to manage and reuse. You can import a module into another module or script to use its functions, classes, and variables.
# Save this code in a file named mymodule.py
def greet(name):
return f"Hello, {name}!"
# Save this code in another file
import mymodule
print(mymodule.greet("Alice"))
Output:
Hello, Alice!
Explanation: The mymodule.py
file contains a function greet
. We import the mymodule
in another file and use the greet
function.
You can create your own modules by saving Python code in a file with a .py
extension. The file name becomes the module name.
# Save this code in a file named mymodule.py
def factorial(x):
result = 1
for i in range(1, x + 1):
result *= i
return result
pi = 3.1415
Explanation: The mymodule.py
file contains a function factorial
and a variable pi
. This file can be imported as a module in other scripts.
You can import a module using the import
statement. You can also use from ... import ...
to import specific functions or variables from a module.
# Save this code in a file named main.py
import mymodule
print(mymodule.factorial(5))
print(mymodule.pi)
Output:
120
3.1415
Explanation: We import the mymodule
and use its factorial
function and pi
variable.
# Save this code in a file named main.py
from mymodule import factorial, pi
print(factorial(4))
print(pi)
Output:
24
3.1415
Explanation: We import the factorial
function and pi
variable directly from the mymodule
.
# Save this code in a file named main.py
from mymodule import *
print(factorial(6))
print(pi)
Output:
720
3.1415
Explanation: We import all functions and variables from the mymodule
using the *
wildcard.
Python comes with a standard library of modules that provide various functionalities. You can import and use these modules in your scripts.
import math
print(math.pi)
print(math.e)
print(math.sqrt(16))
print(math.factorial(5))
Output:
3.141592653589793
2.718281828459045
4.0
120
Explanation: The math
module provides mathematical functions and constants. We use the pi
and e
constants, and the sqrt
and factorial
functions.
import random
print(random.randint(1, 10))
print(random.choice(['apple', 'banana', 'cherry']))
Output:
7
banana
Explanation: The random
module provides functions for generating random numbers and selecting random items from a list. We use the randint
function to generate a random integer and the choice
function to select a random item from a list.
import datetime
now = datetime.datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))
Output:
2023-10-05 14:30:00
2023-10-05 14:30:00
Explanation: The datetime
module provides functions for working with dates and times. We use the now
function to get the current date and time, and the strftime
function to format the date and time.
In this chapter, we covered modules, including creating your own modules, importing and using modules, and standard library modules and their usage.
- Create a module with a function that calculates the area of a circle.
- Create a module with a function that checks if a number is even or odd.
- Import and use the
math
module to calculate the square root of a number. - Import and use the
random
module to generate a random number between 1 and 100. - Import and use the
datetime
module to print the current date and time.