By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Grade 7 Computer Science (ICT) – Python: Functions, Modules, File I/O
"If you write the same code three times in a row—like checking a password, saving a high score, or formatting a username—why does Python let you ‘teach’ it once and then just call it by name? And how do you make your code remember things between runs, like a game saving your progress?"
Imagine you’re running a lemonade stand. Every time a customer orders, you don’t rewrite the recipe from scratch—you just say, "Make a large lemonade." That’s a function: a named set of instructions you can reuse. Now, what if you want to add a loyalty discount or track inventory? Instead of cluttering your main code, you store those rules in a separate module—like a recipe book you can flip to when needed. And when the day ends, you don’t want to lose your sales data. That’s where file I/O comes in: it’s like writing your totals in a notebook so you can read them tomorrow.
Key Vocabulary: - Function Definition: A reusable block of code that performs a specific task when called by name. Example: A function named calculate_tip(bill) that adds 15% to a restaurant bill—used every time a customer pays, not just once. Grade 7 Note: In high school, functions can return multiple values or even other functions (closures).
calculate_tip(bill)
Module Definition: A Python file containing functions, variables, or classes that can be imported into another program. Example: A weather_utils.py module with functions like convert_celsius_to_fahrenheit() that a weather app imports instead of rewriting the formula. Grade 7 Note: Modules can be built-in (like math), third-party (like pandas), or your own.
weather_utils.py
convert_celsius_to_fahrenheit()
math
pandas
File I/O (Input/Output) Definition: Reading from or writing to files on your computer, like saving game progress or loading a list of usernames. Example: A program that writes "High score: 1500" to scores.txt and later reads it back to display on the game’s start screen. Grade 7 Note: In college, file I/O expands to databases, cloud storage, and binary files (like images).
"High score: 1500"
scores.txt
Parameter Definition: A variable passed into a function to customize its behavior. Example: In greet_user(name), name is a parameter—calling greet_user("Aisha") prints "Hello, Aisha!" Grade 7 Note: Later, you’ll learn about default parameters (e.g., name="Guest") and variable-length arguments (*args).
greet_user(name)
name
greet_user("Aisha")
name="Guest"
*args
How This Appears in Classroom Assessments (Grade 7): - Exit Tickets: "Write a function double_number(x) that returns x * 2. Call it with 5 and print the result." - Proficient: Correct function definition, call, and output (10). - Developing: Missing return or prints inside the function instead of returning.
double_number(x)
x * 2
5
10
return
Developing: Only says "it’s shorter" without explaining why that matters.
State Standardized Test (e.g., CSTA or state ICT exams):
open("data.txt", "r")
"r"
open()
readline()
log.txt
enumerate()
readlines()
Model Proficient Response (Short Answer):
with open("log.txt", "r") as file: for line_number, line in enumerate(file, 1): print(f"Line {line_number}: {line.strip()}")
Why it’s proficient: - Uses with to automatically close the file. - enumerate() handles line numbers cleanly. - strip() removes extra whitespace. - No syntax errors or redundant code.
with
strip()
Mistake 1: Forgetting to Return a Value Prompt: "Write a function add(a, b) that returns the sum of a and b." Common Wrong Response:
add(a, b)
a
b
def add(a, b): a + b
Why It Loses Credit: - Missing return statement. The function runs but doesn’t send the result back to the caller. Correct Approach:
def add(a, b): return a + b
Key Idea: Functions return values; they don’t just print them (unless printing is the goal).
Mistake 2: Misusing File Modes Prompt: "Write code to append "New entry" to a file notes.txt." Common Wrong Response:
"New entry"
notes.txt
file = open("notes.txt", "w") file.write("New entry") file.close()
Why It Loses Credit: - "w" mode overwrites the file instead of appending. Previous data is lost. Correct Approach:
"w"
with open("notes.txt", "a") as file: file.write("New entry\n")
Key Idea: "a" = append, "w" = write (erases first), "r" = read only.
"a"
Mistake 3: Importing a Module Without Using It Prompt: "Use the math module to calculate the square root of 25." Common Wrong Response:
import math print(math.sqrt(16)) # Correct function, wrong number
Why It Loses Credit: - The student imports math but doesn’t use it for the specified task (25). Partial credit might be given, but the answer is incomplete. Correct Approach:
import math print(math.sqrt(25)) # Output: 5.0
Key Idea: Read the prompt carefully—assessments test both syntax and attention to detail.
Within Computer Science: Functions-Object-Oriented Programming (OOP) — Functions become methods inside classes, like player.jump() in a game. Understanding functions first makes OOP’s "bundling data + behavior" clearer.
player.jump()
Across Subjects: Modules-Math (Modular Arithmetic) — Just like Python modules group related functions, modular arithmetic groups numbers by remainders (e.g., 17 mod 5 = 2). Both are about organizing complexity into reusable "chunks."
Outside School: File I/O-Save Files in Video Games — When you save a game, it’s writing your progress to a file (like savegame.dat). Loading the game reads that file back. Next time you see a "Save" button, you’ll know it’s just open("savegame.dat", "w") under the hood.
savegame.dat
open("savegame.dat", "w")
"What happens if you define a function inside another function—like a make_greeter() function that returns a greet() function? Can the inner function remember variables from the outer one? Try it with this code:"
make_greeter()
greet()
def make_greeter(name): def greet(): print(f"Hello, {name}!") return greet greeter = make_greeter("Zara") greeter() # What prints?
Pointer Toward the Answer: This is called a closure—the inner function "remembers" the name variable even after make_greeter() finishes running. It’s like a function carrying a backpack of data. In college, closures are used for things like decorators (functions that modify other functions) and event handlers in web apps. Try changing name after creating greeter—does it update? Why or why not?
greeter
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.