Skip to main content

Python Operators Explained: Types and Precedence Rules

Python Operators Explained: Types and Precedence Rules | Rustcode

Python Operators Explained: Types and Precedence Rules

Operators are special symbols or keywords in Python that perform operations on variables and values. They’re the building blocks for expressions, letting you perform arithmetic, make comparisons, assign values, combine logic, and more. This article gives you a clear overview of operator types in Python and the all-important operator precedence rules—with code examples for every key point.


Why Understand Operators?

  • Control Logic: Operators determine how expressions are evaluated and which calculations happen first.
  • Readability: Correct use of operators makes code shorter and easier to read.
  • Bug Prevention: Understanding precedence avoids confusing logic errors.

Common Types of Operators in Python


1. Arithmetic Operators

Used for mathematical operations on numeric values.

a = 10
b = 3

print(a + b)    # 13  (addition)
print(a - b)    # 7   (subtraction)
print(a * b)    # 30  (multiplication)
print(a / b)    # 3.333... (division)
print(a // b)   # 3      (floor division)
print(a % b)    # 1      (modulus / remainder)
print(a ** b)   # 1000   (exponentiation)

Output:

13
7
30
3.3333333333333335
3
1
1000

2. Comparison Operators

Compare values and return True or False. Essential for conditions and control flow.

x, y = 5, 10

print(x == y)   # False (equal to)
print(x != y)   # True  (not equal to)
print(x < y)    # True  (less than)
print(x > y)    # False (greater than)
print(x <= y)   # True  (less than or equal to)
print(x >= y)   # False (greater than or equal to)

Output:

False
True
True
False
True
False

3. Assignment Operators

Assign and update values in variables. Combine with arithmetic for concise updates.

n = 15
n += 5   # equivalent to n = n + 5
n *= 2   # n = n * 2
n -= 8   # n = n - 8
print(n)

Output:

22
Other assignment operators include:
  • /= (divide and assign), //= (floor divide and assign)
  • %= (modulus and assign), **= (power and assign)

4. Logical Operators

Used to combine conditional statements (booleans).

a, b, c = True, False, True

print(a and b)  # False
print(a or b)   # True
print(not c)    # False

Output:

False
True
False
Explanation:
  • and: True if both operands are True
  • or: True if any operand is True
  • not: Inverts the boolean value

5. Other Useful Operators

  • Identity Operators: is, is not (compare object identities)
    
    list1 = [1,2,3]
    list2 = list1
    print(list1 is list2) # True
  • Membership Operators: in, not in
    'a' in 'cat'  # True
  • Bitwise Operators: &, |, ^, ~, <<, >>
    5 & 3  # 1  (bitwise AND)

Operator Precedence Rules

Sometimes, an expression uses multiple operators (e.g., 3 + 2 * 5). Python needs rules for which operation happens first. This is called operator precedence. Use parentheses ( ) to make the meaning explicit and override the default precedence.

Tip: Like in math, multiplication/division comes before addition/subtraction. Parentheses always have the highest precedence.
result1 = 3 + 2 * 5    # * happens first → 3 + 10 = 13
result2 = (3 + 2) * 5  # parentheses first → 5 * 5 = 25

print(result1)
print(result2)

Output:

13
25

Python Operator Precedence Table (Top = Highest)

Level Operators Description / Example
1 ( ) Parentheses (expression grouping)
2 ** Exponentiation (power), 2 ** 3 = 8
3 +, - (unary) Unary plus, minus: -a
4 * / // % Multiplication, division, floor div, modulus
5 + - (binary) Addition and subtraction
6 << >> Bitwise shifts
7 & | ^ Bitwise AND, XOR, OR
8 < > <= >= == != Comparison
9 not Logical NOT
10 and Logical AND
11 or Logical OR
12 = += -= ... Assignment operators
13 is, is not, in, not in Identity/Membership

Conclusion

Understanding operator types and their precedence in Python will help you write clear, correct, and bug-free code. Remember: when in doubt, use parentheses! They clarify the order of operations and make your intent obvious. Explore Python’s operators in your own code to truly internalize how they work.

Comments