Module 3 · Variables and Data Types — topic 4 of 15
Dynamic typing
In Python, values have types but variables don’t. Learn what dynamic typing means, how Python checks types at run time, and how to use the flexibility without creating confusing code.
Learning objectives
- Explain the difference between static and dynamic typing
- Show that a name can be rebound to a value of a different type
- Recognise type errors that happen at run time
Definition
In Python, values have types but variables don’t. Learn what dynamic typing means, how Python checks types at run time, and how to use the flexibility without creating confusing code.
Introduction
In some languages you must say what type a variable will hold, and it can never hold anything else. Python is dynamically typed: the type belongs to the value, and a name can refer to any kind of value at any time.
Why it matters
Dynamic typing is part of why Python code is short and quick to write. It also means type mistakes show up only when the faulty line runs, so understanding how Python checks types helps you write code that fails early and clearly.
Detailed explanation
Every value carries its own type: 5 is an int, "5" is a str. A variable has no fixed type; type(name) tells you the type of the value it currently refers to.
Rebinding a name to a value of a different type is allowed: data = 5 followed by data = "five" is valid.
Python is also strongly typed: it does not silently mix unrelated types. "5" + 5 raises TypeError instead of guessing what you meant.
Type checks happen when an operation runs. A function that adds its arguments works for numbers and for strings, and only fails if it receives values that can’t be added.
This style is often called duck typing: “if it walks like a duck and quacks like a duck, it’s a duck”. Python cares whether a value supports the operation, not what its declared type is.
Optional type hints (Module 17) let you document intended types for tools and readers, but Python itself does not enforce them when the program runs.
Syntax
- No type is declared.
- The same name can be rebound to another type.
- type() reports the type of the current value.
Code examples
One name, several types
int str list
Strong typing still applies
TypeError: can only concatenate str (not "int") to str
More examples
One function, many types (duck typing)
7 database [1, 2, 3] int and str can't be added
Step-by-step explanation
- combine has no type declarations; it simply uses + on whatever it receives.
- For numbers + adds, for strings it joins, and for lists it concatenates.
- Each call decides at run time what + means for those values.
- Mixing an int and a str fails only when that call runs, with a TypeError.
Expected output
Running “One function, many types (duck typing)” prints exactly:
Practical applications
- Data scripts can process numbers or text from a file with the same functions.
- Libraries accept any “file-like” object that has a read() method, a classic use of duck typing.
- Large teams add type hints and a checker such as mypy to catch type mistakes before running code.
Best practices
- Keep each variable to one type for its whole life.
- Convert input to the right type immediately.
- Add type hints in larger programs to document intent.
Common mistakes
Reusing one name for unrelated values
Avoid
Prefer
Allowed, but confusing and error-prone. Use different names for different meanings.
Assuming input is a number
Avoid
Prefer
Dynamic typing means n is whatever input() returned: a string. Multiplying a string repeats it.
Key takeaways
- Values have types; names don’t.
- Python is dynamically and strongly typed.
- Type errors appear when the faulty operation runs.
Try it yourself
- 1Call combine with two tuples and predict the result.
- 2Write a function describe(x) that prints the type name of whatever it receives, and call it with four different types.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat happens after x = 10 and then x = "ten"?