Key Takeaways: Variables, Types, and Expressions

One-Sentence Summary

Variables are name tags that point to objects in memory, each object has a type that determines what operations are valid, and expressions combine values and operators to produce new results.

The Name Tag Model (Threshold Concept)

  WRONG mental model          RIGHT mental model
  ┌──────────┐                ┌───┐     ┌──────┐
  │ x │ 42   │                │ x │────▶│  42  │
  └──────────┘                └───┘     └──────┘
  (box holding a value)       (name tag pointing to an object)
  • Assignment (x = 42) makes a name tag point to an object
  • Reassignment (x = 99) moves the name tag — it doesn't change the old object
  • Multiple names (b = a) makes two name tags point to the same object
  • Verify with id(): if id(a) == id(b), they point to the same object

The Four Fundamental Types

Type Python Name Example Values Falsy Value Common Conversions
Integer int 42, -7, 0 0 int("42"), int(3.7)3 (truncates!)
Float float 3.14, -0.5, 1.0 0.0 float("3.14"), float(42)42.0
String str "hello", "", 'a' "" str(42)"42", str(True)"True"
Boolean bool True, False False bool(0)False, bool("")False

Arithmetic Operators

Operator Name Example Result Type of Result
+ Addition 7 + 3 10 int
- Subtraction 7 - 3 4 int
* Multiplication 7 * 3 21 int
/ True division 7 / 3 2.333... always float
// Floor division 7 // 3 2 int
% Modulo 7 % 3 1 int
** Exponentiation 2 ** 10 1024 int

Precedence: ** → unary -* / // %+ - (use parentheses when in doubt)

Comparison Operators

Operator Meaning Example Result
== Equal to 5 == 5 True
!= Not equal to 5 != 3 True
> Greater than 5 > 3 True
< Less than 5 < 3 False
>= Greater/equal 5 >= 5 True
<= Less/equal 5 <= 3 False

f-String Quick Reference

name = "Elena"
score = 90.3333

f"Hello, {name}"                   # Basic insertion
f"Score: {score:.2f}"              # 2 decimal places → "90.33"
f"Score: {score:.0f}%"             # No decimals → "90%"
f"{'Item':<15}{'Price':>10}"       # Left/right align
f"${1234.5:,.2f}"                  # Thousands + decimals → "$1,234.50"
f"{0.857:.1%}"                     # Percentage → "85.7%"

Three Most Common Beginner Bugs

Bug What Happens Fix
"Score: " + score (where score is int) TypeError Use str(score) or f-string
input() used directly in math TypeError (string, not number) Wrap in int() or float()
= vs == confusion Assignment instead of comparison Use == for comparison

Naming Conventions (PEP 8)

  • Variables: snake_casestudent_name, total_score
  • Constants: ALL_CAPSMAX_RETRIES, TAX_RATE
  • Meaningful names: midterm_score beats m, temperature_celsius beats t
  • Never use: l (lowercase L), O (uppercase O), I (uppercase I) — they look like digits

What's Next

Chapter 4: Making Decisions — Conditionals and Boolean Logic. The boolean expressions from this chapter become the building blocks for if, elif, and else statements.