Python Program to Find Sum of Even Numbers
You can find the sum of even numbers in a list using
sum(num for num in numbers if num % 2 == 0) where numbers is your list of integers.Examples
Input[1, 2, 3, 4, 5]
Output6
Input[10, 15, 20, 25, 30]
Output60
Input[]
Output0
How to Think About It
To find the sum of even numbers, look at each number one by one and check if it divides evenly by 2 using
%. If yes, add it to a total sum. At the end, the total sum will be the sum of all even numbers.Algorithm
1
Get the list of numbers.2
Create a variable to hold the sum, starting at 0.3
For each number in the list, check if it is even using <code>number % 2 == 0</code>.4
If it is even, add it to the sum variable.5
After checking all numbers, return or print the sum.Code
python
numbers = [1, 2, 3, 4, 5] sum_even = sum(num for num in numbers if num % 2 == 0) print(sum_even)
Output
6
Dry Run
Let's trace the list [1, 2, 3, 4, 5] through the code
1
Start with sum_even = 0
sum_even = 0
2
Check 1: 1 % 2 != 0, skip
sum_even = 0
3
Check 2: 2 % 2 == 0, add 2
sum_even = 2
4
Check 3: 3 % 2 != 0, skip
sum_even = 2
5
Check 4: 4 % 2 == 0, add 4
sum_even = 6
6
Check 5: 5 % 2 != 0, skip
sum_even = 6
7
Return sum_even
6
| Number | Is Even? | Sum So Far |
|---|---|---|
| 1 | No | 0 |
| 2 | Yes | 2 |
| 3 | No | 2 |
| 4 | Yes | 6 |
| 5 | No | 6 |
Why This Works
Step 1: Check each number
We use num % 2 == 0 to find if a number is even because even numbers have no remainder when divided by 2.
Step 2: Add even numbers
Only numbers passing the even check are added to the total sum using sum().
Step 3: Return the total
After checking all numbers, the sum contains the total of all even numbers.
Alternative Approaches
Using a for loop and accumulator
python
numbers = [1, 2, 3, 4, 5] sum_even = 0 for num in numbers: if num % 2 == 0: sum_even += num print(sum_even)
This approach is more explicit and easier to understand for beginners but longer than using sum with a generator.
Using filter and sum
python
numbers = [1, 2, 3, 4, 5] sum_even = sum(filter(lambda x: x % 2 == 0, numbers)) print(sum_even)
This uses functional programming style with filter and lambda but can be less readable for new learners.
Complexity: O(n) time, O(1) space
Time Complexity
The program checks each number once, so time grows linearly with the number of elements, making it O(n).
Space Complexity
Only a few variables are used regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in O(n) time; using sum with a generator is concise and efficient, while loops and filter add readability or style differences.
| Approach | Time | Space | Best For |
|---|---|---|---|
| sum with generator | O(n) | O(1) | Concise and efficient |
| for loop with accumulator | O(n) | O(1) | Clear and beginner-friendly |
| filter and sum | O(n) | O(1) | Functional programming style |
Use
num % 2 == 0 to check if a number is even before adding it.Forgetting to check if numbers are even and summing all numbers instead.