Welcome back to our programming tutorial series! Today, we’ll explore how to organize and reuse your code using modules. Modules allow you to break down your programs into smaller, manageable pieces, making your code more modular, reusable, and maintainable.


What Are Modules? #

A module is simply a Python file containing code (functions, variables, classes, etc.) that you can import and use in other programs. Python comes with many built-in modules, and you can also create your own.

Here’s how to use a module:

import math

result = math.sqrt(16)
print(result)  # Outputs: 4.0

In this example, we’re importing the built-in math module and using the sqrt() function to calculate the square root of a number.


Importing Specific Functions or Variables #

You don’t always need to import the entire module. You can import specific functions or variables to save memory and keep your code cleaner.

from math import pi, sqrt

print(pi)  # Outputs: 3.141592653589793
print(sqrt(25))  # Outputs: 5.0

Creating Your Own Modules #

You can create your own modules by writing Python code in a separate file and then importing it into your main program.

  1. Create a Python file called my_module.py with the following content:
def greet(name):
    return f"Hello, {name}!"
  1. Now, in your main program, you can import and use this module:
import my_module

print(my_module.greet("Alice"))  # Outputs: Hello, Alice!

Importing with an Alias #

You can use aliases to shorten module names:

import my_module as mm

print(mm.greet("Bob"))  # Outputs: Hello, Bob!

The from Keyword: Importing Parts of a Module #

If you only need specific functions from a module, you can import them directly using the from keyword.

from my_module import greet

print(greet("Charlie"))  # Outputs: Hello, Charlie!

Using Built-In Modules #

Python comes with a variety of built-in modules that you can use in your programs. Some popular ones include:

  • os: Interact with the operating system.
  • sys: Access system-specific parameters and functions.
  • random: Generate random numbers.
  • datetime: Work with dates and times.

Here’s an example using the random module:

import random

random_number = random.randint(1, 10)
print(random_number)  # Outputs a random number between 1 and 10

Practical Exercise: Create a Utility Module #

Now that you understand how to use modules, try creating a utility module:

  1. Create a module called utilities.py with the following functions:

    • square(n): Returns the square of a number.
    • cube(n): Returns the cube of a number.
    • is_even(n): Returns True if the number is even, otherwise False.
  2. Import and use these functions in a new program.

Here’s an example:

# utilities.py
def square(n):
    return n ** 2

def cube(n):
    return n ** 3

def is_even(n):
    return n % 2 == 0

Now, import and use these functions:

from utilities import square, cube, is_even

print(square(4))  # Outputs: 16
print(cube(3))  # Outputs: 27
print(is_even(10))  # Outputs: True

What’s Next? #

You’ve just learned how to organize your code using modules and import functions efficiently. This will make your programs more modular and easier to manage as they grow. In the next post, we’ll explore lists and arrays, essential data structures for storing and managing collections of data.



Happy coding, and we’ll see you in the next lesson!