How does Python recognize data type of a variable?

Hello Everyone.

Question: How does Python recognize data type of a variable? Like if I assign a value 1 to a variable a, how does python know this is an integer? If I try to do something like this below:

a = 1
b = "hello"
print (a+b)

Python will show me an error. Now my question is how does Python recognize what type of variable or value it is? Plus, I tried googling this question and they said that Python is a oop language so everything is an object. And it is dynamically typed so the type assigning in python happens in runtime not compile time (I even googled what is runtime and compile time).

But I still don’t understand how is any of it related to Python knowing what type of variable it is? Therefore, can someone please help me understand this with simple terms?

Thank you
Joyeta

I don’t have expert knowledge of the inner workings of Python, but from commonsensical speculation, Python complier program must have some codes that automatically determine the data type of a variable the first time a value is assigned to it but data type is not specified explicitly. And it can be done by looking at the sequence of characters that specify the value assigned to the variable. For example, when 123 is assigned to variable a, the complier program can see it consists of purely numerical characters, with no decimal point, so it can infer it’s an integer. But when 123.0 is assigned, the code would determine it is a float. If there’re quotation marks at the beginning and the end, like “123”, then the data type of variable a is set to string.

Hi!
Here data types of Python:
Intiger : 1 2 3
String : ABc, “123”
Float : 1.09
When have “” <= it means this tuple it cannot be changed
So you cannot add intiger and string
But u can convert intiger to string but not string to intiger

It’s checked when you run the code. You can’t add a string to a number in Python, it’s a strongly typed language (compare to JS, which will attempt to produce a result). When it evaluates those lines, the Python interpreter will set the type of the value of a to an integer and the the type of the value of b to a string, then the next line will error because it doesn’t make sense to add those two types.

You’re correct an object is created, and there are different object types. You could get more info with “type()”:

a = 1
b = “hello”
print(type(a))
print(type(b))

This shows that Python will know a is an int object and b is a string object

How does it determine 1 will be an int object and “hello” will be a string object? :thinking:

a is an unquoted collection of characters (containing one character), that:

  • is not a keyword (like if or in for example).
  • is a valid identifier (a collection of characters that are valid as a variable name).

So it’s a variable.

It’s followed by =, which means whatever comes next will be assigned to a.

1 is an unquoted collection of characters (containing one character) that is a valid number. It can’t be a variable because it’s not a valid identifier. It’s given the type integer because it has no decimal seperator (and doesn’t fit into any other category – it’s made up only of the digits 0-9).

Assign an integer 1 to the variable a.

New line ends the expression, the interpreter program stores that info in memory somewhere.

b is an unquoted collection of characters (containing one character), that is not a keyword, and is a valid identifier, so it’s a variable. It’s followed by =, which means whatever comes next will be assigned to b.

" denotes everything up until the next " is a string, it can’t be anything else

Assign the string hello to the variable b.

New line ends the expression, interpreter stores that info as above.

The expression a + b is evaluated. Interpreter looks up the value assigned to a: it’s 1, type integer. Look up b, it’s "hello", type string. The rules for the + operator say it can’t add an integer to a string, so the program blows up instead.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.