Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Demonstrate dynamic typing in Python - Python Program

Dynamic typing in Python means that the type of a variable is determined at runtime, based on the value assigned to it, rather than being explicitly declared during compilation. This allows for flexibility, as a single variable can hold values of different data types throughout the program's execution.

# Initially, 'my_variable' holds an integer value my_variable = 10 print(f"Value: {my_variable}, Type: {type(my_variable)}") # 'my_variable' is now assigned a string value my_variable = "Hello, Python!" print(f"Value: {my_variable}, Type: {type(my_variable)}") # 'my_variable' is then assigned a list my_variable = [1, 2, 3, 4] print(f"Value: {my_variable}, Type: {type(my_variable)}") # 'my_variable' is finally assigned a boolean value my_variable = True print(f"Value: {my_variable}, Type: {type(my_variable)}")

Output

 
OUTPUT  :

Value: 10, Type: <class 'int'>
Value: Hello, Python!, Type: <class 'str'>
Value: [1, 2, 3, 4], Type: <class 'list'>
Value: True, Type: <class 'bool'>

Explanation:

In this example, the variable my_variable is first assigned an integer (10), then a string ("Hello, Python!"), followed by a list ([1, 2, 3, 4]), and finally a boolean (True). At each assignment, the type() function confirms that the variable's type has dynamically changed to reflect the type of the newly assigned value, without requiring any explicit type declarations or conversions. This illustrates the core principle of dynamic typing in Python.