• Home
  • 2.6 Dynamic Typing

2.6 Dynamic Typing

View Categories

2.6 Dynamic Typing

3 min read

Python is a dynamically typed programming language. This means you do not need to declare the data type of a variable before using it.

Instead of variables having fixed data types, the value assigned to a variable determines its type. A variable can even store different types of values at different points during program execution.

This makes Python easier to write and more flexible than statically typed languages such as C or Java.


Example 1: Variable Type is Determined Automatically #

main.py

age = 25

print(age)
print(type(age))

Output #

25
<class 'int'>

Explanation #

  • The variable age is assigned the value 25.
  • Since 25 is an integer, Python automatically treats age as an integer variable.
  • No type declaration such as int age; is required.
  • The type() function displays the data type of the value stored in the variable.

Example 2: Variables Can Store Different Data Types #

main.py

name = "Alice"
salary = 45000.75
is_employee = True

print(type(name))
print(type(salary))
print(type(is_employee))

Output #

<class 'str'>
<class 'float'>
<class 'bool'>

Explanation #

  • name stores a string.
  • salary stores a floating-point number.
  • is_employee stores a Boolean value.
  • Python automatically determines the data type of each variable based on the assigned value.

Example 3: A Variable Can Change Its Type #

main.py

value = 100

print(value)
print(type(value))

value = "Python"

print(value)
print(type(value))

Output #

100
<class 'int'>
Python
<class 'str'>

Explanation #

  • Initially, value stores an integer.
  • Later, a string is assigned to the same variable.
  • Python automatically changes the type of the variable.
  • This is one of the main characteristics of dynamic typing.

Example 4: Changing from Integer to Float #

main.py

number = 25

print(type(number))

number = 25.5

print(type(number))

Output #

<class 'int'>
<class 'float'>

Explanation #

  • The first assignment stores an integer.
  • The second assignment replaces it with a floating-point number.
  • Python automatically updates the variable’s type.

Example 5: Changing from String to Boolean #

main.py

status = "Active"

print(type(status))

status = False

print(type(status))

Output #

<class 'str'>
<class 'bool'>

Explanation #

  • The variable initially stores a string.
  • Later, it stores a Boolean value.
  • Python allows the same variable to reference completely different types of objects.

Example 6: Variables Refer to Objects #

main.py

number = 10

print(number)

number = [10, 20, 30]

print(number)

Output #

10
[10, 20, 30]

Explanation #

  • Initially, number refers to an integer object.
  • Later, it refers to a list object.
  • The variable itself does not have a fixed type.
  • Instead, it refers to different objects over time.

Example 7: Dynamic Typing in Expressions #

main.py

x = 10
y = 20

result = x + y

print(result)
print(type(result))

Output #

30
<class 'int'>

Explanation #

  • Both operands are integers.
  • Therefore, the result is also an integer.
  • Python automatically determines the type of the result.

Example 8: Mixing Different Data Types #

main.py

value = 100

print(value)

value = "One Hundred"

print(value)

Output #

100
One Hundred

Explanation #

  • The same variable stores two completely different kinds of data.
  • Python allows this because variables are not restricted to a single data type.
  • While this flexibility is useful, excessive type changes can make programs harder to understand.

Example 9: Dynamic Typing with User Input #

main.py

age = input("Enter your age: ")

print(type(age))

age = int(age)

print(type(age))

Sample Input #

Enter your age: 25

Output #

<class 'str'>
<class 'int'>

Explanation #

  • input() always returns a string.
  • Initially, age stores a string.
  • int() converts the string into an integer.
  • After conversion, the variable stores an integer instead.

Example 10: Dynamic Typing vs Static Typing #

main.py

value = 50

print(value)

value = "Fifty"

print(value)

value = True

print(value)

Output #

50
Fifty
True

Explanation #

  • The variable stores an integer.
  • Then it stores a string.
  • Finally, it stores a Boolean value.
  • Such type changes are perfectly valid in Python.
  • In statically typed languages like C, Java, or C++, this would require variables of different types.

Common Mistakes #

1. Assuming Variables Have Fixed Data Types #

Incorrect Assumption

age = 25

age = "Twenty Five"

Output #

There is no error.

Reason

Python allows variables to store values of different data types.


2. Assuming input() Returns an Integer #

Incorrect

age = input("Enter age: ")

print(age + 5)

Sample Input #

25

Output #

TypeError: can only concatenate str (not "int") to str

Reason

input() always returns a string.

Correct

age = int(input("Enter age: "))

print(age + 5)

3. Changing Variable Types Unnecessarily #

Poor Practice

data = 100

data = "Python"

data = [1, 2, 3]

Reason

Although this is valid, repeatedly changing the type of a variable can reduce code readability and make debugging more difficult.


Best Practices #

  • Use meaningful variable names that describe the stored value.
  • Avoid changing a variable’s type unless there is a clear reason.
  • Keep the type of a variable consistent throughout a program whenever possible.
  • Use the type() function while learning or debugging to verify the data type of a variable.
  • Remember that variables refer to objects; they do not permanently own a data type.

Key Points to Remember #

  • Python is a dynamically typed language.
  • Variables are created automatically when a value is assigned.
  • The assigned value determines the variable’s data type.
  • A variable can store values of different data types during program execution.
  • Python automatically changes the variable’s type when a new value is assigned.
  • The type() function is used to determine the data type of a value.
  • Dynamic typing makes Python flexible and easy to write, but unnecessary type changes should be avoided for better code readability.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *