Session 2 - Variables & Types

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

What this lesson covers

The lesson, slide by slide

1. Variables & Types

Title

Python Fundamentals - Session 2

Giving the program a memory - and knowing what it holds

2. What you will be able to do

Objectives

A program that cannot remember anything is just a stack of prints. Variables give it a memory. By the end you can:

  1. Store a value in a variable and use it later by name.
  2. Explain what = really does, and trace a variable after it is reassigned.
  3. Use the math operators, including //, %, and **, with correct precedence.
  1. Name the four core types - int, float, str, bool - and check any value with type().
  2. Explain why "100" and 100 differ, and why one cannot do math.

3. What survived from Session 1 - Setup & Your First Program?

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.

4. Variables: A Memory for the Program

Section

Part 1

5. A variable is a named box

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.

6. Break it if you can: A variable is a named box

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.

7. = means assign, not equals

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.

8. By analogy: = means assign, not equals

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.

9. The name is a sticky label

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.

10. Teach it back: The name is a sticky label

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.

11. Predict the next row: Store it, then use it

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

namerefers totype
name"Sam"str
score100int
(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.

12. Store it, then use it

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.

namerefers totype
name"Sam"str
score100int
(printed)Sam has 100 points-

13. Fill in: refers to for Store it, then use it

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.

namerefers totype
name"Sam"str
score100int
(printed)Sam has 100 points-

14. Rules for naming variables

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.

15. Names are case-sensitive

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.

16. Good names read like English

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.

namereadable?
total_pointsyes - says what it is
tpcryptic
xmeaningless here

17. What each one costs: Good names read like English

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.

namereadable?
total_pointsyes - says what it is
tpcryptic
xmeaningless here

18. Something is wrong here: an illegal name

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.

19. Trap: an illegal name

Trap

The trap

Starting a name with a digit, or putting a space in it.

2cool = 1

A 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 writeresult
2cool = 1SyntaxError: invalid decimal literal
my score = 5SyntaxError: invalid syntax

The fix

Move the digit, and use an underscore instead of a space.

cool2 = 1
my_score = 5

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

namevalid?
cool2yes
my_scoreyes

20. Changing a Variable

Section

Part 2

21. Reassignment replaces the value

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.

22. Build the new value from the old

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

23. What has to happen first: A running total

Ranking

Put in order

Put the moves of A running total into the order they have to happen.

  1. Line 1 binds score to 100
  2. Line 2 uses the OLD score to make the NEW one
  3. Trace each line

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.

24. A running total

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.

linescore beforescore after
1-100
2100110
3110110

25. Watch it run: A running total

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?

  1. Step 1: line is 1
  2. Step 2: line is 2
  3. Step 3: line is 3

26. Augmented assignment: += and friends

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.

27. Plan first: Gaining and losing points

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:

  1. score += 15 raises score to 115
  2. lives -= 1 lowers lives to 2
  3. Trace both variables

28. Gaining and losing points

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.

linevariablebeforeafter
2score100115
4lives32

29. Draw the shape of it: Gaining and losing points

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.

30. += is just a shortcut

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.

31. Swapping two variables

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.

32. The one-line swap

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.

stepab
after line 112
after line 221

33. Inspect it line by line: The one-line swap

Error analysis

Annotate

Walk the callouts on The one-line swap. Each one is a place this is easy to get subtly wrong.

  • Python grabs the old values 2 and 1 before assigning anything.
  • Verified by execution: prints 2 1. No temporary variable needed.

34. Something is wrong here: a copy does not track the original

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.

35. Trap: a copy does not track the original

Trap

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

linexy
255
395

The fix

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.

linexy
29-
399

36. Break it on purpose: a copy does not track the original

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.

37. Doing Math

Section

Part 3

38. The everyday operators

Concept

+ add, - subtract, * multiply, / divide. These work just like a calculator.

You can use them with numbers directly or with variables holding numbers.

39. Predict the next row: The four basic operations

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

expressionvalue
7 + 29
7 - 25
7 * 214
7 / 23.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.

40. The four basic operations

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.

expressionvalue
7 + 29
7 - 25
7 * 214
7 / 23.5

41. Watch it run: The four basic operations

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?

  1. Step 1: expression is 7 + 2
  2. Step 2: expression is 7 - 2
  3. Step 3: expression is 7 * 2
  4. Step 4: expression is 7 / 2

42. // floor division and % remainder

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.

43. Take the definitions apart: assignment vs modulo (%)

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.

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.
modulo (%)
The remainder after division.; Very common for 'every Nth' and even/odd checks.
b1
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.
b2
The remainder after division. 7 % 2 is 1. Very common for 'every Nth' and even/odd checks.

44. Whole part and remainder

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.

expressionvaluemeaning
7 // 23whole times 2 fits
7 % 21leftover

45. raises to a power

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.

46. Powers

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.

expressionvalue
2 ** 532
10 ** 2100

47. Fill in: value for Powers

Comparison

Comparison matrix

From Powers: refill the value column from what you know. The rest of the table is as it appeared.

expressionvalue
2 ** 532
10 ** 2100

48. / always gives a float

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.

49. Even division still floats

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.

expressionvaluetype
10 / 25.0float
10 // 25int

50. Precedence: * and / before + and -

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.

51. Why is this step legal: Line 2: parentheses go first

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.

52. Order of operations

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.

expressionvalue
2 + 3 * 414
(2 + 3) * 420

53. Something is wrong here: assuming strict left-to-right

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.

54. Trap: assuming strict left-to-right

Trap

The trap

Reading 2 + 3 * 4 straight across as (2 + 3) then * 4.

print(2 + 3 * 4)  # expecting 20

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

expressionyou might expectactual
2 + 3 * 42014

The fix

If you really want the addition first, say so with parentheses.

print((2 + 3) * 4)  # 20

Parentheses make the order explicit

Why: Now the sum happens first. Real output: 20. When unsure, add parentheses - they cost nothing and remove doubt.

expressionvalue
(2 + 3) * 420

55. Modulo is a wrap-around

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.

56. Even or odd with %

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.

expressionvaluemeans
4 % 20even
7 % 21odd

57. The Four Core Types

Section

Part 4

58. int - whole numbers

Concept

An int is a whole number: 7, 0, -12. No decimal point, and Python ints can be as large as you like.

59. float - numbers with a decimal

Concept

A float has a decimal point: 3.5, 0.0, -2.9. Any / division produces one.

60. Mixing an int and a float gives a float

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.

61. str - text in quotes

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.

62. bool - True or False

Concept

A bool holds just True or False (capitalized, no quotes). It is the answer to every yes/no question.

63. Guess the shape of the answer: type() tells you which

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.

64. type() tells you which

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.

expressionprintstype
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

65. Comparisons produce bools

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.

66. Teach it back: Comparisons produce bools

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.

67. Restore the missing line: A comparison is a value

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.

68. A comparison is a value

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

expressionvaluetype
5 > 3Truebool

69. Quotes decide str vs number

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

70. By analogy: Quotes decide str vs number

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.

71. Restore the missing line: Same digits, different type

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

72. Same digits, different type

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.

variablevaluetype
a100int
b"100"str

73. Inspect it line by line: Same digits, different type

Error analysis

Annotate

Walk the callouts on Same digits, different type. Each one is a place this is easy to get subtly wrong.

  • a is the number 100; b is the two-character text "100".
  • Verified by execution: <class 'int'> <class 'str'>. They print the same but behave differently.

74. Something is wrong here: "100" cannot do math

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.

75. Trap: "100" cannot do math

Trap

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

expressionresult
"100" + 5TypeError: can only concatenate str (not "int") to str

The fix

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

expressionvalue
100 + 5105

76. Which of these survive contact with Session 2 - Variables & Types?

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.

Holds up
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.
Breaks
Starting a name with a digit, or putting a space in it.; Expecting y to change when x later changes.
sound
These are stated as this lesson states them — each one survives the edge cases Session 2 - Variables & Types puts it through.
flawed
Each of these is lifted from a trap in this deck: reasonable-sounding, and wrong in a way that only shows up once you rely on it.

77. A bool is secretly 1 and 0

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.

78. Break it if you can: A bool is secretly 1 and 0

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.

79. Adding up bools

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.

expressionas numbersvalue
True + True1 + 12
True + False1 + 01

80. Draw the shape of it: Adding up bools

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.

81. Patterns & Checks

Section

Part 5

82. Reading any assignment

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.

83. Choosing and checking types

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.

84. Where this shows up: Session 2 - Variables & Types

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.

85. Check: running total

Check

Trace score line by line.

score = 50
score = score + 25
score = score + 25
print(score)
linescore after
1?
2?
3?

Check your understanding

What does this print?

  • A. 100 (correct)
  • B. 50
  • C. 75
  • D. 25

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.

Why B tempts people
That is the starting value; both reassignments change score before it prints.
Why C tempts people
That is score after only line 2; line 3 adds another 25.
Why D tempts people
That is just the amount added, not the total. score began at 50.

86. What stays fixed: Check: running total

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.

  1. Step 1: line is 1
  2. Step 2: line is 2
  3. Step 3: line is 3

87. Check: augmented assignment

Check

Expand the += in your head.

n = 8
n += 2
n -= 3
print(n)
linen after
1?
2?
3?

Check your understanding

What does this print?

  • A. 7 (correct)
  • B. 10
  • C. 13
  • D. 5

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.

Why B tempts people
That is n after only the += 2 step. The -= 3 then lowers it to 7.
Why C tempts people
That would be 8 + 2 + 3. The second step subtracts 3, it does not add it.
Why D tempts people
That would be 8 - 3, ignoring the += 2. Both steps apply, in order.

88. What stays fixed: Check: augmented assignment

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.

  1. Step 1: line is 1
  2. Step 2: line is 2
  3. Step 3: line is 3

89. Check: which type?

Check

Look at the quotes.

x = "7"
print(type(x))
valuetype
"7"?

Check your understanding

What does type(x) print?

  • A. <class 'int'>
  • B. <class 'str'> (correct)
  • C. <class 'float'>
  • D. <class 'bool'>

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.

Why A tempts people
It would be int only without quotes: x = 7. The quotes make it text.
Why C tempts people
A float needs a decimal point and no quotes, like 7.0.
Why D tempts people
A bool is only True or False. "7" is a one-character string.

90. Check: order of operations

Check

Which operator runs first?

print(2 + 3 * 4)
expressionvalue
2 + 3 * 4?

Check your understanding

What does this print?

  • A. 14 (correct)
  • B. 20
  • C. 24
  • D. 9

Answer: A

Why: * runs before +, so 3 * 4 is 12 first, then + 2 gives 14. Verified by execution.

Why B tempts people
20 comes from doing the addition first: (2 + 3) * 4. That needs parentheses, which are not there.
Why C tempts people
24 would be 2 * 3 * 4 or (2+4)*4 - neither matches this expression.
Why D tempts people
9 ignores the multiplication entirely. 3 * 4 must be computed, not just 3 + ... .

91. Check: // versus /

Check

These look similar but differ in type.

print(9 // 2)
print(9 / 2)
expressionvalue
9 // 2?
9 / 2?

Check your understanding

What does this print, line by line?

  • A. 4 then 4.5 (correct)
  • B. 4.5 then 4.5
  • C. 4 then 4
  • D. 5 then 4.5

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.

Why B tempts people
Used / for both. // floors and drops the remainder, giving 4, not 4.5.
Why C tempts people
Assumed // and / behave the same. Only // floors; / produces 4.5.
Why D tempts people
Rounded 9 // 2 up to 5. Floor division goes down to 4.

92. Check: copy independence

Check

Does y follow x?

x = 5
y = x
x = 9
print(y)
linexy
25?
39?

Check your understanding

What does this print?

  • A. 5 (correct)
  • B. 9
  • C. 0
  • D. Error

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.

Why B tempts people
That assumes y tracks x. y was bound to the value 5, not to the name x.
Why C tempts people
y was explicitly given the value 5, not 0.
Why D tempts people
Every name is assigned before use, so there is no error.

93. What each one costs: Check: copy independence

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.

linexy
25?
39?

94. Check: even or odd

Check

Remember what % returns.

print(10 % 2)
print(11 % 2)
expressionvalue
10 % 2?
11 % 2?

Check your understanding

What does this print, line by line?

  • A. 0 then 1 (correct)
  • B. 5 then 5
  • C. 1 then 0
  • D. 0 then 0

Answer: A

Why: % is the remainder. 10 divides evenly by 2 (remainder 0 - even); 11 leaves remainder 1 (odd). Verified by execution.

Why B tempts people
That is 10 // 2 and 11 // 2 (the quotient), not the remainder. % gives what is left over.
Why C tempts people
The values are swapped: 10 is even (0), 11 is odd (1), not the other way around.
Why D tempts people
11 % 2 is 1, not 0, because 11 is odd.

95. Fill in: value for Check: even or odd

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.

expressionvalue
10 % 2?
11 % 2?

96. Connect it up: Session 2 - Variables & Types

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.

97. What you can do now

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 writeIt means
score = 100attach the name score to 100
score += 10score = score + 10
a, b = b, aswap the two values
7 // 2 , 7 % 23 (whole) , 1 (remainder)
10 / 25.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().

Sources

  1. Python 3 Tutorial - Numbers and First Steps
  2. Python 3 Library Reference - Built-in Types (int, float, str, bool)
  3. Python 3 Reference - Operator precedence
  4. All snippets and error messages executed and copied from CPython 3.12. — Author verification run, 2026-07-15 (Python Fundamentals series, Session 2).

Want this taught 1-on-1? Alexander tutors Python Fundamentals — $55/session, free consultation.

Book on Wyzant · Text (657) 465-8108