Neural Sync Active
python-week1
Registry Synced
python-week1
752 words
4 min read
Reading compass
Now · Week map
First-week Python is about predictable state change: what each line does to memory, and which types allow which operations.
Week map
Algorithm as ordered steps → variables and assignment → built-in types → expressions vs statements → operator precedence → tracing print output.
Type notation
int→ integer type → whole numbers →17,-4,0.float→ floating type → decimal approximations →3.0,-0.25,2.718.bool→ boolean type → truth values →True,Falseonly.str→ string type → text sequence →"score",'A'.type(x)→ returns type of value →type(3/2)isfloatin Python 3.
Literals and assignment
x = 5→ assignment statement → bind namexto value 5 → laterxreads 5 until reassigned.=is not equality test; equality is==.
Mini-trace:
pythona = 10 b = a + 3 a = 2 print(b)
b becomes 13 before a changes; prints 13.Expressions and operators
| Operator | Meaning | Example result |
|---|---|---|
+ | add / concat | 3+4 → 7; "ab"+"c" → "abc" |
- | subtract | 7-2 → 5 |
* | multiply / repeat | 3*4 → 12; "ha"*3 → "hahaha" |
/ | true division (float) | 7/2 → 3.5 |
// | floor division | 7//2 → 3 |
% | remainder | 7%2 → 1 |
** | power | 2**3 → 8 |
Precedence: parentheses, then
**, then * / // %, then + -.Mini-example:
2 + 3 * 4 → 2 + 12 → 14. Not 20.Algorithm tracing habit
Columns: line | variables after line | output.
pythonx = 4 y = x * 2 x = y - 1 print(x + y)
| line | x | y | notes |
|---|---|---|---|
| 1 | 4 | — | |
| 2 | 4 | 8 | |
| 3 | 7 | 8 | |
| 4 | prints 15 |
Pattern families
Easy — Final value prediction
- Straight-line assignments without branches.
- Single
printof expression or variable. - Identify type of literal or simple expression.
Medium — Mixed-type expressions
- Division always float in Python 3.
- Floor division and mod with negatives (know course convention; often positive mod for quiz).
- String
+vs numeric+— types must match for numeric add.
Hard — Multi-variable dependency chains
- Reassignment overwrites; old value gone unless another name still references it (week 1 usually one name per value).
- Expression on RHS fully evaluated before LHS update.
printwith comma-separated items vs concatenation.
Worked mini-examples
Example 1 — Division types.
pythona = 9 / 2 # 4.5 float b = 9 // 2 # 4 int c = 9 % 2 # 1
Example 2 — String vs int.
pythonn = 7 msg = "Level " + str(n) # "Level 7" # "Level " + n would error
Example 3 — Precedence.
pythonresult = 10 - 2 ** 3 + 1 # 10 - 8 + 1 = 3
Example 4 — Reassignment chain.
pythonp = 1 q = p + 4 # q = 5 p = q # p = 5 q = p + 2 # q = 7 # p is 5, q is 7
Traps
- Using
=in a condition (week 2 topic, but appears early in distractors). - Assuming
/gives integer quotient in Python 3. - Concatenating str with int without
str(). - Reading code bottom-up when state evolves top-down.
- Confusing
//with/.
Diagnostic (try yourself)
- What is printed?
pythonx = 8 y = x // 3 x = y * 5 print(x)
-
What is the type of
3 + 2.0? What is the type of4 / 2? -
What is the value of
10 - 3 ** 2 + 1? -
After
a = 6,b = a,a = 10, what areaandb? -
Write one expression that produces
"Go" + "Go"using only the integer2and string operations (no quotes in the expression except on"Go").