B2.2 · Store numbers and text with variables and assignment
Learn to store numbers and text with variables and assignment through clear examples and targeted practice.
Ontario Grade 10 Computer Science
Introduction to Programming
Storing numbers and text in Python 3
A program often needs to remember information while it works. For example, a classroom sign-up program might need to remember a student's name or the number of books requested. A variable is a named place in a program for storing a value. Assignment is the instruction that puts a value into that variable. This lesson focuses on storing numbers and text, and reading those stored values. Examples use Python 3; classroom programming languages may use different spelling or punctuation.
What you will learn
Explain how a variable stores a value.
Use assignment to store a number or text in a variable.
Trace how an assigned value is used in a simple program.
1. A variable gives a value a name
Imagine writing a student's name on a labelled card so you can refer to it later. In a program, a variable name plays the role of the label, and the value is the information stored there. For example, a program can store the text `Mina` in a variable named `student_name`.
In Python, the equals sign is used for assignment. In `student_name = "Mina"`, the name is on the left and the value being stored is on the right. Read this as “store the text Mina in student_name,” not as a question asking whether the two sides are already equal.
Text values are written inside quotation marks in Python. Numbers such as `12` are written without quotation marks. That difference matters: `12` is a number, while `"12"` is text made up of characters. Choose the kind of value that matches the information you want to store.
A variable is a named place for a value.
Assignment stores the value on the right in the variable named on the left.
Use quotation marks around text; write a number without them.
2. Input, process, and output
A simple way to describe a program is with three parts: input, process, and output. Input is information supplied to the program. Process is what the program does with that information. Output is information the program presents. For a name display, the user types a name (input), the program stores it in a variable (process), and the program displays a greeting containing it (output).
In Python, `input()` asks for text and gives the typed response to the program. Assigning that response to a variable lets the program remember it. The response is text, even if the person types digits. This lesson keeps the focus on storing values, so examples that use `input()` store the response as text.
The displayed code is text for learning and tracing; it has not been executed. Follow the lines in order and substitute the stated input to work out what would be displayed. The same idea applies in classroom languages, even if their commands differ.
Input enters the program; process includes storing a value; output presents information.
In Python, `input()` supplies text.
Trace code by following each assignment and display instruction in order.
3. Assignment can replace a stored value
A variable can be assigned a new value later. The variable name stays the same, but its stored value changes. For example, after `seats = 18`, the program can assign `seats = 20`. From that point onward, using `seats` refers to 20, not 18.
When tracing, keep track of the latest value assigned to each variable. An assignment does not automatically preserve the old value under the same name. If a program needs to refer to the old information later, it would need to store it separately; these examples only follow the simple replacement of a value.
Use names that help a reader understand what is stored, such as `book_count` or `club_name`. In Python, variable names do not contain spaces. A clear name makes it easier to see whether a variable is meant to hold a number or text.
A later assignment replaces the value currently stored under that variable name.
When tracing, record the most recent assigned value.
Choose short, descriptive variable names without spaces.
4. Practise reading and writing assignments
To practise, choose one value and decide whether it is a number or text. Write an assignment with a helpful variable name, then check the punctuation: text needs quotation marks, while a number does not. Next, read any later lines in order and update your record if the variable is assigned again.
For instance, a club could store the text name `Robotics` in `club_name` and the number 14 in `member_count`. These assignments store information; they do not describe how the club collected it or make claims about a real club.
Keep the focus on the variable's stored value. A variable is not the output itself: a program must include an instruction to display a value if it is meant to appear for the user.
Decide whether the value is text or a number before writing the assignment.
Check quotation marks and the variable name.
Storing a value and displaying a value are separate actions.
Trace the changing value
Program line
Value stored in places_left
places_left = 8
8
places_left = 9
9
print(places_left)
Displays 9
Worked example
Store a name and display it
A student types the name Mina. Store the response in a variable and display it with a greeting. What output is expected?
Python 3
student_name = input("Type your name: ")
print("Welcome,", student_name)
Identify the input
The person types Mina. In Python, input() provides the typed response as text.
Store and display
The assignment puts that text in student_name. The display instruction uses the stored value in the greeting.
Answer: Expected output: Welcome, Mina
Check: The input is text, so the response is stored as text in student_name.
Worked example
Store a number
A sign displays that 6 books are available. Store the number in a variable and display the stored value.
Python 3
books_available = 6
print(books_available)
Assign the number
The value is the number 6, so it is written without quotation marks. Assignment stores it under the descriptive name books_available.
Trace the display
The print instruction refers to books_available. At that point, the value stored there is 6, so the expected output is 6.
Answer: Expected output: 6
Check: The variable stores a number, not the text value "6".
Worked example
Follow a new assignment
A sign-up program first stores 8 places, then updates the value to 9. What does it display after both assignments?
Tutor support for B2.2 · Store numbers and text with variables and assignment
Working on B2.2 · Store numbers and text with variables and assignment in ICS2O? Bring the question and your own attempt. In a live session, you can trace a small input step by step, compare expected and actual output, and test an edge case. Ask about the first step that does not make sense, then try a similar question yourself.
برای مبحث B2.2 · ذخیره عدد و متن با متغیر و انتساب در درس ICS2O، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک ورودی کوچک را گامبهگام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 10 Computer Science (ICS2O), expectation B2.2. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.