Python Program - Add Two Numbers
Here's a simple Python program to add two numbers:
# take input from the user num1 = float(input("Enter first number: ")) num2 = float(input("Enter second number: ")) # add the two numbers sum = num1 + num2 # display the sum print("The sum of {0} and {1} is {2}".format(num1, num2, sum))Source:www.theitroad.com
Output:
Enter first number: 5.6 Enter second number: 3.2 The sum of 5.6 and 3.2 is 8.8
In this program, we take two numbers as input from the user using the input()
function. We then convert these inputs to floating-point numbers using the float()
function.
We add the two numbers using the +
operator and store the result in a variable called sum
.
Finally, we display the result using the print()
function and format the output string using placeholders ({0}
, {1}
, and {2}
) which are replaced with num1
, num2
, and sum
, respectively.