Python Program to Add Two Numbers

In this post, we will learn how to add two numbers using Python Programming language.

Python Program To Add Two Numbers

Adding Two or more numbers is a very easy task in python, you need to only know about three topics in order to write a python program to add two numbers.

  • Python Data Types
  • Python Operators 
  • Python Input and Output

We will explain all three topics by giving very easy examples which will help you to clear the concept very easily.

In the first example the input will be given, while in the second example, the program will ask users for the input and in the third example you will get to know how to write the program in one line.

So, Without wasting further time let’s get to know how to write the python program to add two numbers.

Python Program To Add Two Numbers [Pseudo-Code]

As we all know before writing any program in the code editors, it is a very good idea to write its pseudo- code first.So, let’s understand the Pseudo-code of this Program.

Step 1. Start 
Step 2. Declare Variable names Num_01, Num_02 and Sum.
Step 3. Read Values of Num_01 and Num_02.
Step 4. Add Num_01 and Num_02 and assign its value to Sum.
Step 5. Print/Display Sum.
Step 6. Stop 

Python Program To Add Two Numbers [FlowChart]

Examples

Given two numbers Num_01 and Num_02, our task is to write a python program to add these two numbers.

Sample Example

Input: Num_01 = 9, Num_02 = 6
Output: 15

Input: Num_01 = 7, Num_02 = 5
Output: 12

Python Program to Add Two Numbers When Input is Given

# Python Program to Add Two Numbers
a = 4
b = 8

# Addition of two numbers
sum = a + b

# Displaying output
print("The Sum of {0} and {1} is: {2}" .format(a, b, sum))

Output

The Sum of 4 and 8 is: 12

Python Program to Add Two Number With User Input

# Python Program to Add Two Numbers With User Input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

# Adding two numbers
sum = num1 + num2

# Displaying output
print("The sum of {0} and {1} is: {2}" .format(num1, num2, sum))

Output

The Sum of 8 and 4 is: 12

How Does This Program Work ?

Here, in the first program we have only used operators (+) to add two numbers. Since the input is already provided we don’t have to take input from the user.

In the Second Example, we have used in-built function input() to take the input form the user. When no data type is assigned to input(), it takes input as string in default.

So, to convert the string into numbers we may either use int() or float() function. Then, these numbers are added.

And in the last to display sum of these two numbers we had used print() function.

We can also write this program in a single line without using any variable as.

#Python Program To Add Two Numbers in a Single line
print('The Sum is %.1f' %(float(input('Enter the First Number: ')) + float(input('Enter the Second Number:'))))

But this program is not easily readable and is very difficult to handle, so it’s not recommended to use this method.

Python Program to Add Two Numbers Using Functions

# Python Program to Add Two Numbers Using Function
def sum(a, b):
    return a + b;

# Asking for input 
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))

# Displaying output
print("The sum of {0} and {1} is: {2}" .format(num1, num2, sum(num1,num2)))

Output

Enter the first number: 7
Enter the second number: 8
The sum of 7 and 8 is: 15

Conclusion

I hope after going through this post, you understand how to add two numbers using Python Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published.