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(): ifid(a) == id(b)(ora is b), they point to the same object ==asks "same value?";isasks "same object?" Useisonly for checks likevalue is None
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 (rounds down: -7 // 2 is -4) |
% |
Modulo | 7 % 3 |
1 |
int |
** |
Exponentiation | 2 ** 10 |
1024 |
int |
The "Type of Result" column assumes both operands are ints. If either one is a float, every operator gives a float (7.0 // 2 is 3.0).
Precedence: ** → unary - → * / // % → + - (so -3 ** 2 is -9; use parentheses when in doubt)
Augmented assignment: total += 5 is shorthand for total = total + 5; the same pattern works for -=, *=, /=, //=, %=, **=.
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 = "Imani"
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 |
if x = 5: is a SyntaxError |
Use = to assign, == to compare |
Naming Conventions (PEP 8)
- Variables:
snake_case→student_name,total_score - Constants:
ALL_CAPS→MAX_RETRIES,TAX_RATE - Meaningful names:
midterm_scorebeatsm,temperature_celsiusbeatst - 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.