Operator Precedence in Python (Priority and Order)

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z, with Operator Precedence in Python!

🖥️ Operator Precedence in Python (Operators Priority)

You now have a complete overview of the different operators in Python, but keep in mind that: When two operators share an operand, the operator with the higher precedence goes first. Since multiplication has a higher precedence than addition, “a + b * c” is treated as a + (b * c), and a * b + c is treated as (a * b) + c.

For instance, consider this following expression: 20 + 4 * 10 = 60

There is ambiguity here. Should Python perform the addition 20 + 4 first, and then multiply the sum by 10? Or should the multiplication 4 * 10 be performed first, and the addition of 20 only then?

Clearly, since the result is 60, Python has chosen the latter; if it had chosen the former, the result would be 240. This is standard algebraic procedure, found universally in virtually all programming languages.

Consequently, all operators that the language supports are assigned a precedence. In an expression, all operators of highest precedence are performed first. Once those results are obtained, operators of the next highest precedence are performed – and so on. It continues, until the expression is fully evaluated. Any operators of equal precedence are performed in left-to-right order.

Here is the order of precedence of the Python operators you have seen so far, from lowest to highest precedence:

Operator Description
or Boolean OR
and Boolean AND
not Boolean NOT
==, !=, less than, greater than, is, is not Comparisons, Identity
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
double greater than, double less than Bit shifts
+, – Addition, Subtraction
*, /, //, % Multiplication, Division, Floor division, Modulo
+x, -x, ~x Unary positive, Unary negation, Bitwise negation
** Exponentiation

Operators at the TOP of the table have the lowest precedence, and those at the bottom of the table have the highest. Any operators in the same row of the table will have an equal priority.



○ Operator Precedence with Parentheses.

Operator precedence can be overridden using parentheses. Expressions in parentheses are always performed first, before expressions that are not parenthesises: 4 * (5 + 3) – 1.

There is nothing wrong with making liberal use of parentheses, even when they are not necessary to change the order of evaluation. In fact, it is even considered good practice, because it can make the code more readable, and it relieves the reader of having to recall operator precedence from memory.

Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap