Session 2 of the Python Fundamentals series, covered in depth. It starts with variables and assignment, where Python evaluates the right-hand side and then binds the name, and covers the naming rules and case sensitivity, reassignment, augmented assignment with += and -=, and swapping. It then works through the arithmetic operators, including //, %, and **, operator precedence, and the fact that / always yields a float. From there it covers the four core types - int, float, str, and bool - the type() function, comparisons that produce booleans, and the trap that putting quotes around a number turns it into text. Every snippet and error message was copied verbatim from CPython 3.12.
Subject: Python Fundamentals · 97 slides · code lesson
Open the interactive version of this deck · Homework for this lesson
Title
Python Fundamentals - Session 2
Giving the program a memory - and knowing what it holds
Objectives
A program that cannot remember anything is just a stack of prints. Variables give it a memory. By the end you can:
= really does, and trace a variable after it is reassigned.//, %, and **, with correct precedence.int, float, str, bool - and check any value with type()."100" and 100 differ, and why one cannot do math.Warm-up
Discussion prompt
Before we open Session 2 - Variables & Types: without looking back, what was the main idea of Session 1 - Setup & Your First Program, and what could you do by the end of it that you could not do before?
Hint: One sentence for the idea, one for the skill. If the second one is blank, that is the part to revisit.
Answer:
That session explains what a program is, how Python runs it from top to bottom, and how to use print(), then covers the missing-quote and missing-parenthesis errors.
Section
Part 1
Concept
A variable is a name attached to a value, so you can use the value later without retyping it.
score = 100 makes the name score refer to the value 100. From then on, writing score means 100.
Counterexample
Discussion prompt
score = 100 makes the name score refer to the value 100. From then on, writing score means 100.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Concept
= does not mean 'is equal to'. It means assign: take the value on the right, attach the name on the left to it.
Always read it right-to-left: total = 5 + 3 computes 8 first, then labels it total.
assignment — Evaluate the right-hand side first, then bind the left-hand name to that result. The name is a label you can later move to a different value.
Analogy
Discussion prompt
Explain = means assign, not equals by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
= does not mean 'is equal to'. It means assign: take the value on the right, attach the name on the left to it.
Intuition
Picture the value on a table and the variable name as a sticky label on it. score = 100 sticks the label 'score' onto the value 100.
Reassigning peels the label off and sticks it onto a different value. The label moves; the old value is simply left behind.
Explain it
Discussion prompt
Explain The name is a sticky label to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
Picture the value on a table and the variable name as a sticky label on it. score = 100 sticks the label 'score' onto the value 100.
Pattern
Predict first
The table runs: name | "Sam" | str · score | 100 | int
In Store it, then use it, given the rows so far: what is the next one — the row where name is (printed)?
Correct: (printed) | Sam has 100 points | -
| name | refers to | type |
|---|---|---|
| name | "Sam" | str |
| score | 100 | int |
| (printed) | Sam has 100 points | - |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. name refers to the text "Sam"; score refers to the number 100.
Worked example
name = "Sam"
score = 100
print(name, "has", score, "points")Lines 1-2 store two values under names
Why: name refers to the text "Sam"; score refers to the number 100. Nothing prints yet.
Line 3 swaps each name for its value
Why: Verified by execution: Sam has 100 points.
| name | refers to | type |
|---|---|---|
| name | "Sam" | str |
| score | 100 | int |
| (printed) | Sam has 100 points | - |
Comparison
Comparison matrix
From Store it, then use it: refill the refers to column from what you know. The rest of the table is as it appeared.
| name | refers to | type |
|---|---|---|
| name | "Sam" | str |
| score | 100 | int |
| (printed) | Sam has 100 points | - |
Concept
A name may use letters, digits, and underscores - but cannot start with a digit and cannot contain spaces.
score, high_score, player2 are fine. 2player and high score are not.
Concept
score, Score, and SCORE are three different variables to Python. Capitalization matters.
This is a common source of NameErrors: you define Score but later type score.
Worked example
total_points = 50
print(total_points)Use clear, lowercase names with underscores
Why: total_points says what it holds. A name like tp or x would run fine but read poorly.
Read the output
Why: Verified by execution: prints 50. Good names are for humans - Python is happy either way.
| name | readable? |
|---|---|
| total_points | yes - says what it is |
| tp | cryptic |
| x | meaningless here |
Trade off
Comparison matrix
From Good names read like English: every row here is a choice with a cost. Fill the readable? column, then say which row you would actually pick and what you give up for it.
| name | readable? |
|---|---|
| total_points | yes - says what it is |
| tp | cryptic |
| x | meaningless here |
Anomaly
Predict first
A student writes this, and it looks reasonable:
Starting a name with a digit, or putting a space in it.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: Python starts reading 2cool as the number 2 and then gets confused - a SyntaxError before anything runs.
Move the digit, and use an underscore instead of a space.
Why: Python starts reading 2cool as the number 2 and then gets confused - a SyntaxError before anything runs.
Trap
Starting a name with a digit, or putting a space in it.
2cool = 1A name cannot begin with a digit
Why: Python starts reading 2cool as the number 2 and then gets confused - a SyntaxError before anything runs.
| you write | result |
|---|---|
| 2cool = 1 | SyntaxError: invalid decimal literal |
| my score = 5 | SyntaxError: invalid syntax |
Move the digit, and use an underscore instead of a space.
cool2 = 1
my_score = 5Digits are fine after the first character; underscores replace spaces
Why: cool2 and my_score are both valid. Real outcome: both variables are created with no error.
| name | valid? |
|---|---|
| cool2 | yes |
| my_score | yes |
Section
Part 2
Concept
Assign to the same name again and the label simply moves - the old value is gone unless you saved it elsewhere.
There is no 'memory' of the previous value: after score = 200, the earlier 100 is forgotten.
Concept
A very common move: score = score + 10. The right side is computed first using the current value, then the label moves to the result.
This is not a contradiction - it is 'take the old score, add 10, and make that the new score'.
Ranking
Put in order
Put the moves of A running total into the order they have to happen.
Why: These are the moves of the worked example in the order it makes them, and each one is set up by the one before it. Right side first: 100 + 10 = 110, then the label moves to 110.
Worked example
score = 100
score = score + 10
print(score)Line 1 binds score to 100
Why: The label score is stuck onto 100.
Line 2 uses the OLD score to make the NEW one
Why: Right side first: 100 + 10 = 110, then the label moves to 110.
Trace each line
Why: Verified by execution: prints 110.
| line | score before | score after |
|---|---|---|
| 1 | - | 100 |
| 2 | 100 | 110 |
| 3 | 110 | 110 |
Pattern
Step through it
Step through A running total one row at a time. What is driving the change, and what would the row after the last one be?
Concept
score += 10 is shorthand for score = score + 10. Same for -=, *=, //=, and so on.
It reads as 'add 10 to score', which is exactly what it does. Shorter, and clearer once you know it.
Step zero
Discussion prompt
Gaining and losing points — before any calculation: what is the plan? Name the moves in order, in plain English, without doing the arithmetic.
Hint: It starts with: score += 15 raises score to 115
Answer:
Worked example
score = 100
score += 15
lives = 3
lives -= 1
print(score, lives)score += 15 raises score to 115
Why: Same as score = score + 15.
lives -= 1 lowers lives to 2
Why: Same as lives = lives - 1.
Trace both variables
Why: Verified by execution: prints 115 2.
| line | variable | before | after |
|---|---|---|---|
| 2 | score | 100 | 115 |
| 4 | lives | 3 | 2 |
Blank canvas
Draw it
Draw what Gaining and losing points just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Intuition
Whenever you see x += n, mentally expand it to x = x + n. There is nothing new happening - only fewer keystrokes.
You will reach for it constantly in loops, where a counter or total grows one step at a time.
Concept
Python lets you assign several names at once: a, b = b, a swaps their values in a single line.
The whole right side is evaluated first (using the old values), then handed to the names on the left.
Worked example
a, b = 1, 2
a, b = b, a
print(a, b)Right side is read first: (b, a) is (2, 1)
Why: Python grabs the old values 2 and 1 before assigning anything.
Then a becomes 2 and b becomes 1
Why: Verified by execution: prints 2 1. No temporary variable needed.
| step | a | b |
|---|---|---|
| after line 1 | 1 | 2 |
| after line 2 | 2 | 1 |
Error analysis
Annotate
Walk the callouts on The one-line swap. Each one is a place this is easy to get subtly wrong.
Anomaly
Predict first
A student writes this, and it looks reasonable:
Expecting y to change when x later changes.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: Line 2 gave y the current value of x.
If you want y to follow x, recompute it after x changes.
Why: Line 2 gave y the current value of x. Changing x afterward does not reach back to y.
Trap
Expecting y to change when x later changes.
x = 5
y = x
x = 9
print(y)y was set to the VALUE 5, not to x itself
Why: Line 2 gave y the current value of x. Changing x afterward does not reach back to y.
| line | x | y |
|---|---|---|
| 2 | 5 | 5 |
| 3 | 9 | 5 |
If you want y to follow x, recompute it after x changes.
x = 5
x = 9
y = x
print(y)Assign y after x has its final value
Why: Now y copies the value 9. Real output: 9. A name holds a value, not a live link to another name.
| line | x | y |
|---|---|---|
| 2 | 9 | - |
| 3 | 9 | 9 |
Break the constraint
Discussion prompt
The rule this trap just fixed:
If you want y to follow x, recompute it after x changes.
Now break it on purpose. Build a case that violates it and follow the consequences until something visibly fails. Where does the failure first show up — and would you have noticed it if you had not been looking?
Hint: The dangerous rules are the ones whose violation still produces an answer. If yours fails loudly, try to find one that fails quietly.
Answer:
Line 2 gave y the current value of x. Changing x afterward does not reach back to y.
Section
Part 3
Concept
+ add, - subtract, * multiply, / divide. These work just like a calculator.
You can use them with numbers directly or with variables holding numbers.
Pattern
Predict first
The table runs: 7 + 2 | 9 · 7 - 2 | 5 · 7 * 2 | 14
In The four basic operations, given the rows so far: what is the next one — the row where expression is 7 / 2?
Correct: 7 / 2 | 3.5
| expression | value |
|---|---|
| 7 + 2 | 9 |
| 7 - 2 | 5 |
| 7 * 2 | 14 |
| 7 / 2 | 3.5 |
Why: The relationship between the columns, not the individual numbers, is what generates the next row. 9, 5, 14 respectively - all whole numbers here.
Worked example
print(7 + 2)
print(7 - 2)
print(7 * 2)
print(7 / 2)Add, subtract, multiply behave as expected
Why: 9, 5, 14 respectively - all whole numbers here.
Division with / gives a decimal
Why: Verified by execution: 7 / 2 is 3.5, not 3. More on that soon.
| expression | value |
|---|---|
| 7 + 2 | 9 |
| 7 - 2 | 5 |
| 7 * 2 | 14 |
| 7 / 2 | 3.5 |
Pattern
Step through it
Step through The four basic operations one row at a time. What is driving the change, and what would the row after the last one be?
Concept
// divides and throws away the remainder, giving a whole number: 7 // 2 is 3.
% (modulo) gives only the remainder: 7 % 2 is 1. Together they split a number into a whole part and what is left over.
modulo (%) — The remainder after division. 7 % 2 is 1. Very common for 'every Nth' and even/odd checks.
Definition probe
Sort into buckets
Every line below is part of the definition of assignment or of modulo (%) — one or the other, never both. Put each where it belongs.
Worked example
print(7 // 2)
print(7 % 2)7 // 2 keeps only the whole quotient
Why: 2 fits into 7 three whole times, so the result is 3.
7 % 2 keeps only what is left over
Why: Verified by execution: 7 - (3 * 2) = 1.
| expression | value | meaning |
|---|---|---|
| 7 // 2 | 3 | whole times 2 fits |
| 7 % 2 | 1 | leftover |
Concept
** is exponentiation: 2 ** 5 is 2 multiplied by itself 5 times, which is 32.
Note it is two stars. A single * is ordinary multiplication.
Worked example
print(2 ** 5)
print(10 ** 2)** repeats multiplication
Why: 2 ** 5 is 22222 = 32; 10 ** 2 is 100.
Read the output
Why: Verified by execution.
| expression | value |
|---|---|
| 2 ** 5 | 32 |
| 10 ** 2 | 100 |
Comparison
Comparison matrix
From Powers: refill the value column from what you know. The rest of the table is as it appeared.
| expression | value |
|---|---|
| 2 ** 5 | 32 |
| 10 ** 2 | 100 |
Concept
The / operator produces a float every time, even when it divides evenly. 10 / 2 is 5.0, not 5.
If you want a whole-number result, use // instead.
Worked example
print(10 / 2)
print(type(10 / 2))10 / 2 is 5.0, a float
Why: The decimal point appears even though the division is exact.
type() confirms it
Why: Verified by execution: <class 'float'>. Use // to get the int 5 instead.
| expression | value | type |
|---|---|---|
| 10 / 2 | 5.0 | float |
| 10 // 2 | 5 | int |
Concept
Python follows the usual math order: powers first, then * / // %, then + -. Parentheses override everything.
So 2 + 3 * 4 is 2 + 12 = 14, not 20. Add parentheses to force a different order.
Explain it to yourself
Discussion prompt
In Order of operations this move is made:
Line 2: parentheses go first
Why is that legal? Name the rule or definition it rests on before you read on.
Hint: If you can only say "because that is what you do", the rule is the thing to go and find.
Answer:
Verified by execution: (2 + 3) is 5, then * 4 is 20.
Worked example
print(2 + 3 * 4)
print((2 + 3) * 4)Line 1: multiply before adding
Why: 3 * 4 is 12 first, then + 2 gives 14.
Line 2: parentheses go first
Why: Verified by execution: (2 + 3) is 5, then * 4 is 20.
| expression | value |
|---|---|
| 2 + 3 * 4 | 14 |
| (2 + 3) * 4 | 20 |
Anomaly
Predict first
A student writes this, and it looks reasonable:
Reading 2 + 3 * 4 straight across as (2 + 3) then * 4.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: * binds tighter than +, so 3 * 4 happens first.
If you really want the addition first, say so with parentheses.
Why: * binds tighter than +, so 3 * 4 happens first. The real result is 14, not 20.
Trap
Reading 2 + 3 * 4 straight across as (2 + 3) then * 4.
print(2 + 3 * 4) # expecting 20Left-to-right would give 20 - but that ignores precedence
Why: * binds tighter than +, so 3 * 4 happens first. The real result is 14, not 20.
| expression | you might expect | actual |
|---|---|---|
| 2 + 3 * 4 | 20 | 14 |
If you really want the addition first, say so with parentheses.
print((2 + 3) * 4) # 20Parentheses make the order explicit
Why: Now the sum happens first. Real output: 20. When unsure, add parentheses - they cost nothing and remove doubt.
| expression | value |
|---|---|
| (2 + 3) * 4 | 20 |
Intuition
Think of % like a clock: after 12 it wraps back to 1. n % 2 wraps every 2, so it is 0 for even numbers and 1 for odd.
Any 'every Nth' question - every 2nd, every 5th - is a % N == 0 check underneath.
Worked example
print(4 % 2)
print(7 % 2)An even number has remainder 0 when divided by 2
Why: 4 % 2 is 0.
An odd number has remainder 1
Why: Verified by execution: 7 % 2 is 1. This is how programs test even/odd.
| expression | value | means |
|---|---|---|
| 4 % 2 | 0 | even |
| 7 % 2 | 1 | odd |
Section
Part 4
Concept
An int is a whole number: 7, 0, -12. No decimal point, and Python ints can be as large as you like.
Concept
A float has a decimal point: 3.5, 0.0, -2.9. Any / division produces one.
Concept
Combine a whole number and a decimal in one expression and the result is a float. 3 + 2.5 is 5.5.
The decimal 'spreads': once any float is involved, the answer carries a decimal point too. That is why money math and averages usually come out as floats.
Concept
A str is text wrapped in quotes: "Sam", "100", "hello". The quotes are what make it text rather than a number or a name.
Concept
A bool holds just True or False (capitalized, no quotes). It is the answer to every yes/no question.
Estimation
Predict first
When unsure what you are holding, ask Python:
Commit before you compute: what does type() tells you which come out to? A rough magnitude and the right form is enough — the point is to have something concrete to be wrong about.
Correct: type(value) reports the kind of thing a value is
Why: A prediction you can defend turns the computation into a check rather than a leap of faith — and an answer that contradicts it is caught on the spot. It never changes the value - it just names the type.
Worked example
When unsure what you are holding, ask Python:
print(type(7))
print(type(3.5))
print(type("Sam"))
print(type(True))type(value) reports the kind of thing a value is
Why: It never changes the value - it just names the type. Reach for it whenever a result surprises you.
Read the output
Why: Verified by execution.
| expression | prints | type |
|---|---|---|
| type(7) | <class 'int'> | whole number |
| type(3.5) | <class 'float'> | has a decimal |
| type("Sam") | <class 'str'> | text in quotes |
| type(True) | <class 'bool'> | True/False |
Concept
Every comparison - 5 > 3, x == 10 - evaluates to a bool: True or False.
This is where bool values come from, and it is the fuel for the if statements we build next session.
Explain it
Discussion prompt
Explain Comparisons produce bools to a student a year behind you. No notation, no jargon they have not met — and it still has to be true.
Hint: If your explanation needs a symbol they have never seen, you are describing the notation rather than the idea.
Answer:
Every comparison - 5 > 3, x == 10 - evaluates to a bool: True or False.
Fill the middle
Fill in the blanks
From A comparison is a value — one line has had its right-hand side removed. Put it back.
result = 5 > 3
print(result)
print(type(result))
Why: result is what everything below it consumes, so the wrong expression here fails later and somewhere else. The comparison is true, so result becomes True.
Worked example
result = 5 > 3
print(result)
print(type(result))5 > 3 is computed to a bool and stored
Why: The comparison is true, so result becomes True.
It is a real value with a type
Why: Verified by execution: prints True, then <class 'bool'>.
| expression | value | type |
|---|---|---|
| 5 > 3 | True | bool |
Concept
100 is an int you can do math on. "100" is a str - text that only looks like a number.
This is the single most common beginner confusion, and it becomes critical next session with input().
Analogy
Discussion prompt
Explain Quotes decide str vs number by analogy to something with no Python Fundamentals in it at all — a queue, a recipe, a map, a bank balance, whatever fits. Then say where your analogy breaks.
Hint: An analogy that never breaks is not an analogy, it is the same idea wearing a hat. Find the seam — that is the part that is actually new.
Answer:
100 is an int you can do math on. "100" is a str - text that only looks like a number.
Fill the middle
Fill in the blanks
From Same digits, different type — one line has had its right-hand side removed. Put it back.
a = 100
b = "100"
print(type(a), type(b))
Why: b is what everything below it consumes, so the wrong expression here fails later and somewhere else. a is the number 100; b is the two-character text "100".
Worked example
a = 100
b = "100"
print(type(a), type(b))a has no quotes, b does
Why: a is the number 100; b is the two-character text "100".
type() shows the difference
Why: Verified by execution: <class 'int'> <class 'str'>. They print the same but behave differently.
| variable | value | type |
|---|---|---|
| a | 100 | int |
| b | "100" | str |
Error analysis
Annotate
Walk the callouts on Same digits, different type. Each one is a place this is easy to get subtly wrong.
Anomaly
Predict first
A student writes this, and it looks reasonable:
The quotes make it text - and you cannot add a number to text.
It is wrong. Say what breaks — and say it before you turn the page.
Correct: One side is a string, the other a number.
Store it as a real number - no quotes - so math works.
Why: One side is a string, the other a number. Python refuses to guess and stops.
Trap
The quotes make it text - and you cannot add a number to text.
points = "100"
print(points + 5)points is the TEXT "100", so + 5 mixes types
Why: One side is a string, the other a number. Python refuses to guess and stops.
| expression | result |
|---|---|
| "100" + 5 | TypeError: can only concatenate str (not "int") to str |
Store it as a real number - no quotes - so math works.
points = 100
print(points + 5)Without quotes, points is the number 100
Why: Now + means add: 100 + 5 is 105. Real output: 105. (If it must start as text, convert with int(points) - next session.)
| expression | value |
|---|---|
| 100 + 5 | 105 |
Two truths and a lie
Sort into buckets
Some of these hold up and some are the exact mistakes this lesson is built to prevent. Sort them.
score = 100 makes the name score refer to the value 100. From then on, writing score means 100.; = does not mean 'is equal to'. It means assign: take the value on the right, attach the name on the left to it.; Picture the value on a table and the variable name as a sticky label on it. score = 100 sticks the label 'score' onto the value 100.Concept
Under the hood True counts as 1 and False as 0. So you can even add bools - handy for counting how many conditions are true.
Counterexample
Discussion prompt
Under the hood True counts as 1 and False as 0. So you can even add bools - handy for counting how many conditions are true.
That is stated as though it always holds. Do one of two things: produce a case where it fails, or say precisely what rules such a case out. "It just does" is not on the menu.
Hint: Hunt at the extremes first — zero, one, negative, empty, equal. If every extreme survives, the reason they survive is the proof.
Worked example
print(True + True)
print(True + False)True is 1, False is 0
Why: So True + True is 1 + 1 = 2, and True + False is 1 + 0 = 1.
Read the output
Why: Verified by execution. Later this lets you count how many tests passed by summing their bools.
| expression | as numbers | value |
|---|---|---|
| True + True | 1 + 1 | 2 |
| True + False | 1 + 0 | 1 |
Blank canvas
Draw it
Draw what Adding up bools just did — the shape of it, not the line-by-line working. One picture, labels only where you need them. Then check it against the steps: anything you could not draw is a step you followed rather than understood.
Section
Part 5
Pattern
1. Evaluate the right-hand side completely
Why: Using the current values of every name in it. total = a + b computes a + b first.
2. Bind the left-hand name to that result
Why: The old binding, if any, is replaced. score = score + 10 uses the old score, then moves the label.
3. Read top to bottom, one line at a time
Why: A variable holds a value, not a live link to another variable. Copies do not track their source.
Pattern
1. Numbers bare, text in quotes
Why: 100 is an int; "100" is a str. Quotes are the switch between math and text.
2. Pick the operator you mean
Why: / gives a float, // gives a whole number, % gives the remainder, ** is a power. * and / run before + and -.
3. When a value surprises you, check type()
Why: type(value) tells you what you are really holding, so you know what operations are allowed.
Real world
Discussion prompt
Outside this lesson: where does Session 2 - Variables & Types actually turn up? Name one concrete situation — a job, a piece of software someone ships, a decision somebody has to make — and say which part of Choosing and checking types is doing the work in it.
Hint: Vague is the failure mode here. "Engineering" is not a situation; "deciding whether this build is fast enough to ship" is.
Answer:
Session 2 of the Python Fundamentals series, in depth. Variables and assignment (evaluate the right side, then bind the name); naming rules and case sensitivity; reassignment, augmented assignment (+=, -=), and swapping; the arithmetic operators including // % **; operator precedence; that / always yields a float; the four core types int/float/str/bool; type(); comparisons producing bools; and the trap that quotes turn a number into text.
Check
Trace score line by line.
score = 50
score = score + 25
score = score + 25
print(score)| line | score after |
|---|---|
| 1 | ? |
| 2 | ? |
| 3 | ? |
Check your understanding
What does this print?
Answer: A
Why: score starts at 50, becomes 75 after line 2, then 100 after line 3 - each line builds on the previous value. Verified by execution: prints 100.
Invariant
Step through it
Step through Check: running total one row at a time. One of these columns never changes — find it, and say why it cannot.
Check
Expand the += in your head.
n = 8
n += 2
n -= 3
print(n)| line | n after |
|---|---|
| 1 | ? |
| 2 | ? |
| 3 | ? |
Check your understanding
What does this print?
Answer: A
Why: n += 2 makes n = 10, then n -= 3 makes n = 7. Augmented assignment just adds to or subtracts from the current value. Verified by execution.
Invariant
Step through it
Step through Check: augmented assignment one row at a time. One of these columns never changes — find it, and say why it cannot.
Check
Look at the quotes.
x = "7"
print(type(x))| value | type |
|---|---|
| "7" | ? |
Check your understanding
What does type(x) print?
Answer: B
Why: The quotes around 7 make it text, so x is a str even though it looks like a number. type(x) reports <class 'str'>. Verified by execution.
Check
Which operator runs first?
print(2 + 3 * 4)| expression | value |
|---|---|
| 2 + 3 * 4 | ? |
Check your understanding
What does this print?
Answer: A
Why: * runs before +, so 3 * 4 is 12 first, then + 2 gives 14. Verified by execution.
Check
These look similar but differ in type.
print(9 // 2)
print(9 / 2)| expression | value |
|---|---|
| 9 // 2 | ? |
| 9 / 2 | ? |
Check your understanding
What does this print, line by line?
Answer: A
Why: // is floor division: 9 // 2 drops the remainder and gives the int 4. / always gives a float: 9 / 2 is 4.5. Verified by execution.
Check
Does y follow x?
x = 5
y = x
x = 9
print(y)| line | x | y |
|---|---|---|
| 2 | 5 | ? |
| 3 | 9 | ? |
Check your understanding
What does this print?
Answer: A
Why: y was set to x's value (5) on line 2. Reassigning x to 9 later does not change y, because y holds its own copy of 5. Verified by execution.
Trade off
Comparison matrix
From Check: copy independence: every row here is a choice with a cost. Fill the y column, then say which row you would actually pick and what you give up for it.
| line | x | y |
|---|---|---|
| 2 | 5 | ? |
| 3 | 9 | ? |
Check
Remember what % returns.
print(10 % 2)
print(11 % 2)| expression | value |
|---|---|
| 10 % 2 | ? |
| 11 % 2 | ? |
Check your understanding
What does this print, line by line?
Answer: A
Why: % is the remainder. 10 divides evenly by 2 (remainder 0 - even); 11 leaves remainder 1 (odd). Verified by execution.
Comparison
Comparison matrix
From Check: even or odd: refill the value column from what you know. The rest of the table is as it appeared.
| expression | value |
|---|---|
| 10 % 2 | ? |
| 11 % 2 | ? |
Connect it up
Draw it
One page, no notation unless you need it: draw how these connect — Variables: A Memory for the Program · Changing a Variable · Doing Math · The Four Core Types · Patterns & Checks. Put an arrow wherever one of them is what makes another possible, and label the arrow with why.
Recap
A variable is a name for a value. = assigns: evaluate the right side, then move the label. += is shorthand for building the new value from the old.
| You write | It means |
|---|---|
| score = 100 | attach the name score to 100 |
| score += 10 | score = score + 10 |
| a, b = b, a | swap the two values |
| 7 // 2 , 7 % 2 | 3 (whole) , 1 (remainder) |
| 10 / 2 | 5.0 - always a float |
Four types - int, float, str, bool - and type() tells you which. Quotes decide number vs text. Next session we let the user type values in with input().
Want this taught 1-on-1? Alexander tutors Python Fundamentals — $55/session, free consultation.