Computer Science
Grade 7
20 min
Modules and Packages
Modules and Packages
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define what a module is and explain why it's used to organize code.
Import and use functions from Python's built-in modules, like `math` and `random`.
Use dot notation to access functions and variables inside an imported module.
Differentiate between importing an entire module (`import math`) and importing a specific function from a module (`from math import pi`).
Use the `as` keyword to create a shorter alias (nickname) for a module.
Explain that a package is a collection of related modules organized in a folder.
Have you ever built a huge LEGO set? 🏰 Would it be easier if all the pieces were in one giant pile, or sorted into labeled bags? Let's learn how to sort our Python code!
In this lesson, you'll discover how to use modules, w...
2
Key Concepts & Vocabulary
TermDefinitionExample
ModuleA single Python file (with a `.py` extension) that contains functions, variables, and other code you can use in other programs.The `random.py` file is a module that contains functions for generating random numbers, like `randint()`.
PackageA folder that contains a collection of related Python modules. It's a way to organize a large set of tools.A game might have a package called `graphics` that contains modules for `sprites.py`, `animations.py`, and `sound.py`.
Import StatementThe command used to bring the code from a module into your current program so you can use it.`import math` tells Python you want to use the tools available in the `math` module.
Built-in ModuleA module that is included with Python when you install it. You don't need to download...
3
Core Syntax & Patterns
Standard Module Import
import module_name
Use this to import an entire module. To use anything from it, you must use dot notation (e.g., `module_name.function_name()`). This is the most common and safest way to import.
Specific Function Import
from module_name import function_name
Use this when you only need one or two specific things from a module. You can then call the function directly without using dot notation (e.g., `function_name()`).
Import with an Alias
import module_name as alias_name
Use this to give a module a shorter nickname. This helps keep your code clean and easy to type, especially with long module names. All calls will use the alias (e.g., `alias_name.function()`).
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A student's code is not working:
from math import pi
radius = 10
circumference = 2 * math.pi * radius
print(circumference)
What is the error and how should it be fixed?
A.The formula is wrong. It should be `pi * radius ** 2`.
B.The `print` statement is incorrect.
C.The error is `math.pi`. Since `pi` was imported directly, it should just be `2 * pi * radius`.
D.The import is wrong. It should be `import math`.
Challenging
A student is building a large game. They have one file for player logic (`player.py`) and another for enemy logic (`enemy.py`). What is the best way to organize these files so they can be used together in a main game file?
A.Combine all the code from both files into one giant `main.py` file.
B.Create a package (a folder, e.g., 'game_logic') to hold `player.py` and `enemy.py`, then import them into `main.py`.
C.Rename `player.py` to `math.py` and `enemy.py` to `random.py`.
D.Email the files to a friend and have them combine the code.
Challenging
A student sees this line in a professional's code: `import very_long_module_name_for_calculations as calc`. What is the MOST likely reason the programmer used `as calc`?
A.To make the program run faster.
B.To hide what the module really does.
C.To save typing and make the code easier to read by using a short, memorable alias.
D.Because the module would not work without an alias.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free