How to Print Sum of Two Numbers in Python

Here, we will discuss how to print sum of two numbers in python. We will see many methods to add two numbers in python. We will give two numbers num1 and num2. Python program will add these numbers using various methods.

How to Print Sum of Two Numbers in Python

This is the simplest and easiest way to print the addition program in Python. We will take two numbers while declaring the variables and find the sum of two numbers using the arithmetic operator (+). Then, the sum of numbers will be stored to sum variable. Finally, it will be displayed on the screen.

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

# python program to add two numbers

# take inputs
num1 = 5
num2 = 10

# add two numbers
sum = num1 + num2

# displaying the addition result
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 with User Input

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

Program description:- write a python program to accept two number from user, add the numbers and display it?

# python program to add two numbers with user input

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

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

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

Output for the input values test-case-1:-

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

Output for the input values test-case-2:-

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

Output for the input values test-case-3:-

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 in Python 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.

Program description:- write a program to add two numbers using function in python

# Python program to add two numbers using function

def add_num(a,b):   #user-defined function
    sum = a + b   #adding numbers
    return sum   #return value

# take input
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(num1, num2, add_num(num1, num2)))

Output for the input values test-case-1:-

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

Output for the input values test-case-2:-

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 statements.

# 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 for the input values test-case-1:-

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.

Python Program to Add Two Numbers Without using + Operator

This python program also performs the same task but in different ways. In this program, we add two numbers without using the (+) operator.

# Python program to add two numbers without using + operator

def add_num(a,b):   #user-defined function
   if a!=b:
      return (a*a-b*b)/(a-b)
   else:
      return 2*a

# take inputs
num1 = float(input('Enter first number: '))
num2 = float(input('Enter second number: '))

# calling function
sum = add_num(num1, num2)

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

Output for the input values test-case-1:-

Enter first number: 32.6
Enter second number: 20
The sum of numbers 32.6 and 20.0 is 52.6

# Python program to add two numbers without using + operator

def add_num(a,b):   #user-defined function
   while b != 0:
      c = a & b   #using and operator
      a = a ^ b   #using XOR operator
      b = c << 1
   return a

# take inputs
num1 = int(input('Enter first number: '))
num2 = int(input('Enter second number: '))

# calling function
sum = add_num(num1, num2)

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

Output for the input values test-case-1:-

Enter first number: 10
Enter second number: 30
The sum of numbers 10 and 30 is 40

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

1 thought on “How to Print Sum of Two Numbers in Python”

  1. Hey, thank you so much for making such a website you are helping so many youngsters and aspirants who are learning to code it feels like a blessing to be able to found this.

Leave a Comment

Your email address will not be published. Required fields are marked *