DoAssignment study guide

A1.1 · Use variables and constants with appropriate data types

Learn to use variables and constants with appropriate data types through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

Ontario Grade 11 Computer Science — study topic A1.1

A program often needs to remember information while it works. It can give that information a name, such as temperature or studentName, and choose a data type that describes the kind of value the name will hold. A value that may change is stored in a variable. A value intended to stay the same during the program is represented as a constant. This lesson focuses on making those choices carefully. The examples use Python 3, but other classroom languages may use different rules or notation. The code is shown as text and traced by hand; it has not been run.

What you will learn

  • Explain how a variable differs from a constant.
  • Choose a data type that fits the kind of value a program needs to store.
  • Trace how named values are assigned and used in a short program.
  • Identify common mismatches between a value and its intended type.

1. Names that store information

A variable is a named place for a value that may be updated as a program runs. For example, a program might store the number of pages a reader has completed in a variable named pagesRead. The name helps a person understand what the value represents. If the reader completes another page, the program can assign a new value to pagesRead.
A constant is a named value that the program is meant to keep unchanged. For example, a program that calculates a fixed club fee could store that fee in a constant named CLUB_FEE. Some languages have a special way to declare constants. Python 3 does not enforce constants through a special declaration; programmers commonly write their names in uppercase as a reminder not to change them.
Choose a name that describes the information, rather than a vague name such as x or data when a clearer name is practical. In Python, names are case-sensitive: fee and Fee are different names. Python assignment uses an equals sign to give a name a value; in this context it means assignment, not a claim that two changing program values are permanently equal.
  • Variables are for named values that may change.
  • Constants are named values intended to remain unchanged.
  • Python 3 conventionally uses uppercase names for constants, but this is a reminder rather than an enforced rule.

2. Match the data type to the information

A data type tells the program what kind of value a name holds. It also helps a reader understand how that value is intended to be used. In introductory Python, common types include int for whole numbers, float for numbers with a decimal part, str for text, and bool for a true-or-false value.
Start by asking what the information means. A count of books is naturally a whole number, so int is suitable. A measured distance may include part of a unit, so float may be suitable. A person's name is text, so str is appropriate. A value that records whether a task is complete is true or false, so bool fits.
Do not choose a type only by looking at the characters. A postal code may contain digits but is used as an identifying label, not as a quantity to add. It is usually better represented as text. Similarly, a phone number is not a count or a measurement; storing it as text preserves formatting such as a leading zero. The purpose of a value matters as much as its appearance.
Python associates a type with each value. When a program assigns a new value to a variable, the variable can refer to a value of a different type. For clear beginner programs, choose a type that consistently matches the information you intend the name to represent. This lesson does not rely on converting between types.
  • Use int for whole-number counts, float for values that may include a decimal part, str for text, and bool for true-or-false information.
  • Decide based on the meaning and intended use of the information.
  • An identifier made of digits, such as a postal code, may be text rather than a numeric quantity.

3. Trace assignments and check your choices

A simple way to check a program is to read its assignments in order and record each name's current value and type. An assignment can create a name or give an existing variable a new value. A constant should not be reassigned in the intended design, even if a language does not prevent it.
Before writing a value, make a small plan: identify the information, decide whether it can change, and select an appropriate type. Then trace each assignment and any displayed output. This makes it easier to catch a value that does not fit its name, such as storing a person's age as a sentence when later calculations expect a number.
Python 3 displays strings without their quotation marks when printed. For example, if a string value is 'Ready', printing that value displays Ready. The quotation marks are part of how text is written in source code; they are not included in the displayed text.
  • Trace assignments in the order they appear.
  • Check both the value and its type against the meaning of the name.
  • Distinguish quotation marks in source code from the text printed as output.

Example 1: trace of the reading values

Program pointpages_readTARGET_PAGES
After initial assignments3 (int)10 (int)
After the count update4 (int)10 (int)
At print statement4 (int)10 (int)

Example 2: stored values and suitable types

NameValueTypeReason
member_nameMaya ChenstrA name is text
items_borrowed2intA whole-number count
all_items_returnedFalseboolA true-or-false status
postal_code0412strAn identifying label

Worked example

A changing count and a fixed target

A reading program begins with three completed pages and has a target of ten pages. Choose names and types, update the completed count by one, and determine what the final print statement displays.

Python 3

pages_read = 3
TARGET_PAGES = 10
pages_read = pages_read + 1
print("Pages read:", pages_read, "of", TARGET_PAGES)
  1. Choose the names and types
    pages_read is a changing whole-number count, so it is a variable with an integer value. TARGET_PAGES is intended to stay fixed and is also a whole number, so it is written as a constant by Python convention.
  2. Trace the assignments
    The first assignment gives pages_read the value three. The next assignment replaces it with four. TARGET_PAGES remains ten because the code does not assign it a new value.
  3. Read the output
    The print statement displays the current values in order. The count is four and the target is ten, so the expected line is Pages read: 4 of 10.
Answer: Expected output: Pages read: 4 of 10
Check: The updated count remains a whole number, and the target constant is not reassigned.

Worked example

Choose types by meaning, not appearance

A school club records a member's name, the number of borrowed items, whether the member has returned all items, and a postal code. Select suitable Python types and determine the values and types after the assignments.

Python 3

member_name = "Maya Chen"
items_borrowed = 2
all_items_returned = False
postal_code = "0412"
  1. Match each piece of information to a type
    A member's name and postal code are text, so they use str. The postal code looks numeric, but it identifies a location rather than measuring a quantity. The number of borrowed items is a whole-number count, so it uses int. The returned-all-items status is true or false, so it uses bool.
  2. Trace the stored values
    The assignments give the four names their stated values. Nothing later changes those names, so their values and types remain as assigned.
  3. Check the interpretation
    The postal code is kept with its leading zero because it is text. The item count can be understood as a quantity. The Boolean value False means the member has not returned all items.
Answer: The values are Maya Chen (str), 2 (int), False (bool), and 0412 (str).
Check: Keeping 0412 as text preserves its leading zero and reflects its role as an identifier.

Worked example

Update a measurement while keeping its unit clear

A plant log stores a starting height of 12.5 centimetres. After a new measurement, the height is 13.0 centimetres. Choose a suitable variable and type, then state what the final print statement displays.

Python 3

plant_height_cm = 12.5
plant_height_cm = 13.0
print("Plant height:", plant_height_cm, "cm")
  1. Choose a variable and type
    The plant height changes when a new measurement is recorded. It may include a decimal part, so plant_height_cm is a variable holding a float value. Including cm in the name reminds the reader of the unit.
  2. Trace the new measurement
    The first assignment stores 12.5. The next assignment replaces it with 13.0. The current value is therefore 13.0, and it remains a float in this example.
  3. Determine the displayed line
    The print statement displays the label, current measurement, and unit. Tracing the values gives the expected output Plant height: 13.0 cm.
Answer: Expected output: Plant height: 13.0 cm
Check: A measurement with a decimal part is appropriately represented here as a float, and the variable changes to the newer measurement.

Common mistakes and how to avoid them

Treating every value made of digits as a number.
Correction: Ask what the value means. A postal code or phone number is an identifier, so text is often more suitable than a numeric type.
Changing a constant because Python permits assigning to its name.
Correction: Use a constant name for a value intended to stay fixed, and follow the uppercase naming convention in Python. The programmer is responsible for respecting that intention.
Using a text value where a numeric count is intended.
Correction: Choose int for a whole-number count and make sure the value assigned represents that count.
Thinking that quotes around a string appear in printed output.
Correction: Quotes mark text in Python source code. Printing the string displays its text without the quote characters.

Lesson summary

  • A variable is a named value that may change; a constant is intended to remain unchanged.
  • Choose a data type that matches the value's meaning and intended use.
  • In Python 3, int, float, str, and bool are common introductory types.
  • Trace assignments in order to determine each name's current value and expected output.

Check your understanding

Question 1

A program stores the number of goals scored in a game. Which type is the most suitable if goals are counted as whole numbers?
  1. int
  2. str
  3. bool
  4. float
Show answer and explanation
int
A goal count is a whole-number quantity, so int is suitable.

Question 2

A code uses POSTAL_CODE = "0061". Why is str a suitable type?
  1. The postal code is an identifier, and text preserves the leading zeros.
  2. Postal codes are always decimal measurements.
  3. A string is the only type that can be assigned a name.
  4. The value must be true or false.
Show answer and explanation
The postal code is an identifier, and text preserves the leading zeros.
The value labels a location rather than representing a quantity, and text preserves its leading zeros.

Question 3

A program starts with attempts = 1 and then assigns attempts = attempts + 1. What is the value of attempts after the second assignment?
  1. 1
  2. 2
  3. attempts + 1
  4. The value becomes text.
Show answer and explanation
2
The right side uses the current value, one, and adds one. The new value assigned to attempts is two.

Key terms

Variable
A named place for a value that may change while a program runs.
Constant
A named value intended to stay unchanged in a program.
Data type
A category that describes the kind of value stored, such as whole number, text, or true-or-false information.
Assignment
An instruction that gives a name a value or updates the value associated with that name.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A1.1. 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