Skip to content
  • Predict the Output · Python

Predict the Output

LearnViaNotes @learnvianotes· 5 min read

Short snippets interviewers love because the obvious answer is wrong. Predict first, then read the explanation — every output below was produced by running the code.

Mutability & names

Default argument shared between calls

Multiplying a nested list

Augmented assignment on a tuple item

Rebinding vs mutating a parameter

Scope & closures

UnboundLocalError

Closures in a loop

Class variable vs instance variable

Operators & values

Chained comparisons and precedence

Floats and rounding

and / or return operands

True is an int

Iteration & generators

Exhausted iterators

Modifying a list while iterating

Generator expression evaluation time

Exceptions & finally

finally runs even after return

The else clause of a loop

Which except catches it?

Classes

MRO in diamond inheritance

__eq__ without __hash__

Free for students and teachers

Write notes like this one

Cards for every idea, quizzes and flashcards, read aloud — on the web and your phone.

Something wrong with this note? Open it to report