Learnitweb

Is Python dynamically typed language??

Yes, Python is a dynamically typed language. Now to understand why Python is dynamically typed language, we should discuss a bit about what statically type and dynamically typed means.

In a statically typed language, type of the variable is declared at the declaration of a variable itself. For example, in Java we can declare a variable like this:

int x;

What above statements means? It means x is a variable which can hold a value of type int. Once the type of variable is declared we can not assign a value of different type to this variable. If we try to do so, it will throw error.

int x;
x="hello"; //Not allowed

Also, we can not change the type of the variable later in the program. So the following is also not allowed:

int x;
String x;

Whereas in case of dynamically typed language, there is no such restriction. For example, in case of Python following is perfectly valid:

a = 10
print(type(a))
a = 'hello'
print(type(a))
a = 15.5
print(type(a))

Output

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

As we can see, type of the variable ‘a’ is not declared. And we are able to first assign and integer, string and then float values to the same variable ‘a’.

Thus, Python is a dynamically typed language. This means that the Python interpreter does type checking only as code runs, and the type of a variable is allowed to change over its lifetime.