In this post, we will learn how to perform arithmetic operations using Python Programming language.
This program asks the user to enter two floating point numbers, then it performs the following operations
- Addition
- Subtraction
- Multiplication
- Division
- Modulus
- Exponent
So, without further ado, let’s begin this tutorial.
Python Program to Perform Arithmetic Operations
# Python Program to Perform Arithmetic Operators a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) # Addition of two numbers sum = a + b # Subtraction of two numbers difference = a - b # Multiplication of two numbers multiplication = a * b # Division of two numbers division = a / b # Modulus of two numbers mod = a % b # Exponent exp = a ** b # Displaying output print("The sum of {0} and {1} is {2}" .format(a, b, sum)) print("The difference of {0} and {1} is {2}" .format(a, b, difference)) print("The multiplication of {0} and {1} is {2}" .format(a, b, multiplication)) print("The division of {0} and {1} is {2}" .format(a, b, division)) print("The modulus of {0} and {1} is {2}" .format(a, b, mod)) print("The exponent value of {0} and {1} is {2}" .format(a, b, exp))
Output
Enter first number: 10
Enter second number: 2
The sum of 10.0 and 2.0 is 12.0
The difference of 10.0 and 2.0 is 8.0
The multiplication of 10.0 and 2.0 is 20.0
The division of 10.0 and 2.0 is 5.0
The modulus of 10.0 and 2.0 is 0.0
The exponent value of 10.0 and 2.0 is 100.0
How Does This Program Work ?
a = float(input("Enter first number: ")) b = float(input("Enter second number: "))
In this program, the user is asked to enter two numbers. The value of these two numbers will get stored in the a and b named variable.
# Addition of two numbers sum = a + b
We perform the addition of two numbers with the help of the plus (+) operator.
# Subtraction of two numbers difference = a - b
We perform subtraction of two numbers using the minus (-) operator.
# Multiplication of two numbers multiplication = a * b
Multiplication of two numbers is done with the help of (*) operator.
# Division of two numbers division = a / b
Similarly, division is done with the help of (/) operator.
# Modulus of two numbers mod = a % b
Modulus is obtained using the (%) operator.
# Exponent exp = a ** b
Exponent of a number is obtained using (**) operator.
# Displaying output print("The sum of {0} and {1} is {2}" .format(a, b, sum)) print("The difference of {0} and {1} is {2}" .format(a, b, difference)) print("The multiplication of {0} and {1} is {2}" .format(a, b, multiplication)) print("The division of {0} and {1} is {2}" .format(a, b, division)) print("The modulus of {0} and {1} is {2}" .format(a, b, mod)) print("The exponent value of {0} and {1} is {2}" .format(a, b, exp))
Finally, we print all the results using the print() function.
Conclusion
I hope after going through this post, you understand how to perform different arithmetic operations using Python Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to guide you.
Also Read: