DoAssignment study guide

A1.3 · Use arithmetic and string assignment statements

Learn to use arithmetic and string assignment statements through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

ICS3U study topic A1.3

An assignment statement gives a variable a value or replaces the value it already holds. In arithmetic assignment, the value comes from a calculation. In string assignment, the value is text. The left side names the variable that will be updated; the right side provides the value to store. This lesson uses Python 3 to make the rules visible. Other classrooms may use a different programming language, so some symbols or syntax may differ.

What you will learn

  • Explain how an assignment statement stores a value in a variable.
  • Trace arithmetic assignments by using the current values of variables.
  • Use string assignment statements to store, join, and update text.
  • Distinguish the assignment symbol from a comparison.

1. Assignment: calculate, then store

A variable is a named place in a program where a value can be kept. In an assignment statement such as `score = 12`, the equals sign means “assign”: store the value on the right in the variable named on the left. It does not ask whether two sides are equal.
For arithmetic assignment, first work out the expression on the right using the values that exist at that moment. Then replace the left-side variable’s value with the result. For example, if `score` is 12, the statement `score = score + 3` calculates 15 and stores 15 in `score`. The old value is used in the calculation, then replaced.
In Python 3, `+` adds numbers, `-` subtracts, `*` multiplies, and `/` divides. Parentheses can make the intended order clear. Python follows familiar arithmetic order: multiplication and division are done before addition and subtraction, unless parentheses change the order. These are arithmetic expressions used in assignment; they are not separate comparison statements.
  • The variable being assigned is on the left of `=`.
  • The right side is evaluated using the current values before the assignment takes effect.
  • An assignment can replace a variable’s previous value.

2. Arithmetic assignments in a sequence

A program can contain several assignments. Read them in order: each completed statement affects the values available to the next statement. Keep a trace by writing down a variable’s value after every line that changes it.
The same variable may appear on both sides of an assignment. In `total = total + addition`, the current value of `total` is used to calculate a new value, which is then stored back in `total`. This is a straightforward way to update a running total.
An assignment can also use more than one variable. If `price` is 8 and `quantity` is 3, then `cost = price * quantity` stores 24 in `cost`. The values of `price` and `quantity` are unchanged by that statement; only `cost` is assigned.
  • Trace statements from top to bottom.
  • Only the variable on the left side is changed by an assignment.
  • Use the values available at that exact line, not values from before an earlier update.

3. String assignment: store and join text

A string is text, written between quotation marks in Python 3. For example, `name = "Mina"` assigns the text Mina to `name`. The quotation marks show where the text begins and ends in the code; they are not part of the stored text.
In Python 3, the `+` operator joins strings when both values are strings. For instance, `greeting = "Hi, " + name` assigns a new string made from the first string followed by the value of `name`. Spaces and punctuation must be included where they are needed: joining `"Hi,"` and `"Mina"` would not automatically add a space.
A string assignment can replace a string value just as an arithmetic assignment can replace a number. Keep text in quotation marks when writing a string literal, and use a variable name without quotation marks when you want the value stored in that variable.
  • Quotation marks identify string literals in Python 3.
  • String `+` joins text; it does not add a space automatically.
  • A variable name refers to its stored value; quoted text is taken literally.

4. A reliable way to check an assignment

For each assignment, identify the left-side variable, evaluate the right-side expression using the values currently stored, and record the new value. For a string assignment, carefully preserve spaces and punctuation while tracing the joined text.
Python’s `=` is the assignment symbol in the examples here. Do not read it as a question about whether two values match. Also do not assume an assignment changes every variable mentioned: it changes the variable named on the left.
The examples below show inputs, statement-by-statement reasoning, and expected output derived by tracing. Their code is displayed as text and is not claimed to have been run.
  • Find the left side before calculating.
  • Use current values and follow the statements in order.
  • Check the final stored value before deriving the displayed output.

Trace for the running-total example

StatementValue usedNew value of points
points = 14Starting input14
points = points + 614 + 620
points = points - 320 - 317

Worked example

Update a running total

Starting with 14 points, a player earns 6 more points and then loses 3. Trace the assignments and determine the final displayed total.

Python 3

points = 14
points = points + 6
points = points - 3
print(points)
  1. Identify the starting value
    The input for this trace is the starting points value, 14. The first assignment stores that value in `points`.
  2. Apply each update
    The next line uses the current value 14 and adds 6, so `points` becomes 20. The following line uses 20 and subtracts 3, so the new value is 17.
  3. Read the final value
    The display statement uses the value stored after both updates. Therefore, the expected displayed result is 17.
Answer: The final value of `points` is 17, so the expected displayed result is 17.
Check: The first update changes 14 to 20; the second changes 20 to 17.

Worked example

Calculate a total cost

A notebook costs CAD 7 and a pen costs CAD 2. A student buys 3 notebooks and 2 pens. Use arithmetic assignments to find the total cost.

Python 3

notebook_price = 7
pen_price = 2
notebook_cost = notebook_price * 3
pen_cost = pen_price * 2
total = notebook_cost + pen_cost
print(total)
  1. State the inputs
    The inputs are the two unit prices, 7 and 2, and the quantities, 3 and 2. The code stores each input in a named variable.
  2. Calculate each item cost
    The notebook cost is the current notebook price multiplied by its quantity: 7 times 3 gives 21. The pen cost is 2 times 2, which gives 4.
  3. Add the costs
    The final assignment adds the two stored item costs. Since 21 plus 4 is 25, the expected displayed total is CAD 25.
Answer: The expected displayed total is CAD 25.
Check: Three notebooks cost CAD 21, and two pens cost CAD 4; together they cost CAD 25.

Worked example

Build a message from strings

A program has the name `Ari` and the activity `coding`. Assign each value to a variable, join them into a sentence, and determine the expected displayed text.

Python 3

name = "Ari"
activity = "coding"
message = "Ari likes " + activity
print(message)
  1. Assign the text values
    The inputs are the strings `Ari` and `coding`. The first two assignments store those strings in `name` and `activity`.
  2. Join the strings
    The next assignment joins the literal text `Ari likes `, including its final space, with the value of `activity`. The resulting string is `Ari likes coding`.
  3. Determine the output
    The display statement refers to `message`, whose stored value is the joined sentence. The expected displayed text is Ari likes coding, with one space between the words.
Answer: The expected displayed text is `Ari likes coding`.
Check: The space after `likes` is included in the first string, so joining does not run the words together.

Common mistakes and how to avoid them

Reading `total = total + 4` as a claim that the two sides were already equal.
Correction: In these Python 3 examples, `=` assigns: calculate the right side using the current value, then store the result in the left-side variable.
Using an old value when tracing a later line.
Correction: Update the trace after every assignment. Each later statement uses the most recently stored value.
Expecting string joining to insert spaces automatically.
Correction: Include any needed spaces or punctuation in one of the string values before joining.
Putting a variable name in quotation marks when intending to use its stored text.
Correction: Use quotes for literal text, such as `"coding"`; use the unquoted variable name `activity` to refer to its stored value.

Lesson summary

  • An assignment stores a value in the variable named on the left.
  • Arithmetic assignments calculate the right side using current values, then update the left-side variable.
  • Python 3 uses `+` to join strings, and required spaces must be present in the text.
  • Trace assignments in order to derive the final values and expected output.

Check your understanding

Question 1

If `items` starts at 5, what value is stored after `items = items * 2`?
  1. 5
  2. 7
  3. 10
  4. 25
Show answer and explanation
10
The right side uses the current value 5 and multiplies it by 2, so 10 is stored in `items`.

Question 2

In Python 3, what value is stored by `label = "Room " + "8"`?
  1. Room 8
  2. Room8
  3. Room 16
  4. 8 Room
Show answer and explanation
Room 8
Both values are strings, so `+` joins them. The first string already contains a space after `Room`.

Question 3

If `count` is 9, what is its value after `count = count - 4`?
  1. 4
  2. 5
  3. 9
  4. 13
Show answer and explanation
5
The assignment calculates 9 minus 4 and stores the result, 5, in `count`.

Key terms

Assignment statement
A statement that stores a value in the variable named on its left side.
Variable
A name used in a program to refer to a stored value.
Arithmetic expression
A combination of number values and arithmetic operations that produces a number.
String
A value representing text.
String joining
Combining strings in order to make one longer string; in the Python 3 examples, the `+` operator does this.

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.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.

Official curriculum reference

Report a correction or ask a question