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
- In the first line, we are using input function and displaying the message “Enter First Number” in it.
- 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.
- In the second line, we repeated the above steps again to store the second integer value.
- 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.
- In the print functions, {} is the placeholder. By mentioning it, we are telling the python to print the result over there.
- .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.