How to assign multiple values as numbers to a variable in Python?

Nested Loop

I’m curious about this. Variables can store tuple,list,dictionary,numbers,strings etc. Here in the code…

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
for element in location:
print(element)
scoops_sold += element

print(scoops_sold)

The element variable can store multiple numbers and it’s data type is int but when i try this method individually for eg.

abc = 1,2,3,4,5,6

This change the data type to tuple. How to assign multiple numbers to a variable and use the data type as Int?

Thanks alot

you can’t. Integers, floats and strings can’t hold multiple values.

Why would you want this? What advantage would multiple integers have over a container like a list?

I’m just trying to understand how the temporary variable can store multiple values? from this code…

sales_data = [[12, 17, 22], [2, 10, 3], [5, 12, 13]]
scoops_sold = 0
for location in sales_data:
for element in location:
print(element)
scoops_sold += element

The temporary variable is element in this case. Since it’s a variable, How can it store multiple integers? Just trying to deep understand the fundamental behind this method?

Thank you.

it can’t, python simply re-assign the variable with the next value from the list.

we can do this as well:

element = 12
element = 17
element = 22

So the variable will hold one integer at a time.

1 Like

Great answer. Now I have bit clear understanding. So in this case, the element variable is temporary, it’s holding one number at a time and adding the number to the scoops_sold and again adding another number until it’s complete. So i got the result as 96.

Thank you.

1 Like