Computer Science
Grade 5
20 min
Creating Your First Table: A Class Roster Database
Design and create a simple table to store information about students in a class.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Use a `for` loop to iterate through a list of student data.
Write an `if/elif/else` statement to make decisions based on student information.
Combine loops and conditionals to filter a class roster based on a specific criterion.
Represent a simple database table using a list of lists in Python.
Use a counter variable inside a loop to assign a unique ID to each student.
Explain how control structures help manage and organize data in a program.
Ever wonder how a video game keeps track of every player's score and level? 🎮 We can learn the secret by building our own mini-database for our class roster!
In this lesson, you'll learn how to use special commands called 'control structures' to build and manage a class roster. These commands le...
2
Key Concepts & Vocabulary
TermDefinitionExample
List of ListsA way to create a table or grid of data. It's a list where each item inside is another list, representing a row.`roster = [["Ava", 10], ["Ben", 9], ["Cora", 10]]` is a table with student names and ages.
Control StructureA command that directs the order in which the program's code is executed. It controls the 'flow' of the program.`for` loops and `if` statements are the two main control structures we will use.
For LoopA control structure that repeats a block of code for each item in a sequence, like a list.`for student in students: print(student)` will print each student's name from the `students` list.
Conditional (if/else)A control structure that lets the program make a decision. It runs one block o...
3
Core Syntax & Patterns
The `for` Loop Pattern
for item_variable in list_variable:
# code to repeat
Use this pattern to perform an action for every single item in a list. The `item_variable` will hold the current item from the list during each repetition. Don't forget the colon and the indentation!
The `if-else` Decision Pattern
if condition:
# code to run if condition is True
else:
# code to run if condition is False
Use this to make a choice in your code. The program checks the `condition`. If it's true, the first block of code runs. If not, the code under `else` runs.
Accessing Table Data
table[row_index][column_index]
To get a specific piece of data from our roster table (a list of lists), use two sets of square brackets. The first number is the row (the student),...
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
You have a `full_roster` list and a `students_absent` list. You need to create a new list called `students_present`. Which set of steps correctly describes the logic?
A.Loop through `students_absent`. For each absent student, add them to the `students_present` list.
B.Create an empty `students_present` list. Loop through the `full_roster`. If a student's name is also in `students_absent`, add them to `students_present`.
C.Create an empty `students_present` list. Loop through the `full_roster`. If a student's name is NOT in `students_absent`, add them to `students_present`.
D.Copy the `full_roster` to `students_present`. Then loop through `students_absent` and add them again.
Challenging
To build a feature that checks for duplicate names in your `class_roster` list, which is the most effective method using control structures?
A.Use one `for` loop to print every name. You can see the duplicates on the screen.
B.Use a nested loop. The outer loop picks a student, and the inner loop checks if any *other* student in the list has the same name.
C.Use one `if` statement to check if `class_roster == class_roster`.
D.Create a variable `has_duplicates = False` and hope for the best.
Easy
In our class roster program, what is the main job of a `for` loop?
A.To store one student's name
B.To repeat a set of instructions for every student in the roster
C.To ask a yes or no question about a student
D.To stop the program immediately
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free