How do you sum odd numbers in python?

You can use the built-in sum() function like this:

num = int(input("Insert number: "))
s = sum(range(1, num+1, 2))

range() takes start (inclusive), end (exclusive), and step (In our case: start=1, end=num+1 and step=2)

Output:

>>> num = 9
>>> s = sum(range(1, num+1, 2))
>>> s
25

If using while is a requirement, then you can achieve the same result with:

>>> s = 0
>>> i = 1
>>> while i < num + 1:
...     if i % 2:  # Or: i % 2 != 0, which means we only consider odd nums
...         s += i
...     i += 1
...
>>> s
25

Python program to get input n and calculate the sum of odd numbers till n

Sample Input 1:

5

Sample Output 1:

9(1+3+5)

Program or Solution

				
			
					
n=int(input("Enter n value:"))
sum=0
for i in range(1,n+1,2):
    sum+=i
print(sum)
    

			
				
			

Program Explanation

For Statement is used to execute the sequence of instruction repeatedly.

Range() method gives list of elements, here range() method gives list which has 1,3,5... to n or n-1. for statement executes the instructions iteratively and for takes the elements one by one as value of i in sequential manner.

so it adds odd numbers.

Sum of Odd Numbers using For Loop in Python


Source Code

print("Sum of Odd Number using for loop ")

start = int(input("Enter the starting value : "))
end   = int(input("Enter the ending value : "))

result=0

for x in range(start,end):
    if(x % 2 == 1):
        result=result+x
        
print("Sum of odd number is ",result)

To download raw file Click Here

Output

Sum of Odd Number using for loop
Enter the starting value : 1
Enter the ending value : 10
Sum of odd number is  25


  • Previous
  • Next

The following article shows how given an integer list, we can produce the sum of all its odd and even digits.

Input : test_list = [345, 893, 1948, 34, 2346] 
Output : 
Odd digit sum : 36 
Even digit sum : 40 
Explanation : 3 + 5 + 9 + 3 + 1 + 9 + 3 + 3 = 36, odd summation.
Input : test_list = [345, 893] 
Output : 
Odd digit sum : 20 
Even digit sum : 12 
Explanation : 4 + 8 = 12, even summation. 

Method 1 : Using loop, str() and int()

In this, we first convert each element to string and then iterate for each of its element, and add to respective summation by conversion to integer.

Python3

test_list = [345, 893, 1948, 34, 2346]

print("The original list is : " + str(test_list))

odd_sum = 0

even_sum = 0

for sub in test_list:

    for ele in str(sub):

        if int(ele) % 2 == 0:

            even_sum += int(ele)

        else:

            odd_sum += int(ele)

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 2: Using loop and sum()

In this, we perform task of getting summation using sum(), and loop is used to perform the task of iterating through each element.

Python3

test_list = [345, 893, 1948, 34, 2346]

print("The original list is : " + str(test_list))

odd_sum = 0

even_sum = 0

for sub in test_list:

    odd_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 1])

    even_sum += sum([int(ele) for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

The original list is : [345, 893, 1948, 34, 2346]
Odd digit sum : 36
Even digit sum : 40

Method 3: Using list comprehension 

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum([int(ele)

                for sub in test_list for ele in str(sub) if int(ele) % 2 == 1])

even_sum += sum([int(ele)

                 for sub in test_list for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

Odd digit sum : 36
Even digit sum : 40

Method 4: Using the enumerate function

Python3

test_list = [345, 893, 1948, 34, 2346]

odd_sum = 0

even_sum = 0

odd_sum += sum([int(ele) for i, sub in enumerate(test_list)

                for ele in str(sub) if int(ele) % 2 == 1])

even_sum += sum([int(ele) for i, sub in enumerate(test_list)

                 for ele in str(sub) if int(ele) % 2 == 0])

print("Odd digit sum : " + str(odd_sum))

print("Even digit sum : " + str(even_sum))

Output

Odd digit sum : 36
Even digit sum : 40


How do you find the sum of even and odd numbers in python?

append(int(input())) except ValueError: print("\nInvalid Element Input! ") exit() sum = 0 count = 0 for i in range(tot): if nums[i]%2 == 0: sum = sum + nums[i] count = count+1 if count==0: print("\nEven number is not found in this list!

What is the formula for sum of odd numbers?

The total of any set of sequential odd numbers beginning with 1 is always equal to the square of the number of digits, added together. If 1,3,5,7,9,11,…, (2n-1) are the odd numbers, then; Sum of first odd number = 1. Sum of first two odd numbers = 1 + 3 = 4 (4 = 2 x 2).

How do you sum numbers in python?

Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

What is odd function python?

# Python program to check if the input number is odd or even. # A number is even if division by 2 gives a remainder of 0. # If the remainder is 1, it is an odd number. num = int(input("Enter a number: ")) if (num % 2) == 0: print("{0} is Even".format(num)) else: print("{0} is Odd".format(num)) Run Code.