Creating Modules (.py files to import)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

This Creating Modules (.py files to import) worksheet is part of the Python Coding journey. Print it as a booklet?

Creating Modules (.py files to import)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a module?
A A folder
B A .py file with functions you can import
2. If you create helpers.py, how do you import it?
A import helpers
B import helpers.py
3. from math import sqrt what does this do?
A Imports everything
B Imports only the sqrt function
4. Why create your own modules?
A To organize and reuse code
B Python requires it
5. Where must your module file be?
A In the same folder (or Python's path)
B In a special folder only

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1import my module AOk: Import specific function
2from helpers import greet() BOutput: (defines greet in module)
3import helpers helpers.greet() CError: Module names can't have spaces
4from helpers import greet greet() DError: Must name what to import
5# In helpers.py: def greet(): print('Hi') EValid: Use module prefix
6# In main.py: import helpers helpers.greet() FOutput: Hi

Definitions

Module

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ helpers print(helpers.greet('Al'))
2.
____ helpers import greet print(greet('Al'))
3.
import helpers print(helpers._____('Alice'))
4.
if ________ == '__main__': print('Running directly')
5.
from helpers import ___ result = add(3, 5)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A # Import our helpers module
B import helpers.py
C helpers.greet('Alice')
D print('Done')
Question 2
A import Helpers
B result = Helpers.greet('Al')
C print(result)
D print('Done')
Question 3
A from helpers import greet
B result = helpers.greet('Al')
C print(result)
D print('Done')
Question 4
A import helpers
B result = greet('Al')
C print(result)
D print('Done')
Question 5
A from helpers import *
B # imports everything
C greet('Alice')
D # recommended approach?

Answer Key

Creating Modules (.py files to import)
Section A: Quick Questions
Q1
B — A .py file with functions you can import
Q2
A — import helpers
Q3
B — Imports only the sqrt function
Q4
A — To organize and reuse code
Q5
A — In the same folder (or Python's path)
Section B: Match the Pairs
Q1
import my module → Error: Module names can't have spaces
Q2
from helpers import greet() → Error: Must name what to import
Q3
import helpers helpers.greet() → Valid: Use module prefix
Q4
from helpers import greet greet() → Ok: Import specific function
Q5
# In helpers.py: def greet(): print('Hi') → Output: (defines greet in module)
Q6
# In main.py: import helpers helpers.greet() → Output: Hi
Section C: Definitions
Q1
Module — Any .py file that can be imported and reused.~~import module
Section D: Fill in the Blanks
Q1
import helpers print(helpers.greet('Al'))
Q2
from helpers import greet print(greet('Al'))
Q3
import helpers print(helpers.greet('Alice'))
Q4
if __name__ == '__main__': print('Running directly')
Q5
from helpers import add result = add(3, 5)
Section E: Spot the Error
Q1
B — Don't include .py: just the module name
Q2
A — Module names are case-sensitive. File is helpers.py, so use helpers.
Q3
B — With 'from import', use greet() directly: no module prefix
Q4
B — With 'import helpers', must use helpers.greet() not just greet()
Q5
B — import * is not recommended: import specific functions for clarity