Python Program to Add Two Numbers – My Easy Tuts

By | May 13, 2018

We need to accept input from the user on Console and provide the result after adding the two numbers. The problem is very straightforward and easy too.

Python code to add two Numbers

num1 = int (input('Enter First Number '))
num2 = int (input('Enter Second Number '))
print('The Sum is {}'.format(num1 + num2))

Explanation of python addition code

  1. In the first line, we are using input function and displaying the message “Enter First Number” in it.
  2. By default, input function will store the data as String type. We need to convert it into integer data type by typecasting it. So, we are using (int) for it.
  3. In the second line, we repeated the above steps again to store the second integer value.
  4. In the third line, we are using print() function to display the output on the console. Instead of storing the value into the third variable. We are directly adding the two numbers while formatting the String.
  5. In the print functions, {} is the placeholder. By mentioning it, we are telling the python to print the result over there.
  6. .format(num1 + num2) is used for addition of two numbers and place the result on the String.


Python Single Line Code to add Two Numbers

print('Answer is {}'.format((int(input('Enter first Number ')))+(int(input('Enter Second Number ')))))


Explanation of the Code

In this code, we are nesting two functions. We are using input() function of Python inside print() function. Basically while formatting the String, we are asking for the values from the user. After that, we are typecasting it to Integer. Then after adding the two values, we are printing the result on the console.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.