PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Programs and Examples » Python Programs for Summing Even Numbers from 1 to n

Python Programs for Summing Even Numbers from 1 to n

Updated on: March 31, 2025 | Leave a Comment

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
  • 2. Using a For Loop with Modulus Operator ( % )
  • 3. Using range() with a Step and sum()
  • 4. Using a List Comprehension with sum()
  • 5. Using Functional Programming with filter() and lambda
  • Summary

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

n = 100
k = n // 2  # here, k = 50
even_sum = k * (k + 1)
print(f'Sum of even number from 1 to {n} is {even_sum}')

# Output:
# Sum of even number from 1 to 100 is 2550Code language: Python (python)

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

n = 100
even_sum = 0
for i in range(1, n + 1):
    if i % 2 == 0:
        even_sum += i
print(f'Sum of even number from 1 to {n} is {even_sum}')Code language: Python (python)

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

n = 100
even_sum = sum(range(2, n + 1, 2))
print(f'Sum of even number from 1 to {n} is {even_sum}')

# Output:
# Sum of even number from 1 to 100 is 2550Code language: Python (python)

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

n = 100
even_sum = sum([i for i in range(2, n + 1, 2)])
print(f'Sum of even number from 1 to {n} is {even_sum}')

# Output:
# Sum of even number from 1 to 100 is 2550Code language: Python (python)

Explanation

  • range(2, n + 1, 2) generates numbers starting from 2, incrementing by 2, up to n.
  • 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

n = 100
even_sum = sum(filter(lambda x: x % 2 == 0, range(1, n + 1)))
print(f'Sum of even number from 1 to {n} is {even_sum}')

# Output:
# Sum of even number from 1 to 100 is 2550Code language: Python (python)

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().
  • lambda function checks if the number is even (num % 2 == 0).
  • range() is used to iterate from the 1 to n + 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.

MethodTime ComplexitySpace Complexity
Using a Mathematical FormulaO(1)O(1)
Using a For LoopO(n)O(1)
Using range() with a StepO(n)O(1)
Using a List Comprehension with sum()O(n)O(n)
Using Functional Programming with filter() and lambdaO(n)O(n)

Filed Under: Programs and Examples, Python, Python Basics

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

I’m Vishal Hule, the Founder of PYnative.com. As a Python developer, I enjoy assisting students, developers, and learners. Follow me on Twitter.

Related Tutorial Topics:

Programs and Examples Python Python Basics

All Coding Exercises:

C Exercises
C++ Exercises
Python Exercises

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 25+ questions
  • Each Quiz contains 25 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

In: Programs and Examples Python Python Basics
TweetF  sharein  shareP  Pin

 Explore Python

  • Python Tutorials
  • Python Exercises
  • Python Quizzes
  • Python Interview Q&A
  • Python Programs

  Python Tutorials

  • Get Started with Python
  • Python Statements
  • Python Comments
  • Python Keywords
  • Python Variables
  • Python Operators
  • Python Data Types
  • Python Casting
  • Python Control Flow statements
  • Python For Loop
  • Python While Loop
  • Python Break and Continue
  • Python Nested Loops
  • Python Input and Output
  • Python range function
  • Check user input is String or Number
  • Accept List as a input from user
  • Python Numbers
  • Python Lists
  • Python Tuples
  • Python Sets
  • Python Dictionaries
  • Python Functions
  • Python Modules
  • Python isinstance()
  • Python OOP
  • Python Inheritance
  • Python Exceptions
  • Python Exercise for Beginners
  • Python Quiz for Beginners

All Python Topics

  • Python Basics
  • Python Exercises
  • Python Quizzes
  • Python File Handling
  • Python Date and Time
  • Python OOP
  • Python Random
  • Python Regex
  • Python Pandas
  • Python Databases
  • Python MySQL
  • Python PostgreSQL
  • Python SQLite
  • Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Coding Exercises

  • C Exercises
  • C++ Exercises
  • Python Exercises

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our:

  • Terms Of Use
  • Privacy Policy
  • Cookie Policy

Copyright © 2018–2026 pynative.com