Skip to content

Types and Variables

Python is dynamically typed and strongly typed. These two properties are frequently Confused, so it is worth being precise about what they mean.

  • Dynamic typing: Variables do not carry type annotations that the interpreter enforces at assignment. A name is bound to an object, and that object has a type. The same name can be rebound to an object of a completely different type at any time. Type checking happens at runtime, not at compile time.
  • Strong typing: The interpreter does not perform implicit type coercions that could silently lose data. Operations between incompatible types raise TypeError rather than silently converting one operand to match the other.
## Dynamic: the name 'x' is rebound to different types
x = 42 # int
x = "hello" # str
x = [1, 2, 3] # list
## Strong: these raise TypeError, not silent coercion
"hello" + 42 # TypeError: can only concatenate str (not "int") to str
[1, 2] + (3, 4) # TypeError: can only concatenate list (not "tuple") to list

Compare this with JavaScript (weakly and dynamically typed), where "5" + 3 silently produces "53"Or C (statically and weakly typed), where implicit conversions between int and float Occur without warning.

Python’s design prioritizes developer velocity and flexibility over static safety. Guido van Rossum designed Python for scripting, prototyping, and teaching — domains where the overhead of Type declarations is a genuine barrier. Dynamic typing enables:

  • Rapid prototyping without ceremony
  • Highly polymorphic code (duck typing)
  • Metaprogramming and runtime introspection via getattr``setattr``dirAnd type

The trade-off is that errors a compiler would catch in statically-typed languages surface at Runtime. Python mitigates this with extensive testing culture and, since Python 3.5, optional static Type checking via type hints (discussed later).

Strong typing prevents entire classes of bugs caused by silent data corruption. When "2" * 3 Produces "222" in Python, that is a deliberate, documented operation on the str type — not an Implicit coercion. The principle is that surprising implicit behavior is more dangerous than Explicit errors.