Question 1:
To sum a list of numbers, use sum:
xs = [1, 2, 3, 4, 5]
print(sum(xs))
This outputs:
15
Question 2:
So you want (element 0 + element 1) / 2, (element 1 + element 2) / 2, ... etc.
We make two lists: one of every element except the first, and one of every element except the last. Then the averages we want are the averages of each pair taken from the two lists. We use zip to take pairs from two lists.
I assume you want to see decimals in the result, even though your input values are integers. By default, Python does integer division: it discards the remainder. To divide things through all the way, we need to use floating-point numbers. Fortunately, dividing an int by a float will produce a float, so we just use 2.0 for our divisor instead of 2.
Thus:
averages = [(x + y) / 2.0 for (x, y) in zip(my_list[:-1], my_list[1:])]
To sum a list of numbers:
sum(list_of_nums)
Generate a new list with adjacent elements averaged in xs using a list comprehension:
[(x + y) / 2 for x, y in zip(xs, xs[1:])]
Sum all those adjacent elements into a single value:
sum((x + y) / 2 for x, y in zip(xs, xs[1:]))
Hello all,
I am new to python so please be understanding. I just need help understanding how this works.
num = int(input('Entre a four digit number: '))
if num <= 999 or num >= 10000:
print ('Number was not four digits, quitting')
elif num >= 1000:
print ('The individual input of your number was: ',(num //1000,num//100%10,num//10%10,num %10))
print (sum(num//1000,num//100%10,num//10%10,num %10))The point of this little program is to get a user to input a four digit number, then output the four individual digits (input -> 1234, output -> The individual input of your number was: 1,2,3,4). I also have to print the sum, my attempt at this is on the last line. The red syntax tells me i have 4 arguements but it can only take 2, what does this mean exactly? How do I go aboout fixing this?
Sum does something like this
def sum(values, start = 0):
total = start
for value in values:
total = total + value
return total
sum([1,2],[3,4]) expands something like [3,4] + 1 + 2, which you can see tries to add numbers and lists together.
In order to use sum to produce lists, the values should be a list of lists, whereas start can be just a list. You'll see in your failing examples that the list contains at least some ints, rather then all lists.
The usual case where you might think of using sum with lists is to convert a list of lists into a list
sum([[1,2],[3,4]], []) == [1,2,3,4]
But really you shouldn't do that, as it'll be slow.
a=[[1, 20], [2, 3]]
b=[[[[[[1], 2], 3], 4], 5], 6]
sum(b, a)
This error has nothing to do with the start parameter. There are two items in the list b. One of them is [[[[[1], 2], 3], 4], 5], the other is 6, and a list and int cannot be added together.
sum(a, b)
This is adding:
[[[[[[1], 2], 3], 4], 5], 6] + [1, 20] + [2, 3]
Which works fine (as you're just adding lists to lists).
a=[1,2]
b=[3,4]
sum(a,b)
This is trying to add [3,4] + 1 + 2, which again isn't possible. Similarly, sum(b,a) is adding [1, 2] + 3 + 4.
What if the start is not a string and not an integer?
sum can't sum strings. See:
>>> sum(["a", "b"], "c")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]