What is the type of comma separated variables?

Viewed 342

I have this simple code:

>>> a, b = 1, 2
>>> a, b
(1, 2)

Does this mean that the type is tuple? In not, then why the added parenthesis?

2 Answers

You are assigning 1 to a and 2 to b.

a, b = 1, 2

If you want to check the type of a variable in Python, do:

type(a)

... which returns: int. I'd recommend you to read about data structures and types, especially the section on tuples and sequences.

However, if you would do:

a, b = 1, 2
c = (a, b)

type(c)

The output is: tuple. You could also leave out the parentheses and do c = a, b which is also treated as a tuple.

If we test the below code:

a, b = 1,2
c = a, b
print(type(c))

it will print tuple. In Python, if we put multiple values/variables separated by commas (with or without parenthesis) like above, that is considered as a tuple. That is the reason when you are trying to print the value of a, b you are getting the output in format of a tuple.

Related