Learn to distinguish a variable from a constant through clear examples and targeted practice.
Ontario Grade 10 Computer Science
Introduction to Programming
ICS2O study guide for distinguishing two kinds of named values
A program often needs to remember values by giving them names. Some named values may change while the program is working. Others are intended to stay the same. In this lesson, you will learn to distinguish a variable from a constant by asking whether its value is meant to change. The examples use beginner-level Python 3. Classroom programming languages can use different rules or conventions, so follow the language and instructions used by your teacher.
What you will learn
Explain the difference between a variable and a constant.
Identify whether a named value is intended to change or stay the same in a program.
Use simple Python 3 examples to show a variable changing and a constant being kept fixed.
1. A name can hold a value
Imagine keeping track of how many books have been placed on a return cart. At the start, the count is zero. Each time a book is added, the count changes. A program can give this changing count a name, such as books_on_cart. The name helps a reader understand what the value represents.
A variable is a named value that can change while a program is being used. In Python, an assignment such as books_on_cart = 0 gives the name an initial value. Later, another assignment such as books_on_cart = books_on_cart + 1 gives the name a different value. The name is still books_on_cart, but its value has changed.
Input, process, and output describe three parts of a simple program situation. Input is information provided to the program, process is what the program does with information, and output is what it presents. For example, a person could provide the number of books added as input; the program could update a variable as its process; and the new count could be output. These labels describe the situation, while variable describes a kind of named value.
A variable has a name and a value that may change.
An assignment gives a name a value.
A program can keep the same variable name while giving it a new value.
2. A constant is intended to stay the same
Now imagine a classroom activity that uses a fixed maximum of 20 books for one cart. If the program uses that limit throughout the activity, it can give the value a meaningful name such as MAX_BOOKS. A constant is a named value intended to stay the same while the program is being used.
In Python, writing a name in capital letters, as in MAX_BOOKS, is a common convention for showing that the value is intended to be constant. Python does not make the value impossible to change just because the name is capitalized. The convention communicates the programmer's intention: use this value as a fixed setting rather than changing it during the program.
To distinguish the two, focus on intended use rather than the spelling alone. A name in lowercase is not automatically a variable, and a name in capitals is not magically protected from changes. Ask: Is this value expected to be updated as the program works, or is it meant to remain fixed? A changing book count is a variable. A fixed limit for the activity is a constant.
A constant is a named value intended to remain fixed.
Capital letters are a common Python convention for constant names.
A name's intended role matters; capitalization alone does not enforce that role.
3. Read and compare short examples
In the first example, the program starts with a book count of 2. Adding one book changes the value associated with books_on_cart to 3. The name is a variable because the program updates its value.
In the second example, MAX_BOOKS is set to 20 and is only read when the program displays the limit. It is intended to remain fixed. The code does not change it. Python will allow a programmer to assign a different value to that name, but doing so would go against the convention used here.
The examples use input, process, and output in a small way. The starting count is the input to the example, increasing the count is the process, and displaying the result is the output. The fixed limit is a constant used by the program. These examples are written code, not claims that the code was run. When tracing, follow each assignment in order and note the value each name has at that point.
When you practise, mark each named value as variable or constant and give a reason based on what the program intends. For a count that goes up as items are added, explain that its value changes. For a fixed limit used throughout an activity, explain that it is intended to stay the same. Do not decide based only on whether a name uses capital letters.
Trace assignments in order to see whether a value changes.
Explain the intended role of each name.
A constant can be used in a calculation or displayed without changing it.
4. Check your understanding
For each name in a short program, ask two questions: What value does it represent? Is that value intended to change as the program works? Use the answers to decide whether it is a variable or a constant.
For practice, consider a program that keeps track of the number of people who have entered a room and uses a fixed room limit of 30. The number of people may change as people enter, so its name represents a variable. The room limit is intended to stay at 30, so its name represents a constant. In both cases, your reason is more important than the particular name chosen.
Common mistakes include calling every named value a variable, or assuming that capital letters guarantee an unchangeable value. A named value can instead be intended as a constant, and Python capitalization is a convention rather than a lock. Another mistake is to call a value constant simply because it has not changed yet. Consider what the program is meant to do with it.
Changing count: variable.
Fixed limit: constant.
Use intended behaviour, not just the name's appearance, to distinguish them.
Trace of the changing book count
Program point
books_on_cart
After the first assignment
2
After adding one
3
Worked example
A count that changes
Trace the value of books_on_cart. It starts at 2, and then one more book is added. Is it a variable or a constant?
The first assignment gives books_on_cart the value 2.
Update the count
The next assignment adds one to the current value, so the name now represents 3. Because its value is intended to change, books_on_cart is a variable.
Answer: books_on_cart is a variable. Its value changes from 2 to 3.
Check: The name remains the same, but its value is updated.
Worked example
A fixed activity limit
A program uses 20 as the maximum number of books for a cart. The limit is meant to stay fixed during the activity. Which kind of named value should represent it?
Python 3
MAX_BOOKS = 20
print(MAX_BOOKS)
Name the fixed value
MAX_BOOKS is assigned 20. The capital letters follow a common Python convention for a value intended to remain fixed.
Read the value
Displaying MAX_BOOKS does not change it. Since the limit is intended to stay the same throughout the activity, it is a constant.
Answer: MAX_BOOKS is intended to be a constant with the value 20.
Check: The code uses the constant but does not assign it a new value.
Worked example
One variable and one constant
A room has a fixed limit of 30 people. At one point, 12 people are in the room. Identify the constant and the variable, then state the value of each.
Tutor support for B1.3 · Distinguish a variable from a constant
Working on B1.3 · Distinguish a variable from a constant 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.
برای مبحث B1.3 · تمایز متغیر و ثابت در درس ICS2O، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک ورودی کوچک را گامبهگام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 10 Computer Science (ICS2O), expectation B1.3. 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.