Modules in Python are files containing Python code. This code can define functions, classes, and variables. A module is a way to structure your Python code and make it more manageable by dividing it into smaller, self-contained units.
Creating a Module
To create a module, you simply write your Python code in a file with a .py extension.
greet(name): This function takes a name and returns a greeting string.add(a, b): This function takes two numbers and returns their sum.pi: This is a constant representing the value of π.
# mymodule.py
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + b
pi = 3.14159Using a Module
To use a module, you need to import it into your Python script using the import statement.
import mymodule: This statement imports the modulemymodule.mymodule.greet("Alice"): Calls thegreetfunction frommymodulewith “Alice” as an argument.mymodule.add(5, 3): Calls theaddfunction frommymodulewith 5 and 3 as arguments.mymodule.pi: Accesses thepivariable frommymodule.
# main.py
import mymodule
# Using functions from the module
print(mymodule.greet("Alice")) # Output: Hello, Alice!
print(mymodule.add(5, 3)) # Output: 8
# Accessing variables from the module
print(mymodule.pi) # Output: 3.14159Importing Specific Items
You can import specific functions, classes, or variables from a module using the from ... import ... statement.
from mymodule import greet, pi: Imports only thegreetfunction andpivariable frommymodule.greet("Bob"): Calls thegreetfunction with “Bob” as an argument.pi: Accesses thepivariable directly.
# main.py
from mymodule import greet, pi
# Using the imported function and variable
print(greet("Bob")) # Output: Hello, Bob!
print(pi) # Output: 3.14159Using Aliases
You can assign an alias to a module or an item within a module to make it easier to reference.
import mymodule as mm: Importsmymoduleand gives it the aliasmm.from mymodule import add as sum_up: Imports theaddfunction frommymoduleand gives it the aliassum_up.mm.greet("Carol"): Calls thegreetfunction frommymoduleusing the aliasmm.sum_up(10, 5): Calls theaddfunction using the aliassum_up.
# main.py
import mymodule as mm
from mymodule import add as sum_up
# Using the module and function with aliases
print(mm.greet("Carol")) # Output: Hello, Carol!
print(sum_up(10, 5)) # Output: 15Exploring the sys Module
The sys module provides access to some variables and functions used or maintained by the Python interpreter.
import sys: Imports the built-insysmodule.sys.version: Retrieves the Python version.sys.platform: Retrieves the platform on which Python is running.
import sys
print("Python version:", sys.version)
print("Platform:", sys.platform)Python Modules Example Code
- mymodule.py: Defines the module with a greeting function, an addition function, and a constant.
- main.py: Imports the module and demonstrates using its functions and variables.
Step 1: Create a module named mymodule.py.
# mymodule.py
def greet(name):
return f"Hello, {name}!"
def add(a, b):
return a + b
pi = 3.14159Step 2: Create a script named main.py that imports and uses the module.
# main.py
import mymodule
# Using functions from the module
print(mymodule.greet("Alice")) # Output: Hello, Alice!
print(mymodule.add(5, 3)) # Output: 8
# Accessing variables from the module
print(mymodule.pi) # Output: 3.14159

