Computer Science Grade 5 20 min

Data Storage Concepts

Data Storage Concepts

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Use an 'if' statement to check the value of data stored in a variable. Write an 'if-elif-else' structure to make decisions based on different data values. Use a 'for' loop to access each piece of data stored in a list. Use a 'while' loop to change data in a variable until a condition is met. Explain how variables and lists are used as storage for data in a program. Write a simple program that stores data and uses a loop to process it. Ever played a game where you collect coins? 🪙 How does the game remember your score and decide if you can buy a new power-up? In this lesson, we'll learn how to be the boss of our data! We will use special Python commands called 'control structures' to look at, change,...
2

Key Concepts & Vocabulary

TermDefinitionExample VariableA storage box with a name that holds one piece of data, like a number or a word.In `player_score = 150`, the variable is `player_score` and it's storing the number 150. ListA big storage container that holds many pieces of data in a specific order.In `inventory = ["sword", "shield", "potion"]`, the list `inventory` stores three different words. Control StructureA command that controls the flow of the program, like making a decision or repeating an action.The `if` statement and the `for` loop are both control structures. ConditionA question the computer asks that is either True or False. It's used to make decisions.The code `player_score > 100` is a condition that asks, 'Is the data in player_score bigger than 10...
3

Core Syntax & Patterns

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 between two actions. The computer checks the condition and runs the code in the 'if' block if it's true, or the code in the 'else' block if it's false. The 'for' Loop Pattern for item in list_name: # Code to run for each item Use this to do something with every single piece of data stored in a list. The loop will automatically go through the list one item at a time. The 'while' Loop Pattern while condition: # Code to run as long as condition is True # IMPORTANT: Something inside must change the condition! Use this to repeat an action as long a...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
You have a list of numbers `data = [10, 15, 20, 25, 30]` and you want to create a new list that stores only the even numbers. Which code block correctly does this?
A.`evens = []` `if data % 2 == 0:` `evens.append(data)`
B.`evens = []` `for num in data:` `if num % 2 == 0:` `evens.append(num)`
C.`evens = []` `for num in data:` `if num % 2 != 0:` `evens.append(num)`
D.`evens = data` `for num in evens:` `if num % 2 == 0:` `evens.remove(num)`
Challenging
A programmer wants to store a copy of a list, but reversed. They write this code. Why will the `reversed_list` not contain the correct data? `original = [1, 2, 3]` `reversed_list = []` `for item in original:` `reversed_list.append(item)` `reversed_list.reverse()`
A.The code is correct and works as intended.
B.The `.reverse()` method modifies the list but doesn't store a return value, so the final line does nothing useful.
C.`for` loop cannot be used to copy a list.
D.The `.append()` method adds to the beginning of the list, not the end.
Challenging
In a game, you store your state in two variables. What are the final values stored in `lives` and `score`? `lives = 3` `score = 0` `actions = ['hit', 'coin', 'hit', 'coin', 'hit']` `for action in actions:` `if action == 'hit':` `lives = lives - 1` `elif action == 'coin':` `score = score + 10` `if lives == 0:` `break`
A.lives is 0, score is 20
B.lives is 0, score is 10
C.lives is -1, score is 20
D.lives is 0, score is 0

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Control Structures in Python

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.