Skip to content

How to add multiple numbers in Python

add multiple numbers in Python

In this tutorial we will learn how to add multiple numbers in Python . By the end of this tutorial we will learn how to add two numbers in Python , how to add three numbers in Python , how to add four numbers in Python and so on just writing a single program ..

How to add multiple numbers in Python

# How to add multiple numbers in Python
numbers = [5,8,9,4]
addition=0
for number in numbers:
    addition += number
print(addition)   
26
  • In the above code first we have declared two variable numbers and addition as [5,8,9,4] and 0 .
  • Then we have iterated each element from numbers and add the numbers with addition variable .
  • Then we have printed addition which printed 26 in the output .

How to add multiple numbers in Python taking user input

# How to add multiple numbers in Python taking user input
numbers = input("Enter the numbers separated by comma  :")
numbers = list(map(int,numbers.split()))
addition=0
for number in numbers:
    addition += number
print(addition)  
Enter the numbers separated by comma  :7 4 9
20
  • In the above code we have given input as 7 4 9 . So numbers variable is assigned with "7 4 9" .
  • Then list(map(int,numbers.split())) has converted "7 4 9" to [7,4,9]
  • After that we have iterated the list and add all the numbers .
  • Then we have printed addition variable which printed 20 in the output .

Using the above code we can add two numbers , three numbers , four numbers and so on .

How to add multiple numbers in Python using function

# How to add multiple numbers in Python using function
# defining the function
def add(*numbers):
    addition=0
    for number in numbers:
        addition += number
    print(addition)  

# calling the function
add(7,8,2)
add(3,7,9,4,2)
17
25
  • In the above code when we have passed 7,8,2 in add() function under the effect of asterisk (*) operator it becomes (7,8,2) .Then numbers variable is assigned with (7,8,2) .
  • After that we have iterated each elements of numbers and add them . Then we have printed the addition which printed 17 in the output .
  • Same as the above when we called add(3,7,9,4,2) it printed 25 in the output .

Using the above code we can add two numbers , three numbers , four numbers and so on .

How to add multiple numbers in Python using sum function

# How to add multiple numbers in Python using sum function
numbers = input("Enter the numbers separated by comma  :")
numbers = list(map(int,numbers.split()))
addition=sum(numbers)
print(addition)
Enter the numbers separated by comma  :5 6 7
18

In the above code sum(numbers) have returned sum of all the element of numbers list and stored it to addition variable . Then we have printed addition which printed 18 in the output .

Using the above code we can add two numbers , three numbers , four numbers and so on .

How to add multiple numbers in Python using class

# How to add multiple numbers in Python using class
numbers = input("Enter the numbers separated by comma  :")
numbers = list(map(int,numbers.split()))

# defining the class
class add:
    def __init__(self,numbers):
        self.addition = sum(numbers)
        
# creating object
addnumbers = add(numbers)
print(addnumbers.addition)
Enter the numbers separated by comma  :6 3 8 9 5
31
  • In the above code first we have taken user input , converted it into a list and stored it in numbers variable .
  • Then a class add is written which have a constructor which takes one parameter except self .
  • Then a object variable addition is created which is assigned with sum of numbers .
  • After that we have created an object with the numbers and printed the object variable addition which printed 31 in the output .

Using the above code we can add two numbers , three numbers , four numbers and so on .

Conclusion

Here we have seen how to add multiple numbers in Python using various ways . Now it is on you and your requirement which method is suitable for you .

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply