In this tutorial, we’ll learn about Python operators precedence in detail. We’ll uncover this concept with practical Python code examples to better understand its role in Python programming language.
After reading this post, you’ll have a detailed understanding of how Python operator precedence works.
Outline
- Introduction: Python Operators Precedence
- Understanding Python Operators Precedence (Multiple Examples)
- Example 1: Default Python Operators Precedence
- Example 2: Precedence of same Operators used Multiple Times
- Example 3: Custom Precedence of Python Operators
- Example 4: Multiple Programs Of Python Operators Precedence
- Conclusion
Introduction: Python Operators Precedence
In Python, we can use operators to perform some specific actions like addition(+) operator can be used to add values, subtraction(-) is used to subtract values etc. But when multiple operators are used in same line then Python has pre-defined prioritization which operator should be executed first. This is called Python operators precedence.
We can also make customization to which operator should be executed first. Let’s now understand this concept with practical Python code examples.
Understanding Python Operators Precedence (Multiple Examples)
Below list will show the precedence of Python operators.
- () (Parenthesis is the first priority)
- ** (Exponentiation comes second)
- / (Division comes after exponentiation)
- * (Precedence of multiplication is less than division)
- + (Addition comes after multiplication)
- – (Finally, the subtraction is executed)
Below examples will explain Python operator precedence in detail.
Example 1: Default Python Operators Precedence
val=2-2+3*10/5 print(val)
Output
6.0
In this example, first division will take place as it has high precedence than other operators in this line. After that, multiplication will take place. Then addition will be executed as it has lesser precedence than multiplication but higher precedence than subtraction. Finally, subtraction operator will get executed as it has the least precedence.
See below example as well.
val=2-3+2*8/2**2 print(val)
Output
3.0
The order or precedence in this example is ** > / > * > + > -.
Example 2: Precedence of same Operators used Multiple Times
val=3+2-4+4 print(val)
Output
5
When same operators are used then precedence for those specific operators starts from left to right.
But in case of exponentiation, the precedence is from right to left. See below example:
val=2**3**3 // first 3**3 will be executed, then result will be used with 2 (right to left) print(val)
Output
134217728
Example 3: Custom Precedence of Python Operators
We can specify which operator should be executed first by using parenthesis (). See below code:
value=5+4-5-2 print(value) value=5+4-(5-2) print(value)
Output
2 # parenthesis not used so first addition will be executed, the subtraction 6 # parenthesis used on subtraction operator
In the above example, we’ve used tw0 examples using same operators and values but in the first one we’ve not specified any parenthesis so normal precedence will take place. But in the second example, we’ve used parenthesis with subtraction operator, so it’ll be executed first. As a result, output for both these examples are different.
If multiple parenthesis are used then precedence will start from left to right, means parenthesis of left side will execute first then it’ll move to the right direction and execute the parenthesis block one by one from left tot right. See below example:
value= 3-4-3/4*2+20+5 print(value) value= 3-4-3/(4*2)+20+5 print(value) value= (3-4)-3/4*(2+20)+5 print(value)
Output
22.5 23.625 -12.5
Example 4: Multiple Programs Of Python Operators Precedence
print(2*(4-3*4)+10/3**2) print(10+2-5) print(7*2-3) print(12*21+5-4) print(13**2-3+5-2) print(8**2-3+(5-2)+23/5) print((2**5)*3+12**4-34) print(22+4-(8+3)-23*3/5-33) print(22+4-(8+3)-23*3/(5-33))
Output
-14.88888888888889 7 11 253 169 68.6 20798 -31.8 17.464285714285715
So this is how Python operators precedence works. Click here if you want to learn more about it.
Feel free to ask if you have any questions regarding Python operators precedence. We’ll be more than happy to answer your questions.
Conclusion
To conclude this tutorial, hope you now have a proper and in-depth practical understanding of how to use and customize Python operators precedence. We’ll be looking forward to have your amazing feedback on this article.
Do visit our other posts on Python programming. Thank you for reading this article.