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:
1import math
2
3result = math.sqrt(16)
4print(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.
1from math import pi, sqrt
2
3print(pi) # Outputs: 3.141592653589793
4print(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.
- Create a Python file called
my_module.py
with the following content:
1def greet(name):
2 return f"Hello, {name}!"
- Now, in your main program, you can import and use this module:
1import my_module
2
3print(my_module.greet("Alice")) # Outputs: Hello, Alice!
Importing with an Alias #
You can use aliases to shorten module names:
1import my_module as mm
2
3print(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.
1from my_module import greet
2
3print(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:
1import random
2
3random_number = random.randint(1, 10)
4print(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:
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)
: ReturnsTrue
if the number is even, otherwiseFalse
.
Import and use these functions in a new program.
Here’s an example:
1# utilities.py
2def square(n):
3 return n ** 2
4
5def cube(n):
6 return n ** 3
7
8def is_even(n):
9 return n % 2 == 0
Now, import and use these functions:
1from utilities import square, cube, is_even
2
3print(square(4)) # Outputs: 16
4print(cube(3)) # Outputs: 27
5print(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.
Related Articles #
- Advanced Functions: Default Arguments, Lambda Functions, and Scope
- Control Structures: Mastering Program Flow
- Lists and Arrays: Storing Collections of Data
Happy coding, and we’ll see you in the next lesson!