Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 08

Dynamically Typed Programming

How Python assigns types at runtime and why variables can change type.

Beginner10 min

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

text
int age = 20
String name = "Ravi"

The variable is restricted to its declared type.

Static typing concept

Declared int age rejects a string assignment

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.

python
x = 10

Python automatically understands that x is an integer.

Later…

python
x = "Python"

Now x becomes a string.

No type declaration changes are required.

How Python Decides the Type

Python examines the value you assign.

Values map to int, float, str, and list

The value determines the type, not the variable name.

Inspecting Types with type()

Python provides the built-in type() function.

Syntax

python
type(value)

Example

python
x = 100

print(type(x))

Output:

text
<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.

python
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

text
10 <class 'int'>
10.5 <class 'float'>
Python <class 'str'>
[10, 20, 30] <class 'list'>

Visual timeline

Variable x over time: int, float, str, list

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

python
a = 10
b = 20

c = a + b

print(c, type(c))

Output:

text
30 <class 'int'>

Float example

python
a = 11.4
b = 34.56

c = a + b

print(c, type(c))

Output:

text
45.96 <class 'float'>

Arithmetic flow

int plus int yields int; float plus float yields float

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.

python
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

text
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, or list during the same program.
  • Use type() to inspect the current data type.

Interview Questions

  1. What is the difference between static typing and dynamic typing?
  2. Why is Python called a dynamically typed language?
  3. Which function shows the current type of a variable?
  4. Can one variable hold different data types in Python? Explain with an example.
  5. 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.