What is Dynamic Typing in Python
Python is a dynamically typed language. What is dynamic typing? Simple saying we won’t have to declare the type of variable while assigning a value to a variable in Python. It states the kind of variable in the runtime of the program. In other languages like C, C++, Java, etc., there is a strict declaration of variables before assigning values to them.
The Python interpreter does type checking only as code runs, and the type of a variable is allowed to change over its lifetime.
What it does is, It stores that value at some memory location and then binds that variable name to that memory container. And makes the contents of the container accessible through that variable name. So the data type does not matter. As it will get to know the type of the value at run-time.
In the following code, 6 is stored in the memory and binds the name x to it. After it runs, type of x will be int
. The next instruction will store ‘hello’ at some location. The memory binds name x to it. After it runs, the type of x will be str
.
>>> x = 10 >>> print(type(x)) <class 'int'> >>> x = 'hello' >>> print(type(x)) <class 'str'>