0

Given a list of integers (1-10). Write a code to calculate and display the sum of all odd numbers.(using loop)

3rd Oct 2018, 2:01 PM
partha
partha - avatar
4 Answers
0
print(sum(x for x in range(1, 11) if x % 2 == 1))
3rd Oct 2018, 2:07 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
0
thanks Kevin Dietrichstein it did work but I'm a total newbie in python so I'd appreciate a bit more explaination.
3rd Oct 2018, 2:13 PM
partha
partha - avatar
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)
3rd Oct 2018, 2:21 PM
Kevin Dietrichstein
Kevin Dietrichstein - avatar
0
thanks a bunch Kevin Dietrichstein
3rd Oct 2018, 2:31 PM
partha
partha - avatar