0
Given a list of integers (1-10). Write a code to calculate and display the sum of all odd numbers.(using loop)
4 Respuestas
0
print(sum(x for x in range(1, 11) if x % 2 == 1))
0
thanks Kevin Dietrichstein it did work but I'm a total newbie in python so I'd appreciate a bit more explaination.
0
first we get the list of integers from 1 to 10
lst = list(range(1,11))
now we get the odds from lst by checking every integer if theres a remainder if we divide the integer by 2
odds = []
for num in lst:
if num % 2 == 1:
odds.append(num)
now we need to sum up all odd integers from odds
result = sum(odds)
0
thanks a bunch Kevin Dietrichstein