Find the Sum of Even Digits in a Number in Python

Recently, I was working on a small Python project where I needed to calculate the sum of even digits in a number. It sounded simple at first, but I realized there are several clean and efficient ways to do it in Python.

As a Python developer, I’ve often come across such tasks while cleaning numeric data or validating digit-based IDs. In this tutorial, I’ll show you how I approached this problem using Python loops, list comprehensions, functions, and recursion.

We’ll go step by step, and I’ll share practical examples that you can easily try in your own Python environment.

What Does “Sum of Even Digits” Mean?

Before we start coding, let’s quickly understand what we’re trying to achieve.

If we take a number like 48215, the even digits are 4, 8, and 2.
The sum of even digits would be:

4 + 8 + 2 = 14

That’s exactly what we need our Python program to calculate.

Method 1 – Use a Python while Loop

The most basic way to find the sum of even digits in a number is by using a while loop. This method is great for beginners who want to understand how digit extraction works in Python.

Here’s how I do it in my projects.

number = int(input("Enter a number: "))
sum_even = 0

while number > 0:
    digit = number % 10
    if digit % 2 == 0:
        sum_even += digit
    number //= 10

print("Sum of even digits:", sum_even)

I have executed the above example code and added the screenshot below.

sum of even numbers in python

This code works by repeatedly extracting the last digit of the number using the modulus operator (%). If the digit is even, it adds it to the running total. Finally, it prints the result.

This approach is simple, efficient, and easy to understand for anyone starting with Python.

Method 2 – Use a for Loop and String Conversion

Sometimes, I prefer using a for loop with string conversion because it’s shorter and more readable. Python makes it easy to iterate through each digit of a number when you convert it to a string.

Here’s how you can do it:

number = input("Enter a number: ")
sum_even = 0

for ch in number:
    digit = int(ch)
    if digit % 2 == 0:
        sum_even += digit

print("Sum of even digits:", sum_even)

I have executed the above example code and added the screenshot below.

sum of even numbers

This method is great when you’re dealing with user input or working with numeric strings. It’s also easier to debug and modify, especially for data validation tasks in Python.

Method 3 – Use Python List Comprehension and sum() Function

When I want to write compact and efficient Python code, I use list comprehensions. This method is both elegant and Pythonic.

Here’s an example:

number = input("Enter a number: ")

even_sum = sum(int(digit) for digit in number if int(digit) % 2 == 0)

print("Sum of even digits:", even_sum)

I have executed the above example code and added the screenshot below.

sum digits of a number

Here, the list comprehension filters out only the even digits, and the sum() function adds them up. This is a clean one-liner that’s perfect for quick scripts or data analysis tasks.

Method 4 – Use a Python Function

In real-world Python applications, it’s always a good idea to wrap logic inside functions. It makes your code reusable and easier to test.

Here’s how you can define a function to calculate the sum of even digits:

def sum_of_even_digits(num):
    total = 0
    while num > 0:
        digit = num % 10
        if digit % 2 == 0:
            total += digit
        num //= 10
    return total

# Example usage
number = int(input("Enter a number: "))
print("Sum of even digits:", sum_of_even_digits(number))

I have executed the above example code and added the screenshot below.

digit sum in python

This method is ideal when you plan to reuse this logic across multiple Python modules or scripts. It’s also easy to integrate into larger programs, such as financial calculators or data validation systems.

Method 5 – Use Python Recursion

If you’re comfortable with recursion, you can solve this problem elegantly using a recursive approach. This method is not the most efficient for very large numbers, but it’s a great way to understand how recursion works in Python.

# Python recursive program to find the sum of even digits

def sum_even_recursive(num):
    if num == 0:
        return 0
    digit = num % 10
    if digit % 2 == 0:
        return digit + sum_even_recursive(num // 10)
    else:
        return sum_even_recursive(num // 10)

# Example usage
number = int(input("Enter a number: "))
print("Sum of even digits:", sum_even_recursive(number))

This recursive function keeps breaking down the number until it reaches 0. At each step, it checks if the digit is even and adds it to the total sum.

Real-Life Example – Validate Employee IDs

Let’s take a more practical example. Imagine you’re working in a U.S.-based company that assigns numeric employee IDs, and you want to check if the sum of even digits in the ID meets a certain threshold for a validation rule.

Here’s how you could do it in Python:

# Python program to validate employee ID based on even digit sum

def validate_employee_id(emp_id):
    even_sum = sum(int(d) for d in str(emp_id) if int(d) % 2 == 0)
    if even_sum > 15:
        return "Valid ID"
    else:
        return "Invalid ID"

emp_id = int(input("Enter employee ID: "))
print("Result:", validate_employee_id(emp_id))

This is a practical example of how such logic can be used in real-world Python applications.
It’s simple, effective, and can easily be integrated into larger systems.

Common Mistakes to Avoid

Here are a few common mistakes I’ve seen beginners make when writing similar Python programs:

  • Forgetting to convert strings to integers before performing modulus operations.
  • Not handling negative numbers (you can use abs(num) to fix that).
  • Using recursion unnecessarily for very large numbers can cause a stack overflow.

Avoiding these mistakes will make your Python code cleaner and more reliable.

Bonus Tip – Handle Negative Numbers

If your input number can be negative, make sure to take its absolute value before processing.
Here’s a quick fix:

number = abs(int(input("Enter a number: ")))
even_sum = sum(int(d) for d in str(number) if int(d) % 2 == 0)
print("Sum of even digits:", even_sum)

This ensures your Python program works correctly for both positive and negative inputs.

Finding the sum of even digits in a number in Python is a simple yet powerful exercise that strengthens your understanding of loops, conditionals, and basic arithmetic operations.

We explored multiple approaches, from traditional loops to Pythonic one-liners and recursive methods. Each has its own advantages, and the best choice depends on your use case. I prefer the list comprehension method for its readability and efficiency. However, if you’re teaching Python to beginners, the while loop method is perfect for building foundational logic.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.