InterviewPrepKit

Home / Cheat Sheet / Algorithms & Data Structures with Python

Cheat sheet

How Python Runs Your Code

Read the full lesson →

How Python turns your text into actions, plus the core ideas every later lesson leans on.

Values and types

  • A program is instructions run in order, top to bottom.
  • Every value has a type that decides what operations do.
  • Core types: integer (42), float (3.14), string ("hi"), boolean (True/False).
  • print(...) is a function: a named action you call on a value.
  • + adds numbers but joins strings: 2 + 3 -> 5, "2" + "3" -> "23".
  • Mixing types that don’t fit raises an error, not a guess: "2" + 3 -> TypeError.

Variables

  • A variable is a name pointing at one value; create with = (“assign”, not “equals”).
  • Read score = score + 5 right-to-left: compute the right side, then rebind the name.
  • Names: lowercase words joined by underscores, describe the value, cannot start with a digit.

How a file runs

.py text  ->  parse (grammar)  ->  compile to bytecode  ->  execute (top to bottom)
                 |
                 +-- SyntaxError: stop, nothing runs
  • Parse checks grammar first; a malformed line gives SyntaxError and nothing runs.
  • Valid code compiles to bytecode, then executes one instruction at a time.
  • Runtime errors (like TypeError) surface only when the bad line is reached.

Comments

  • Text after # is ignored by Python: notes for humans.

Common pitfalls

  • = assigns, == compares (gives True/False).
  • 7 / 2 -> 3.5 (float); 7 // 2 -> 3 (whole-number division); % is the remainder.
  • Indentation is syntax, not style; inconsistent indentation is a SyntaxError.
Want the full picture? The lesson has the derivations, worked examples, and diagrams this card compresses into bullets. Read the full lesson →
Report a bug