bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python/Foundations Practice
Python•Foundations Practice

Python Operator Precedence

Operator Precedence

Operator precedence describes the order in which operations are performed.

Example

Parentheses has the highest precedence, meaning that expressions inside parentheses must be evaluated first:

print((6 + 3) - (6 + 3))

Example

Multiplication

  • has higher precedence than addition +, and therefore multiplications are evaluated before additions:
print(100 + 5 * 3)

Precedence Order

The precedence order is described in the table below, starting with the highest precedence at the top:

Operator

Description

Try it

()

Parentheses

Try it » **

Exponentiation

Try it » +x -x ~x Unary plus, unary minus, and bitwise NOT Try it » * / // % Multiplication, division, floor division, and modulus Try it » +

Addition and subtraction

Try it » << >>

Bitwise left and right shifts

Try it » &

Bitwise AND

Try it » ^

Bitwise XOR

Try it » |

Bitwise OR

Try it » == != > >= < <= is is not in not in Comparisons, identity, and membership operators Try it » not

Logical NOT

Try it » and

And

Try it » or OR Try it »

Left-to-Right Evaluation

If two operators have the same precedence, the expression is evaluated from left to right.

Example

Addition

+ and subtraction

has the same precedence, and therefore we evaluate the expression from left to right:

print(5 + 4 - 7 + 3)

Previous

Python Bitwise Operators

Next

Python Operators Code Challenge