In this example, we add two numbers in python with different methods.
In this method we will define two variable and make sum of them.
Example :
# Python3 program to add two numbers
n1 = 10
n2 = 5
sum = n1 + n2
print("{0} + {1} = {2}" .format(n1, n2, sum))
Output :
10 + 5 = 15
In this example, the user is first asked to enter two numbers and the input is scanned using the input() function and stored in the variables n1 and n2. Then, the variables n1 and n2 are added using the arithmetic operator + and the result is stored in the variable sum.
Example :
# Python3 program to add two user inputted numbers
n1 = input("Enter First number: ")
n2 = input("\nEnter Second number: ")
sum = float(n1) + float(n2)
print("{0} + {1} = {2}" .format(n1, n2, sum))
Output :
Enter First number: 10
Enter Second number: 20
10 + 20 = 30.0
Ask anything about this examples