DoAssignment study guide

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 lineValue stored in places_left
places_left = 88
places_left = 99
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)
  1. Identify the input
    The person types Mina. In Python, input() provides the typed response as text.
  2. 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)
  1. 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.
  2. 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?

Python 3

places_left = 8
places_left = 9
print(places_left)
  1. Read the first assignment
    The first line stores the number 8 in places_left. For a moment, that is the value associated with the variable.
  2. Read the later assignment
    The second assignment stores 9 using the same variable name. It replaces the earlier value, so the following display instruction presents 9.
Answer: Expected output: 9
Check: The latest assignment determines the value stored in places_left when it is displayed.

Common mistakes and how to avoid them

Writing text without quotation marks, such as student_name = Mina.
Correction: In Python, write text in quotation marks: student_name = "Mina".
Putting quotation marks around a number when the program needs a number.
Correction: Write a number such as 6 without quotation marks. Quotation marks make it text.
Thinking a variable keeps its earlier value after a new assignment to the same name.
Correction: A new assignment replaces the value stored under that name.
Assuming that storing a value automatically displays it.
Correction: Use a display instruction, such as Python's print(), when the value should appear as output.

Lesson summary

  • A variable is a named place for storing a value.
  • Assignment stores a value in a variable; in Python, the variable name is on the left of the equals sign.
  • Text is written in quotation marks, while numbers are written without them.
  • A later assignment to the same variable replaces the earlier stored value.
  • Input, process, and output describe information entering, being handled, and being presented by a simple program.

Check your understanding

Question 1

Which assignment stores the text Maple in a Python variable named town?
  1. town = "Maple"
  2. town = Maple
  3. "Maple" = town
  4. town == "Maple"
Show answer and explanation
town = "Maple"
The variable name is on the left, and quotation marks show that Maple is text.

Question 2

A program has the lines count = 4, then count = 7, then print(count). What value is displayed?
  1. 4
  2. 7
  3. count
  4. Nothing, because count has two values
Show answer and explanation
7
The second assignment replaces the first value. When print(count) is reached, count stores 7.

Question 3

In Python, what kind of value does input() provide?
  1. Text
  2. A number in every case
  3. A variable name
  4. Output
Show answer and explanation
Text
input() provides the typed response as text. The examples here store that response as text.

Key terms

Variable
A named place in a program for storing a value.
Assignment
An instruction that stores a value in a variable.
Text
Words or characters stored as a value; in Python, text is written inside quotation marks.
Input
Information supplied to a program.
Output
Information a program presents.

Continue through ICS2O

View the complete ICS2O Ontario Grade 10 Computer Science curriculum and lessons

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.

Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.

برای مبحث 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.

Official curriculum reference

Report a correction or ask a question