Home > other >  Doing math with numbers in a list
Doing math with numbers in a list

Time:11-26

i want to be able to add, subtract, divide, multiply etc with integers in a list and in order.

I know you can use sum() to add, but i also want to be able to subtract, etc in order... so i tried making a for loop idk if thats the right thing to do, but it doesn't give me the right output and it really confuses me because it really seems like it should work. I was wondering if anyone knows how to fix this or explain why its not giving me the same output as i expected.

my_list = [100, 15, 3]
for i in my_list:
    i -= i
print(i)

# 100 - 15 - 3 = 82
# Wanted output: 82
# Actual output: 0



my_list = [100, 15]
for i in my_list:
    i  = i
print(i)

# 100   15 = 115
# Wanted output: 115
# Actual output: 30

CodePudding user response:

There are two main issues with your code:

  1. i can't be your loop variable and the sum, because it will be overwritten all the time. So make two variables.
  2. Your first task is different from the second. The sum is easy: take all the values of the list and add them, so the order is irrelevant. For your subtraction it's different because you have to take the first value and subtract all others, so it's basically 100-15-3, which means that also the order of the values in the list matter.

There are more elegant ways to solve it, but for the beginning this should be better to understand.

my_list = [100, 15, 3]


my_difference = my_list[0] #initialize with the first value of your list
my_list_sub = my_list[1:] #make a new list with the remaining numbers

for val in my_list_sub:
    my_difference=my_difference-val
print(my_difference)


my_list = [100, 15]
my_sum = 0 #initialize your sum with 0

for val in my_list:
    my_sum=my_sum val
print(my_sum)


CodePudding user response:

As others already pointed out: The "running"/temporary variable is overwritten in every loop. You can try this out with a simple test:

for entry in [0, 'a', 13.37]:
    print(entry) 

It's always a good idea of trying out what happens in simple cases to learn what is going on.

But your idea of solving this with a loop is absolutely fine. If you want to re-use this functionallity later, it is also nice to wrap that in a function.

Assume integer values my_values = [100, 50, 123, 51, 124, 121] in the following examples.

Lets first tacle the sum.

def calculate_sum(values: list) -> int:
    result = 0
    for entry in values:
        result  = entry
    return result

Check that it does what we want with

print(calculate_sum(my_values))
print(sum(my_values))

Now difference is 'almost' like summing up, but you want to sum up all values but the first one, and then compute the difference to the first one (a-b-c-d = a-(b c d)). Great, that we have already a method for summing up stuff, so we could simply do

def calculate_difference(values: list) -> int:
    first, *other = values
    return first - calculate_sum(other)

Note the *-marker in front of the other variable. When assigning a list two multiple variables, they are "unpacked" by python. Thus a, b, c = [0, 1, 2] would assign 0 to a and so on. However, when we do a, b = [0, 1, 2], then python complains because there are too many variables to unpack in the list (3 > 2). With the asterisk we simply tell python to put all other values, not used so far, into this special variable again. a, b, *rest = [1, 2, 3, 4, 5, 6] is also possible.


Ok, computing the product is as easy as summing up, just replace = by *= in the method. And for the quotient we can do the same as for the difference, since a * 1/b * 1/c * 1/d = a / (b*c*d). However, note that if the divisor is zero, python will raise an Error DivisionByZero, as this is not legal. Also, the result of the method is float and no longer int.

  • Related