Why Python is called Dynamically Typed?



Python is a dynamically typed language. What is dynamic? We don't have to declare the type of a variable or manage the memory while assigning a value to a variable in Python. Other languages like C, C++, Java, etc.., there is a strict declaration of variables before assigning values to them. We have to declare the kind of variable before assigning a value to it in the languages C, C++, Java, etc..,

Python don't have any problem even if we don't declare the type of variable. It states the kind of variable in the runtime of the program. Python also take cares of the memory management which is crucial in programming. So, Python is a dynamically typed language. Let's see one example.

Example

 Live Demo

## assigning a value to a variable
x = [1, 2, 3]

## x is a list here
print(type(x))

## reassigning a value to the 'x'
x = True

## x is a bool here
print(type(x))
## we can also redefine 'x' as many times as we want

Output

If you run the above program, it will generate the following results.

<class 'list'>
<class 'bool'>

As you see, we didn't declare the type of variable in the program. Python will automatically recognize the type of variable with the help of the value in the runtime.


Advertisements