free stats

Python Program to Add Two Numbers

Last Update : 08 Oct, 2022 Python, Programming, Example

The best way to learn Python programming is to write programs yourself by practicing examples. 

You have given two numbers called number_1 and number_2. The task of this tutorial is to write a program for getting the sum of two numbers.

If the input number_1 = 20 and number_2 = 25. The output should be 45.

 

Adding Two Numbers without User Input

Example 01 -:

# Python program to add two numbers

# Store two numbers in variables
number_1 = 20
number_2 = 25

# Adding two numbers & store in the variable of "sum"
sum = number_1 + number_2

# printing values on the screen
print("{0} + {1} = {2}" .format(number_1, number_2, sum))

This program produces the following result -:

20 + 25 = 45

In the above program, the programmer can store two numbers in two variables. After that, the variables number_1 and number_2 are added using the arithmetic + operator. Then, the result is stored in the variable sum. Finally, this program prints the value of the variable sum on the screen using the print() function.

 

Adding Two Numbers with User Input

Example 02 -:

# Python program to add two numbers with user input

# Request the user to enter two numbers and
# Store those numbers in variables
number_1 = input("Number One: ")
number_2 = input("Number Two: ")

# Adding two numbers & store in the variable of "sum"
sum = int(number_1) + int(number_2)

# printing values on the screen
print("{0} + {1} = {2}" .format(number_1, number_2, sum))

This program produces the following result -:

Number One: 55
Number Two: 35
55 + 35 = 90

This program is prepared with a small modification of Example 01. Here,  the user is given the opportunity to enter 2 numbers of their choice using the input() function.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.