# Python Program to Add Two Numbers

We will discuss all possible methods to add two numbers in python. We will give two numbers num1 and num2. Python program will add these numbers using various methods.

Addition of Two numbers in python using arithmetic operator (+)

This is the simplest and easiest way to print the addition program in Python. We will take two numbers while declaring the variables and its addition value will be stored to sum variable, and finally it will be displayed to the screen.

Program description:- Write a Python program to add two numbers.

``````# python program to add two numbers

# take inputs
num1 = 5
num2 = 10

sum = num1 + num2

print('{0} + {1} = {2}'.format(num1, num2, sum))``````

Output:-

5 + 10 = 15

In this program, we have hardcoded the values of numbers num1 and num2 in the source code, 5 and 10 numeric values are assigned to them.

Add these numbers using the arithmetic operator (+) and display the sum of those numbers using the print() function.

In the print function, {} is the placeholder. By mentioning it, we are telling Python to print the result over there. Then, using the .format() function displays the sum of those numbers.

## Python Program to Add Two Numbers Provided by User

In the previous program, inputs are hardcoded in the program but in this program, inputs will be provided by the user.

``````# python program to add two numbers provided by the user

# store input numbers
num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')

# User might also enter float numbers
sum = float(num1) + float(num2)

# value will print in float
print('The sum of numbers {0} and {1} is {2}'.format(num1, num2, sum))``````

Output for the different input values:-

Enter First Number: 10
Enter Second Number: 8
The sum of numbers 10 and 8 is 18.0

Enter First Number: 12
Enter Second Number: 5.5
The sum of numbers 12 and 5.5 is 17.5

Enter First Number: 12.55
Enter Second Number: 8.34
The sum of numbers 12.55 and 8.34 is 20.89

In this program, inputs are scanned using the input() function and stored in variable num1 and num2.

``````num1 = input('Enter First Number: ')
num2 = input('Enter Second Number: ')``````

Since, input() returns a string, we convert the string to a number using the float() function.

``sum = float(num1) + float(num2)``

Then, the numbers num1 and num2 are added using the arithmetic operator (+) and display the sum of those numbers using print() function. The values of the sum will be printed in the float.

## Add Two Numbers using Function

We can also take the help of function to add two numbers in python. A function is a block of code that performs a specific task

``````# Python program to add two numbers using function

sum = a + b   #adding numbers
return sum   #return value

#taking input from the user
num1 = float(input('Enter first number : '))
num2 = float(input('Enter second number : '))

#function call
print('The sum of numbers {0} and {1} is {2}'.format(

Output for the different input values:-

Enter first number: 10.5
Enter second number: 5
The sum of numbers 10.5 and 5.0 is 15.5

Enter first number: 25.38
Enter second number: 12.45
The sum of numbers 25.38 and 12.45 is 37.83

In this program, we will be the first defined function. Inputs are scanned using the input() function and stored in variable num1 and num2. Then call the function and print the sum of those numbers.

## Add Two Numbers in Python Without Using Any Variables

This is a different method of adding two numbers in python. Let us see how to add two numbers in python without using any variables. This addition program is written in only one line statement.

``````# Python program to add two numbers in one line
# Without using any variables

print('The sum is %.2f' %(float(input('Enter First Number: '))
+ float(input('Enter Second Number: '))))``````

Output:-

Enter First Number: 25
Enter Second Number: 30
The sum is 55.00

In this program, we are nesting two functions. We are using the input() function of Python inside print() function. Basically while formatting the String, we are asking the value of numbers from the user. After that, we added numbers and printed the sum of those numbers.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!