+ 2
Why we don't use data type in python for declaring variable
19 odpowiedzi
+ 16
Mainly because Python is an interpreted language and there isn't really any need of having types.
In a compiled language, the data type of each value must be known. Variables go on the stack in a compiled language. After putting a value on the stack, the stack pointer is offset-ed by the size of the data type (or more) so that the next value can be put. If the size is not known, the stack pointer cannot be offset. Python doesn't really have a true stack, which eliminates the need of knowing types.
Also, when a data type is used, the whole code needs to be checked for type correctness before the code is executed. The basic definition of an interpreted language is that the full code is not analyzed before execution, so the types cannot be checked.
This of course, is my speculation and not from any concrete source. Anyone can feel free to correct me or add any point.
+ 11
[continued from previous answer]
The only purpose of explicitly specifying data types in python is readability, which can be achieved using type annotations. Example
```
age: int = 0
name: str = "Name"
```
Read more here:
https://docs.python.org/3/library/typing.html
+ 7
Sumit Kumar ,
there is a technique that is named "type inference", that detects automatically the datatype of a variable during declaration / initialization. if you are interested in this you can read more about at this location:
https://en.m.wikipedia.org/wiki/Type_inference
+ 5
Guido decided so.
+ 4
Sumit Kumar well..
Guido van Rossum is the inventor of Python.
And had had to decide little things and also things that characterize the language.
One of the last kind of decision was not to use data types.
So my answer was short but finally correct.
+ 3
Python uses objects as the core infrastructure. Sometimes typecasting with int for conversion from a string is necessary, however. It is similar to var declaration for generic objects in JavaScript.🤗
+ 1
Because they are reserved word and cannot be used for another function
+ 1
Mainly because Python is an interpreted language and there isn't really any need of having types.
In a compiled language, the data type of each value must be known. Variables go on the stack in a compiled language. After putting a value on the stack, the stack pointer is offset-ed by the size of the data type (or more) so that the next value can be put. If the size is not known, the stack pointer cannot be offset. Python doesn't really have a true stack, which eliminates the need of knowing types.
Also, when a data type is used, the whole code needs to be checked for type correctness before the code is executed. The basic definition of an interpreted language is that the full code is not analyzed before execution, so the types cannot be checked.
This of course, is my speculation and not from any concrete source. Anyone can feel free to correct me or add any point
The only purpose of explicitly specifying data types in python is readability, which can be achieved using type annotations. Example
```
age: int = 0
name: str = "Name"
```
Read more here:
https://docs.python.org/3/library/typing.html
+ 1
Venkatesh L
Hmm.... did you just copy-pasted my answer into the same thread where I've answered?
0
Nice answer
0
Python is strongly-typed so a declaring variable's type is unnecessary. (For obvious reasons you must usually still declare variables!) Most other languages do not behave in this way and bad things can happen because of it.
0
Python automatically finds the type of the variable and operations that can be performed on it based on the type of value it contains. Python is a dynamic language.
0
Thanks
0
Okay thanks
0
They are reserved words. So cannot be used for another propose other from what it was reserved for.
- 1
Thanks for your kind information
- 1
Thank you