Variable declaration in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Variable declaration in Python

In python I found that when we declare a variable, then we do not specify the data type. Then how does the machine know what is the datatype of the variable?

7th Sep 2020, 11:58 AM
Abhijnan Saraswat Gogoi
Abhijnan Saraswat Gogoi - avatar
4 Answers
+ 4
The 'literal' of the value is used for that. If you write a bunch of digits, Python knows it's supposed to be an int. If there's a dot in it, Python knows it's a float. If there's quotation marks left and right of it, Python knows you want a string. And so on, each of the built-in types has their own distinguishable way of writing a value.
7th Sep 2020, 12:10 PM
HonFu
HonFu - avatar
+ 4
before executing, the script are broken into Token. This token has identifier of what they are x = 57 x is identifier = is assignment operator 57 is number So what ASSIGNED into IDENTIFIER x again ? Oh 57, a NUMBER
7th Sep 2020, 12:04 PM
Rei
Rei - avatar
+ 3
It has built in code that will help machine to know about data type in python. That why it is popular language.
7th Sep 2020, 12:05 PM
Sâñtôsh
Sâñtôsh - avatar
+ 2
It uses duck typing, also known as dynamic typing, which defines the data type at runtime, so user is not supposed to mention the data type himself. Interpreter itself identifies what type the variable should be. It also helps in changing the type of the variable to another during runtime
7th Sep 2020, 12:08 PM
Charitra
Charitra - avatar