Is Python statically or dynamically typed, and is it strongly or weakly typed?
Python is dynamically typed, because types are checked at runtime and a name can point to any object. It is also strongly typed, because it will not silently mix incompatible types, so "5" + 5 raises TypeError.
Explain it simply
Two separate ideas get mixed up here.
Dynamic typing: the type belongs to the value, not the variable. A name is a label you can stick on any object. In Java you declare int x; in Python, x can be an int now and a string later.
Strong typing: Python refuses to guess when types don't fit. JavaScript will turn "5" + 5 into "55"; Python stops with an error and makes you decide.
Example
x = 10
print(type(x)) # Output: <class 'int'>
x = "ten"
print(type(x)) # Output: <class 'str'>
try:
print("5" + 5)
except TypeError as e:
print("TypeError") # Output: TypeError
print(int("5") + 5) # Output: 10
print("5" * 3) # Output: 555What to say in the interview
"Python is dynamically and strongly typed. Type checking happens at runtime and variables are just names bound to objects. It's strong because there are no implicit conversions between unrelated types like str and int. Type hints are optional and only checked by external tools such as mypy."
Common mistakes
- Saying "dynamic means weak". They are different axes.