Python Program To Add Two Numbers

Python program to add two numbers is provided on this page.

Python Program To Add Two Numbers

There are several ways to add two number in python.

For three such ways python programs are provided below.

Program 1

This program uses + operator perform the addition.


# This program adds two numbers
num1 = 2.8
num2 = 7.3
# Add two numbers
sum = num1 + num2
# Display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Program 2

This program also uses + operator but it accepts the numbers from user using input() function instead of writing the numbers directly in program.


# two variable to store two input numbers
first_number = input('Enter first number: ')
second_number = input('Enter second number: ')
# add two numbers
sum = float(first_number) + float(second_number)
# Display the sum
print('The sum of {0} and {1} is {2}'.format(first_number, second_number, sum))

Program 3

Here is one liner to python program to add two numbers.

This is very simple and efficient but complex way of doing addition of two numbers in python.


print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))

Related Python Programs

Which of following is invalid
Which of following commands will create
Which of following statements create

Comments