Python theorytheory 0/50 · 0%
Types · easy

2. Variables and dynamic typing

Names are bound to objects; types are checked at runtime.

A Python variable is a name bound to an object; the same name can be rebound to a different type at any time, because types belong to values, not to names.

python
x = 5
x = "now a string"
x = [1, 2, 3]

Under the hood everything is an object, including integers and functions. `type(x)` reports the current binding's type, and `isinstance(x, int)` checks membership including subclasses.

Multiple assignment and tuple unpacking are idiomatic:

python
a, b = 1, 2
a, b = b, a  # swap without a temp variable

Check your understanding

  1. 1. In Python, types belong to:

  2. 2. What does `a, b = b, a` do?

  3. 3. Which function checks an object's type including subclasses?