In this Python tutorial, we will discuss ** how to find the sum of even digits of a number in python**.

Recently, when I was working on a Python project where I have a number value. And I need to calculate the sum of even digits of a number using a python program. So, in this tutorial, we discuss different methods to achieve this particle task.

Here is the list of methods that we will discuss.

- Find sum of even digits of a number in python using for loop & if
- Calculate sum of even digits of a number in python using while loop & if
- Find sum of even digits of a number in python using sum(), for loop & if statement

## Sum of even digits of a number in python using for loop & if

In this section, we will discuss how to find the sum of even digits of a number in python using for loop & if statement.

In this method, first, we will use the for loop to iterate over each digit given in a number. And then, we will use the IF statement in Python to determine which digit is even. And in the last, we will calculate the sum of all the even digits.

Here is an example where we utilized the for loop and IF statement to calculate the sum of all even digits.

*# Taking input from the user*
number = input("Enter a series of numbers: ")
*# Initialize a integer value*
even_sum = 0
*# Using for loop to iterate over each even number*
for element in number:
if int(element) % 2 == 0:
# Calculating sum
even_sum += int(element)
*# Printing the final result*
print("Sum of even digits: " + str(even_sum))

- In the above example, first, we are taking a number value as input from the user. After this, we are using the for loop to iterate over each digit given in a number.
- Next, we are using the IF statement to find which number is even and combine the even value to calculate the sum.

The sample execution of the above example is given below.

So, in this section, we have seen an example of how to calculate the sum of even digits in Python.

Read: Python for loop index

## Sum of even digits of a number in python using while loop & if

In this section, we will discuss how to find the sum of even digits of a number in python using the while loop & if statement.

Here we will cover a method where first we will take a number as input from the user. After this, we will use the while loop to get each digit from the series of numbers. Next, we will use the IF statement to find the even number. In the last, we calculate the sum of all the even digits.

Here is an example where we utilized the while loop and IF statement to calculate the sum of all even digits.

*# Taking input from the user*
number = int(input("Enter a series of numbers: "))
*# Initialize a integer value*
even_sum = 0
*# Using while loop & if statement*
while number > 0:
digit = number % 10
if(digit%2 == 0):
# Calculating sum
even_sum += int(digit)
number = number//10
*# Printing the final result*
print("Sum of even digits: ", str(even_sum))

Once we run the above python program, we need to enter any series of numbers. And then the program will return the sum of all the even digits from the given series of numbers.

The sample execution of the above program is given below.

So, in this section, we have seen an example of how to calculate the sum of even digits using a while loop and an if statement in Python.

Read: Python while loop continue

## Sum of even digits of a number in python using for loop, sum() & if

This section will illustrate how to calculate the sum of even digits of a number in Python using for loop, sum(), and IF statements.

Here first, we will use the for loop to iterate over each digit and then use the sum() method and IF statement to find the sum of only even digits. The sample example related to this execution in Python is given below.

*# Defining a function*
def sum_of_even_digits(num):
*# Using for loop, if, & sum()*
result = sum(int(x) for x in str(num) if x in '2468')
return result
*# Taking input for the function*
num = input("Enter a number: ")
*# Printing the final result*
print("Sum of even digits:",sum_of_even_digits (num))

In the above example, we have defined a function that uses the for loop, IF statement, and sum() method altogether to determine the sum of even digits. After this, we are taking input from the user and use the input to execute the function.

Here is the sample execution of the above python program.

So, in this section, we have covered how to calculate the sum of even digits of a number using sum(), for loop, and IF statements.

Also, take a look at some more Python tutorials.

- Function in Python
- Python Return Function
- Python Recursion
- Python zip() Function Examples
- Sum All the Items in Python List Without using sum()

## Conclusion

With the end of this Python tutorial, we have understood ** how to calculate the sum of even digits of a number** using either for loop, while loop, or sum() method in Python. Additionally, to cover each topic, we have illustrated some examples.

Here is the list of topics that we have discussed.

- Find sum of even digits of a number in python using for loop & if
- Calculate sum of even digits of a number in python using while loop & if
- Find sum of even digits of a number in python using sum(), for loop & if statement

I am a Master’s graduate currently working with TSInfo Technologies as a Cloud database developer. I love working on technologies like Azure, Python, Django, and Databases like SQL Server, MySQL, etc.