Add Two Numbers with User Input
In the below program to add two numbers in Python, the user is first asked to enter two numbers, and the input is scanned using the Python input() function and stored in the variables number1 and number2.
Then, the variable’s number1 and number2 are added using the arithmetic operator +, and the result is stored in the variable sum.
Python3
# Python3 program to add two numbers number1 = input ( "First number: " ) number2 = input ( "\nSecond number: " ) # Adding two numbers # User might also enter float numbers sum = float (number1) + float (number2) # Display the sum # will print value in float print ( "The sum of {0} and {1} is {2}" . format (number1, number2, sum )) |
Output:
First number: 13.5 Second number: 1.54
The sum of 13.5 and 1.54 is 15.04
How to Add Two Numbers in Python – Easy Programs
Learn easy techniques and codes to add numbers in Python. Adding numbers in Python is a very easy task, and we have provided you 7 different ways to add numbers in Python.
Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers.
Examples:
Input: num1 = 5, num2 = 3
Output: 8
Input: num1 = 13, num2 = 6
Output: 19
There are many methods to add two number in Python:
- Using “+” operator
- Defining a function that adds two number
- Using operator.add method
- Using lambda function
- Using Recursive function
Each of these methods have been explained with their program below.
Contact Us