An Even Number is an integer that is divisible by 2 without leaving a remainder. For Example: 0, 2, 4, 6, 8, 10, etc.
In this article, we discuss several ways to find the sum of even numbers from 1 to n in Python. It outlines the various methods, each with an explanation.
Table of contents
1. Using a Mathematical Formula
This method directly uses a mathematical formula to derive the sum of even numbers from 1 to n in Python.
The mathematical formula to find the sum of n even numbers is:
sum_of_even_nums = k × ( k + 1 ), where k = n // 2
For Example:
n = 100
k = ( n // 2 ) = ( 50 // 2 ) = 50. Hence, k = 50sum_of_even_nums = 50 * ( 50 + 1 ) = 50 * 51 = 2550
This approach is much faster than iterating through the numbers.
Code Example
2. Using a For Loop with Modulus Operator (%)
The modulus operator (% 2 == 0) is a simple and effective way to find even numbers. The modulo operator (%) in Python returns the remainder when one number is divided by another.
Here, we use for loop to iterate through numbers from 1 to n, and for each number, it checks whether the number is divisible by 2 (i.e., i % 2 == 0). If the condition is True, then it’s an even number, and we add it.
Code Example
Output
Sum of even number from 1 to 100 is 2550
3. Using range() with a Step and sum()
In Python, you can directly generate even numbers using the range() function with a step of 2. The range() function generates a sequence of numbers and is commonly used in loops for iteration.
Syntax: range(start, stop, step)
start(Optional, default = 0) → The starting value of the sequence.stop(Required) → The sequence stops before this value.step(Optional, default = 1) → The difference between consecutive numbers.
The range(2, n + 1, 2) generates numbers starting from 2 and increases by 2 in each iteration, automatically generating even numbers.
The sum() function in Python is used to calculate the sum of all elements in an iterable (like a list, tuple, or range). It is a built-in function that makes summation simple and efficient.
This method reduces the number of iterations because it skips odd numbers entirely.
Code Example
4. Using a List Comprehension with sum()
This approach to finding the sum of even numbers between 1 and n combines list comprehension with the built-in sum() function in Python.
List comprehension is a concise way to create Python lists by applying an expression to each item in an iterable, with optional filtering using a condition.
Code Example
Explanation
range(2, n + 1, 2)generates numbers starting from2, incrementing by2, up ton.- This method uses list comprehension to directly create a list of even numbers by iterating through,
range(2, n + 1, 2). - Then, the list gets passed to the
sum()function.
5. Using Functional Programming with filter() and lambda
This approach uses functional programming techniques like filter() and lambda in Python to derive the sum of the first n even numbers.
A lambda function in Python is a small, anonymous function that can have multiple arguments but only one expression, which is evaluated and returned.
The filter() function filters elements from an iterable based on a given condition (function). It returns only the elements that satisfy the condition.
Code Example
Explanation
- The
filter()function applies a lambda function to each element in the range of 1 to n, returning only the even numbers, which are then summed using sum(). lambdafunction checks if the number is even (num % 2 == 0).range()is used to iterate from the1ton + 1.sum()is used to add all the given numbers.- This is a more functional programming-oriented approach.
Summary
Each of these methods in Python can be used based on the context or specific needs (performance, code readability, or conciseness).
The mathematical formula method seems to be the most efficient in terms of time and space complexity.
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Using a Mathematical Formula | O(1) | O(1) |
| Using a For Loop | O(n) | O(1) |
Using range() with a Step | O(n) | O(1) |
Using a List Comprehension with sum() | O(n) | O(n) |
Using Functional Programming with filter() and lambda | O(n) | O(n) |

Leave a Reply