Program to add two numbers in Python

Introduction

In this section of python programming, we will learn the addition of two numbers by using arithmetic operator +.

Program to add two numbers

Let us now look into the python code.

Program

Program 1: Adding two given numbers

Python program to add two numbers

num1 = 9
num2 = 2
sum_res = num1 + num2
#Display result
print("The sum of number {0} and {1} is {2}".format(num1, num2, sum_res))

OUTPUT:

Output of Program to add two numbers

Program 2: Adding two numbers by taking user input

Program to add two numbers in python.JPG

num1 = input("Please enter the first number: ")
num2 = input("Please enter the second number: ")
sum_res = float(num1) + float(num2)
#Display result
print("The sum of number {0} and {1} is {2}".format(num1, num2, sum_res))

 

OUTPUT

Output of Program to add two numbers python

Explanation

In the above programs we are adding two numbers num1 and num2 and the sum is stored in third variable sum_res. In Program 2, we are taking the input numbers from the user through input() function and the resultant addition is stored in variable sum_res. The output is displayed by print() function.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.