Here’s a straightforward way to add even and odd numbers separately in Python:
# Define a list of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Initialize sums for even and odd numbers
sum_even = 0
sum_odd = 0# Loop through each number in the list
for number in numbers:
# Check if the number is even
if number % 2 == 0:
sum_even += number # Add to even sum
else:
sum_odd += number # Add to odd sum
# Print the results
print("The sum of even numbers is:", sum_even)
print("The sum of odd numbers is:", sum_odd)
Hello, We’re content writer who is fascinated by content fashion, celebrity and lifestyle. We helps clients bring the right content to the right people.
Leave a Reply