简体   繁体   中英

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

I'm a beginner learning python from Codecademy. My question is that We can create a variable with a list , tuple , string , numbers , dictionary , Boolean and etc.

Using single variable, we can add multiple numbers and strings to create a list, tuple and dictionary etc. How to create a single variable and assign multiple values of numbers? When I write a variable for example:

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

This becomes a tuple. I can't add multiples values as numbers to a variable, it changes the data type. I want the data type to be int. From the following example.

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)

In the above example, the element variable data type is int but it can store multiple values in a single variable? How's it possible in python? Just curious.

In python, variables are dynamic. If you have eg. an integer, you can transform it to another type like a tuple by assigning values of other types. But an integer is always only one value. With the last line in your code, you just add element to scoops_sold, for example if element is 2 and scoops_sold is 7, scoops_sold becomes 9. If you want to store the single elements, declare scoops_sold as a list, like scoops_sold=[].

In your example, element successively refers to multiple (nine) distinct integers. As such, it is not a variable that can hold multiple values, but rather a name that can successively refer to any object you like.

It could for instance refer to a dictionary later on in your code: element = {'a': 1} .

This is fairly typical for Python.

Python for loops are a bit different than most languages. I'm fairly new to Python as well, but I don't think it's possible to have multiple numbers assigned to 1 variable unless it's a tuple or array. What you have is a 2 dimensional array and a nested for loop to iterate through it. The outer part of the loop for location in sales_data: iterates through each array within the larger array. So it would first look at [12, 17, 22]. Then the inner for loop for element in location: iterates through that smaller array printing each individual integer separately. So essentially you don't have multiple integers assigned to one variable, you have an array of smaller arrays filled with integers assigned to one value and the loops iterates through each value.

Hope that helped!

In the code you wrote you just adding values to a single variable, the final value of scoop_sold is 12 + 17 + 22 + 2 + 10 + 3 + 5 + 12 + 13 is equal to 96 .

But the thing u want is " storing values in one value ", so you can use .append to a list

For example:

my_list = [1,2,3]
my_value = []
for element in my_list:
    my_value.append(element) 

and now in my_value you stored all variables you wanted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM