In the previous tutorial, we learned to write a Python program to Add Two Numbers in different ways. In this tutorial, we will learn to write a Python program to Add Three Numbers.
Adding two, three, or more small numbers in real life is very easy without using technology but when we have multiple numbers or big numbers to add then it takes time to calculate and add those numbers by humans. Technology helps us to perform calculations in a very quick time and it gives correct results that’s why we humans believe in technology.
Program 1: Add Three Numbers
# This program adds three numbers in simple way
number1 = 10
number2 = 40.5
number3 = 7988
# Add three numbers and store result in sum variable
sum = number1 + number2 + number3
# Display the sum
print('The sum of {} + {} + {} = {}'.format(number1, number2, number3, sum))
Output
The sum of 10 + 40.5 + 7988 = 8038.5
In the above program, we simply took three variables and assigned a value to each variable. In the next statement, we simply add those three variables by using the add (+) operator and store their value in the sum variable. In the last statement, we print the value to the output screen in a formatted way.
Program 2: Add three numbers using user input
# This program adds three numbers by taking input from the user
number1 = input("Enter first number:")
number2 = input("Enter second number:")
number3 = input("Enter third number:")
# Add three numbers and store result in sum variable
sum = float(number1) + float(number2) + float(number3)
# Display the sum
print('The sum of {} + {} + {} = {}'.format(number1, number2, number3, sum))
Output
Enter first number:698
Enter second number:56
Enter third number:454.32
The sum of 698 + 56 + 454.32 = 1208.32
In the above program, we took input from the user in three different variables. After that, we add those three inputs by using the add (+) operator but before adding we convert those variables’ value into float type because whenever we take input from the user in Python then it is considered as string type and if we add those input variable without converting to number type then those numbers will be concatenated instead of addition.
Here, we have converted to float type because the user can input any number like integer or float. In the last statement, we have displayed the output on the screen by using a print statement.
Program 3: Add three numbers in Python by using the function
# Function to add three numbers
def add_three_numbers(num1, num2, num3):
result = num1 + num2 + num3
return result
# Input from the user
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: "))
num3 = float(input("Enter the third number: "))
# Call the add_three_numbers function and store the result
sum_result = add_three_numbers(num1, num2, num3)
# Display the result
print(f"The sum of {num1} + {num2} + {num3}= {sum_result}")
Output
Enter the first number: 56
Enter the second number: 365
Enter the third number: 45.25
The sum of 56.0 + 365.0 + 45.25= 466.25
In the above program, we created a function add_three_numbers and took three parameters num1, num2, and num3, and then the next statement inside the function performed an addition operation and returned the result. In the next statement, we took input from the user, converted it to float type, and called the above function by passing the value of the parameter dynamically. At last, we are displaying results in a formatted way.