In this example, we are going to perform arithmetical operations in python with user input.
Arithmetic operation are calculation which is performed by calculator like addition, substraction, multiplication, division. This example exmples show basic arithmetic operations listed below :
Example :
# Getting user input:
no1 = input('Enter first number: ')
no2 = input('Enter second number: ')
#Performing arithmetic operations
sum = float(no1) + float(no2)
sub = float(no1) - float(no2)
mul = float(no1) * float(no2)
div = float(no1) / float(no2)
#Printing output
print('The addition of {0} and {1} is {2}'.format(no1, no2, sum))
print('The subtraction of {0} and {1} is {2}'.format(no1, no2, sub))
print('The multiplication of {0} and {1} is {2}'.format(no1, no2, mul))
print('The division of {0} and {1} is {2}'.format(no1, no2, div))
Output :
Enter first number: 10
Enter second number: 5
The sum of 10 and 5 is 15.0
The subtraction of 10 and 5 is 5.0
The multiplication of 10 and 5 is 50.0
The division of 10 and 5 is 2.0
Explanation :
In above example we take user input using input() function then store user input into no1 and no2 variable. Then we have performed arithmetic operations on inputted values using +, -, * and / operation.Last we have printed result using print() function.
Ask anything about this examples