Why we don't use data type in python for declaring variable | Sololearn: Learn to code for FREE!

+2

Why we don't use data type in python for declaring variable

6/18/2021 11:10:56 AM

Sumit Kumar

19 Answers

New Answer

+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.

+4

Because Python is a dynamically typed language http://en.wikipedia.org/wiki/Dynamic_programming_language https://medium.com/android-news/magic-lies-here-statically-typed-vs-dynamically-typed-languages-d151c7f95e2b https://www.geeksforgeeks.org/why-python-is-called-dynamically-typed/

+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.🤗

+2

Lothar Is very correct. Python automagically recognise each datatype of it's variables before storing them in memory

+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