Lesson 08
Dynamically Typed Programming
How Python assigns types at runtime and why variables can change type.
What you will learn
By the end of this lesson, you'll understand:
- What is static typing vs dynamic typing?
- How does Python assign types at runtime?
- Why can one variable store different data types?
- How do you inspect types using
type()?
What is Dynamic Typing?
A programming language decides how variables receive data types in one of two ways:
| Static Typing | Dynamic Typing |
|---|---|
| Type declared first | Type chosen from value |
| Fixed during execution | Can change at runtime |
| Compile-time checking | No declaration needed |
Python is a dynamically typed language.
Static Typing
In statically typed languages, the programmer declares the data type before using the variable.
Examples from the source
- C
- C++
- Java
- C#
Conceptual example
int age = 20
String name = "Ravi"The variable is restricted to its declared type.
Static typing concept
According to your notes, once a variable has a declared type, later assignments are generally restricted.
Dynamic Typing
In Python, you do not declare the type.
The runtime assigns the type based on the value.
x = 10Python automatically understands that x is an integer.
Later…
x = "Python"Now x becomes a string.
No type declaration changes are required.
How Python Decides the Type
Python examines the value you assign.
The value determines the type, not the variable name.
Inspecting Types with type()
Python provides the built-in type() function.
Syntax
type(value)Example
x = 100
print(type(x))Output:
<class 'int'>This is the easiest way to verify the current data type.
One Variable, Many Types
This is the primary example from your source.
x = 10
print(x, type(x))
x = 10.5
print(x, type(x))
x = "Python"
print(x, type(x))
x = [10, 20, 30]
print(x, type(x))Output
10 <class 'int'>
10.5 <class 'float'>
Python <class 'str'>
[10, 20, 30] <class 'list'>Visual timeline
The same variable name refers to different object types during execution.
Dynamic Typing with Arithmetic
Your notes also demonstrate automatic type selection during calculations.
Integer example
a = 10
b = 20
c = a + b
print(c, type(c))Output:
30 <class 'int'>Float example
a = 11.4
b = 34.56
c = a + b
print(c, type(c))Output:
45.96 <class 'float'>Arithmetic flow
Python automatically chooses the resulting type.
Why Dynamic Typing Is Useful
According to your source, dynamic typing offers two important advantages:
| Advantage | Meaning |
|---|---|
| Automatic typing | Complex values receive a type during execution |
| Flexible variables | One variable name can store different types over time |
This makes Python especially convenient for scripting and rapid development.
Practice Exercise
Create one variable named data.
Store four different values one after another.
data = 101
print(data, type(data))
data = 92.5
print(data, type(data))
data = "Ravi"
print(data, type(data))
data = [85, 90, 88]
print(data, type(data))Expected output
101 <class 'int'>
92.5 <class 'float'>
Ravi <class 'str'>
[85, 90, 88] <class 'list'>Key Points
- Python is a dynamically typed language.
- You do not declare data types before creating variables.
- Types are assigned at runtime from the stored value.
- One variable can hold an
int,float,str, orlistduring the same program. - Use
type()to inspect the current data type.
Interview Questions
- What is the difference between static typing and dynamic typing?
- Why is Python called a dynamically typed language?
- Which function shows the current type of a variable?
- Can one variable hold different data types in Python? Explain with an example.
- Name two dynamically typed languages mentioned in the source.
Summary
| Static Typing | Dynamic Typing |
|---|---|
| Type declared before use | Type assigned automatically |
| Usually fixed | Can change during execution |
| Compile-time restrictions | Runtime flexibility |
| Examples: C, C++, Java, C# | Examples: Python, JavaScript |
The core idea is simple: in Python, variables do not have fixed types—values do. The runtime determines the type automatically, and type() lets you verify what Python currently sees.
Next
Next lesson: Freeware & Open Source — what it means for Python to be free software, the role of CPython, and why the Python ecosystem is community-driven.