← Back to Chapters

Python Modules

? Python Modules

? Quick Overview

A module in Python is simply a file that contains Python code such as functions, classes, and variables. Modules help you:

  • Organize code into logical, manageable files.
  • Reuse code across multiple programs.
  • Share functionality with others in a clean way.

? Key Concepts

  • Module: A .py file containing Python code (functions, classes, variables).
  • Built-in modules: Provided by Python (e.g., math, random).
  • Importing modules: Use the import statement to bring in functionality.
  • Import specific items: Import only the functions or variables you need.
  • Aliases: Use as to give a shorter name to a module.
  • User-defined modules: Your own Python files that you can import into other programs.

? Syntax & Theory

Common ways to import and use modules in Python:

  • Import the whole module:
    import module_name
    Use with prefix, e.g., module_name.function().
  • Import specific names:
    from module_name import name1, name2
    Use directly, e.g., name1().
  • Import with alias:
    import module_name as alias
    Use shorter name, e.g., alias.function().
  • User-defined module:
    Save your functions in mymodule.py and then do import mymodule in another file.

? Code Examples

? Importing a Built-in Module

? View Code Example
# Importing the math module
import math

print(math.sqrt(16))  # Output: 4.0

? Importing Specific Functions

? View Code Example
from math import pi, pow

print(pi)        # Output: 3.141592653589793
print(pow(2, 3)) # Output: 8.0

✏️ Importing with an Alias

? View Code Example
import math as m

print(m.sqrt(25))  # Output: 5.0

? Creating and Using Your Own Module

Save your reusable functions in one file and import them into another file.

? View Code Example
# mymodule.py
def greet(name):
    return f"Hello, {name}!"

# main.py
import mymodule

print(mymodule.greet("Alice"))  # Output: Hello, Alice!

? Output & Explanation

? What the code prints

  • Basic import: math.sqrt(16) returns 4.0 using the square root function from the math module.
  • Specific imports: pi is a constant for π, and pow(2, 3) computes 2 to the power of 3, giving 8.0.
  • Alias: import math as m lets you call m.sqrt(25) instead of math.sqrt(25).
  • User-defined module: The function greet is defined in mymodule.py, imported in main.py, and prints Hello, Alice!.

? Tips & Best Practices

  • Organize related functions into modules for better structure and reusability.
  • Use aliases to shorten long module names (e.g., import numpy as np).
  • Use from module import function when you only need a few specific functions.
  • Avoid from module import * in larger projects, as it can clutter the namespace.

? Try It Yourself

  • Create a module with multiple mathematical functions (e.g., add, subtract, multiply) and import them in another file.
  • Use aliasing for long module names like numpy or pandas and observe how it simplifies your code.
  • Experiment with from module import function vs import module and see how the function calls differ.
  • Create your own mymodule.py with a few helper functions and reuse it across different small projects.