In this tutorial, we’ll learn about Python arithmetic operators in detail. We’ll be explaining all of them with practical Python code examples for better understanding.
Outline
- Introduction: Python Arithmetic Operators
- 7 Arithmetic Operators In Python (Explained With Examples)
- Addition(+)
- Subtraction(-)
- Multiplication(*)
- Division(/)
- Exponentiation(**)
- Modulus(%)
- Floor Division(//)
- Python Program Implementing All 7 Operators
- Conclusion
Introduction: Python Arithmetic Operators
Arithmetic operators are used to perform mathematical operations in python. These are addition, subtraction, multiplication and division.
We’ll go through arithmetic operations one by one with proper practical code examples to better understand how they work. So let’s jump right into it.
7 Arithmetic Operators In Python (Explained With Examples)
These 7 Python arithmetic operators are listed below:
- Addition
- Subtraction
- Multiplication
- Division
- Exponentiation
- Modulus
- Floor Division
Addition(+)
We can add values using this operator. See below example:
val1=20 val2=50 result=val1+val2 print(result)
Output
70
This operator can also be used to concatenate Python strings. See below example:
v1='This is ' v2='Python' res=v1+v2 print(res)
Output
This is Python
We can also add integer with float value. See below example:
v1=20 v2=40.5 res=v1+v2 print(res)
Output
60.5
See also below example in which three values are added.
v1=20.0 v2=40.5 v3=637 res=v1 + v2 + v3 print(res)
Output
697.5
Subtraction(-)
This operator is used to subtract values. See below examples:
val1=20.0 val2=40.5 result=val1 - val2 print(result) print(20 - 56 - 10) print(15.3 - 56.0 - 20) print(52 - 10 - 20)
Output
-20.5 -46 -60.7 22
We can see through the above examples that if a higher value operand has the subtraction operator on its left side, then it’ll also be shown in the result.
Multiplication(*)
As the name suggests, this Python operator is used to multiply the values of its operands. See below examples:
a=5 b=3 result=a * b print(result) print(20 * 10) print(5 * 3.9) print(10 * 23 * 5.0)
Output
15 200 19.5 1150.0
Division(/)
This operator is used to perform the division operation. See below examples:
a=10 b=4 result=a / b print(result) print(4 / 3) print(10 / 18) print(40 / 34.2) print(5 / 10.2 / 55)
Output
2.5 1.3333333333333333 0.5555555555555556 1.1695906432748537 0.008912655971479501
Exponentiation(**)
This operator is used to specify second operand as a power to the first operand. See below examples:
a=10 b=5 res=a**b print(res) print(2**3) print(4**2**3) print(5**4.3) print(4**3**2.8)
Output
100000 8 65536 1012.9103729329762 11195966703675.746
Modulus(%)
This operator is used to fetch the remainder of values passed to it as an operands. It first divide them and then return/show its remainder. See below examples:
aa=14 bb=5 res=aa % bb print(res) print(30 % 14) print(10 % 3) print(15 % 4.6) print(54 % 10.8 % 5)
Output
4 2 1 1.200000000000001 0.7999999999999972
Floor Division(//)
This operator is used to find floor of quotient after division takes place. See below examples:
a=4 b=3 print(a/b) // simple division print(a//b) // floor division
Output
1.3333333333333333 // simple division 1 // floor division
Other examples are given below:
print(4//5) print(40//34) print(10//3.5) print(20//5.3//4) print(23//5//4)
Output
0 1 2.0 0.0 1
So this is how we can use Python arithmetic operators.
Python Program Implementing All 7 Operators
This program will implement all 7 Python arithmetic operators. It’s a simple program which will first ask the user to enter a specific symbol to select the type of operation. After that, it’ll ask the user to enter two numbers(in integer format). At last, it’ll show the result. See below code:
class AOClass: def arOperations(self,symbol,val1,val2): if symbol=='+': return val1+val2 elif symbol=='-': return val1-val2 elif symbol=='*': return val1*val2 elif symbol=='/': return val1/val2 elif symbol=='-': return val1-val2 elif symbol=='%': return val1-val2 elif symbol=='**': return val1**val2 elif symbol=='//': return val1//val2 else: return 'wrong input' sym=input(''' Choose the type of operation(Enter the symbol): Addition(+) Subtraction(-) Multiplication(*) Division(/) Exponentiation(**) Modulus(%) Floor Division(//) ''') a=int(input('Enter first value(only in integer format):')) b=int(input('Enter second value(only in integer forma):')) obj=AOClass() result=obj.arOperations(sym,a,b) print(result)
Output
Choose the type of operation(Enter the symbol): Addition(+) Subtraction(-) Multiplication(*) Division(/) Exponentiation(**) Modulus(%) Floor Division(//) ** # entered symbol Enter first value(only in integer format): 2 Enter second value(only in integer forma): 6 64 # result
So this is ho we can easily use Python arithmetic operators. Do feel free to share this post with your developer friends.
Don’t hesitate to ask if you have any questions related to Python arithmetic operators. We’ll be very glad to answer all.
Conclusion
To conclude this tutorial, hope you now have an in-depth understanding of how to use Python arithmetic operators. We’ll very glad to have your valuable feedback on this article.
Do visit our other articles on Python programming. Thank you for reading this article.