Quiz 2
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, False only.
  • str → string type → text sequence → "score", 'A'.
  • type(x) → returns type of value → type(3/2) is float in Python 3.

Literals and assignment

  • x = 5 → assignment statement → bind name x to value 5 → later x reads 5 until reassigned.
  • = is not equality test; equality is ==.
Mini-trace:
python
a = 10
b = a + 3
a = 2
print(b)
b becomes 13 before a changes; prints 13.

Expressions and operators

OperatorMeaningExample result
+add / concat3+4 → 7; "ab"+"c""abc"
-subtract7-2 → 5
*multiply / repeat3*4 → 12; "ha"*3"hahaha"
/true division (float)7/2 → 3.5
//floor division7//2 → 3
%remainder7%2 → 1
**power2**3 → 8
Precedence: parentheses, then **, then * / // %, then + -.
Mini-example: 2 + 3 * 42 + 1214. Not 20.

Algorithm tracing habit

Columns: line | variables after line | output.
python
x = 4
y = x * 2
x = y - 1
print(x + y)
linexynotes
14
248
378
4prints 15

Pattern families

Easy — Final value prediction

  • Straight-line assignments without branches.
  • Single print of 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.
  • print with comma-separated items vs concatenation.

Worked mini-examples

Example 1 — Division types.
python
a = 9 / 2      # 4.5 float
b = 9 // 2     # 4 int
c = 9 % 2      # 1
Example 2 — String vs int.
python
n = 7
msg = "Level " + str(n)   # "Level 7"
# "Level " + n  would error
Example 3 — Precedence.
python
result = 10 - 2 ** 3 + 1   # 10 - 8 + 1 = 3
Example 4 — Reassignment chain.
python
p = 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)

  1. What is printed?
python
x = 8
y = x // 3
x = y * 5
print(x)
  1. What is the type of 3 + 2.0? What is the type of 4 / 2?
  2. What is the value of 10 - 3 ** 2 + 1?
  3. After a = 6, b = a, a = 10, what are a and b?
  4. Write one expression that produces "Go" + "Go" using only the integer 2 and string operations (no quotes in the expression except on "Go").
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.