Python Modules

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.

Explanation:

  • 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.14159




Using a Module

To use a module, you need to import it into your Python script using the import statement.

Explanation:

  • import mymodule: This statement imports the module mymodule.
  • mymodule.greet("Alice"): Calls the greet function from mymodule with “Alice” as an argument.
  • mymodule.add(5, 3): Calls the add function from mymodule with 5 and 3 as arguments.
  • mymodule.pi: Accesses the pi variable from mymodule.
# 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




Importing Specific Items

You can import specific functions, classes, or variables from a module using the from ... import ... statement.

Explanation:

  • from mymodule import greet, pi: Imports only the greet function and pi variable from mymodule.
  • greet("Bob"): Calls the greet function with “Bob” as an argument.
  • pi: Accesses the pi variable directly.
# main.py

from mymodule import greet, pi

# Using the imported function and variable
print(greet("Bob"))  # Output: Hello, Bob!
print(pi)            # Output: 3.14159




Using Aliases

You can assign an alias to a module or an item within a module to make it easier to reference.

Explanation:

  • import mymodule as mm: Imports mymodule and gives it the alias mm.
  • from mymodule import add as sum_up: Imports the add function from mymodule and gives it the alias sum_up.
  • mm.greet("Carol"): Calls the greet function from mymodule using the alias mm.
  • sum_up(10, 5): Calls the add function using the alias sum_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: 15




Exploring the sys Module

The sys module provides access to some variables and functions used or maintained by the Python interpreter.

Explanation:

  • import sys: Imports the built-in sys module.
  • 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

Explanation:

  • 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.14159

Step 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




Python Labs

Scroll to Top