Learn what dynamic typing in Python means with simple examples. Discover how Python handles variables differently from statically typed languages like Java or C++.
video: Is Python a Dynamic Language?
Dynamic typing means that:
This is different from static typing (used in languages like Java or C++) where you must declare a variable’s type when you create it, and it can’t change.
Feature | Python (Dynamic Typing) | Java/C++ (Static Typing) |
---|---|---|
Type Declaration | Not required (x = 10 ) |
Required (int x = 10 ) |
Type Change | Allowed (x = "text" ) |
Not allowed (Compile error) |
Flexibility | High (Works with any type) | Strict (Fixed type) |
my_var = 10 # my_var is an integer
print(type(my_var)) # Output: <class 'int'>
my_var = "Hello" # Now my_var is a string
print(type(my_var)) # Output: <class 'str'>
my_var = 3.14 # Now it's a float
print(type(my_var)) # Output: <class 'float'>
Another example of variable type changes at runtime.
# Initial assignment of an integer value
x = 10
print(x) # Output: 10
print(type(x)) # Output: <class 'int'>
# Reassigning a string value to the same variable
x = "Hello, World!"
print(x) # Output: Hello, World!
print(type(x)) # Output: <class 'str'>
# Reassigning a list to the same variable
x = [1, 2, 3]
print(x) # Output: [1, 2, 3]
print(type(x)) # Output: <class 'list'>
# Reassigning a float value to the same variable
x = 3.14
print(x) # Output: 3.14
print(type(x)) # Output: <class 'float'>
In this example:
x
is initially assigned an integer value of 10
.x
is then reassigned a string value "Hello, World!"
.x
is later reassigned a list [1, 2, 3]
.x
is reassigned a float value 3.14
.Each time, the type of x
changes dynamically to match the type of the value assigned to it. This flexibility is one of the powerful features of Python, allowing for more concise and adaptable code.
# In statically typed languages you might need to do:
# int age = 25
# string name = "Alice"
# In Python, it's much simpler:
age = 25 # Python knows it's an integer
name = "Alice" # Python knows it's a string
price = 9.99 # Python knows it's a float
# Python (Dynamic)
age = 25 # Automatically an int
name = "Alice" # Automatically a str
# Java (Static)
# int age = 25; # Must declare type
# String name = "Alice";
def multiply(x):
return x * 2
print(multiply(5)) # 10 (int)
print(multiply("A")) # "AA" (str)
print(multiply([1,2])) # [1, 2, 1, 2] (list)
a = "10"
b = 5
print(a + b) # TypeError (can't add str + int)
✅ Fix: Explicitly convert types
print(int(a) + b) # 15
count = "5" # Meant to be int?
total = count + 10 # Error!
✅ Solution: Use type()
checks
if isinstance(count, int):
total = count + 10
else:
count = int(count)
x = 3.14
print(type(x)) # <class 'float'>
print(isinstance(x, float)) # True
def greet(name: str) -> str:
return "Hello, " + name
❗ Note: Type hints improve readability but don’t enforce types.
✔ Python variables do not need type declarations.
✔ The same variable can hold different types (int
, str
, list
, etc.).
✔ Dynamic typing enables flexible functions but requires care with type mismatches.
✔ Use type()
and isinstance()
for runtime type checks.
✔ Type hints (e.g., variable: int
) improve code clarity but are optional.
Dynamic typing makes Python easy for beginners while remaining powerful for advanced use cases.