Add Two Numbers in Python

(Last Updated On: 16/01/2024)

In this tutorial, we will learn how to add (sum) two numbers in Python or how to find the addition of two numbers in pythonwith and without user input and display it using the print() function. First, let us begin with the sum of two numbers without user input.

Python Program to Add Two Numbers Without User Input

# Python Program to Add Two Numbers Without User Input

num1 = 15
num2 = 20

# Addition of two numbers
sum = num1 + num2

# Display the sum of two numbers
print('The sum of two number is {2}'.format(num1, num2, sum))

Output:

The sum of two number is 35

In the above program to find the addition of two numbers in python without user input, the two numbers (15, 20) are stored in the variables num1 & num2. Then the two variables num1 & num2 are added with the help of the arithmetic operator ‘+’ and the result is stored in the variable sum (sum = num1 + num2). You can use different arithmetic operators to divide (/) multiply (*), subtract ( – ), etc instead of the addition (+) Operator to explore more.

Python Program to Add(Sum) Two Numbers With User Input

# Python Program to Add Two Numbers With User Input

# Taking a number from user & Store as input numbers
num1 = input('Enter first number: ')
num2 = input('Enter second number: ')

# sum of two numbers
sum = float(num1) + float(num2)

# Display the sum of two number
print('The sum of two number is {2}'.format(num1, num2, sum))

Output:

Enter first number: 14.5
Enter second number: 8.3
The sum of two number is 22.8

Here, in the above program, we are finding the addition of two numbers in python, i.e, the user will tell the program which numbers are to be added. We take the input from the user with the help of the input() function and then those values are saved separately into two different variables num1 and num2 respectively. 

And since the input() function records input as a String, we use the float() function to convert a string into floating numbers to perform our calculation. Then the result is stored in the variable sum. And in the end, we display the results to the user with the print() function. 

Programs, where you use basic Mathematical Operators, are very important for you as a beginner because it builds a logical approach in your mind. And in python, where 99.99% of the times you will be using this logic, this simple and easy program will remind you how important it is. 

It’s like those really simple math problems of what’s 2+2 which you solved as kid, that developed your brain and skills to solve more complex problems like trigonometry, Algebra, etc at later stages of your life.

And that’s what we’ll be doing here, solving simple problems of mathematical Operations to have a firm grip over the basic concepts of programming and then we gradually increase the complexity of the problems we solve.

Slow and Steady wins the race!

There’s a lot more in Python Programming, Not just addition but more complex problems where you’ll be dealing with strings, arrays, building more complex functions, and a lot more, and we’ll be covering all of that. But first, you need to build a strong foundation, so keep practicing, and don’t lose hope! We’ll see you in the next one!

For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions

About The Author

Leave a Reply