Lecture Notes — Lecture 04: First Python: State and Sequences

Companion to the lecture plan and the worked examples. Read the Definitions before class, the Explanation after it; the practice questions and the lecture quiz close the loop.

Definitions

Explanation

Python as executable pseudocode. The course grammar maps line to line: READ → input(), WRITE → print(), x <- 5 → x = 5, IF/ELSE and WHILE look nearly the same. New this week is only the punctuation.

State, names, and order. Python runs top to bottom; using a name before assignment is an error, not zero. Reassignment x = x + 1 is the pseudocode x <- x + 1 exactly.

Types are safety rails. input() gives strings; "3" * 4 is '3333', not 12. Convert deliberately: int(...), float(...). When something odd happens, the first question is 'what type is this really?'

Lists as sequences. Positions start at 0; len() counts; for item in lst visits every element. The last valid index is len(lst) - 1 - remember this number, lecture 6 will show you every way to get it wrong.

First debugging tool. print() between lines is the honest first debugger: show the state you assumed, compare with reality. (The editor's debugger arrives in the lab.)

Examples

The lecture's worked examples live in examples.md; the case studies this lecture builds on are worked in class from the projector. The case sequence with difficulties:

Common misconceptions

Summary and key takeaways

  1. Predict, then run, then check - never just run.
  2. input() gives strings; convert before arithmetic.
  3. Lists start at index 0; the last index is len-1.
  4. print() the state you assumed; compare with the state you got.

Practice questions

Where this leads

Next lecture: Numbers, Remainders, and Cycles. The quiz below checks this lecture's essentials before we build on them.