Python Programming Operators
7 Types Precedence

Python Operators Complete Guide

Learn all Python operators - arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators with practical examples and operator precedence rules.

Arithmetic

Math operations

Comparison

Relational operators

Logical

AND, OR, NOT

Bitwise

Binary operations

What are Python Operators?

Operators are special symbols in Python that perform operations on variables and values. Python provides various types of operators to perform different tasks.

Python operators reference diagram: arithmetic (+, -, *, /), comparison (==, !=, <, >), logical (and, or, not), assignment (=, +=), bitwise, membership (in), and identity (is) operators in expressions
Python operators: Symbols that combine operands—used for math, comparisons, logic, assignments, and more throughout this guide.

Key Concept

Operators work on operands. For example, in 5 + 3, + is the operator, and 5 and 3 are operands.

Basic Operator Examples
# Arithmetic operators
result = 10 + 5    # Addition
result = 10 - 5    # Subtraction
result = 10 * 5    # Multiplication
result = 10 / 3    # Division (returns float)

# Comparison operators
is_equal = (10 == 5)      # Equal to
is_greater = (10 > 5)     # Greater than
is_less = (10 < 5)        # Less than

# Logical operators
result = (10 > 5) and (3 < 7)  # AND operator
result = (10 > 5) or (3 > 7)   # OR operator
result = not (10 > 5)          # NOT operator

Python Operators Classification

Python operators are classified into 7 main categories. Each type serves a specific purpose in programming.

Complete Operators Reference Table

Operator Type Symbol/Name Description Example Result
Arithmetic + Addition - Adds two operands 5 + 3 8
Arithmetic - Subtraction - Subtracts right operand from left 10 - 4 6
Arithmetic * Multiplication - Multiplies two operands 7 * 3 21
Arithmetic / Division - Divides left operand by right (float result) 10 / 3 3.333...
Arithmetic // Floor Division - Divides and rounds down to nearest integer 10 // 3 3
Arithmetic % Modulus - Returns remainder of division 10 % 3 1
Arithmetic ** Exponentiation - Raises left operand to power of right 2 ** 3 8
Comparison == Equal to - Returns True if operands are equal 5 == 5 True
Comparison != Not equal to - Returns True if operands are not equal 5 != 3 True
Comparison > Greater than - Returns True if left is greater than right 10 > 5 True
Comparison < Less than - Returns True if left is less than right 3 < 7 True
Comparison >= Greater than or equal to 10 >= 10 True
Comparison <= Less than or equal to 5 <= 10 True
Logical and Logical AND - True if both operands are True True and False False
Logical or Logical OR - True if at least one operand is True True or False True
Logical not Logical NOT - Reverses the logical state not True False
Assignment = Assignment - Assigns right value to left variable x = 5 x becomes 5
Assignment += Add AND - Adds right to left and assigns to left x += 3 (same as x = x + 3) x increases by 3
Assignment -= Subtract AND x -= 2 x decreases by 2
Assignment *= Multiply AND x *= 4 x multiplied by 4
Assignment /= Divide AND (float division) x /= 2 x divided by 2
Assignment //= Floor Divide AND x //= 2 x floor divided by 2
Assignment %= Modulus AND x %= 3 x = x % 3
Bitwise & Bitwise AND - Sets each bit to 1 if both bits are 1 5 & 3 (101 & 011) 1 (001)
Bitwise | Bitwise OR - Sets each bit to 1 if one of bits is 1 5 | 3 (101 | 011) 7 (111)
Bitwise ^ Bitwise XOR - Sets each bit to 1 if only one bit is 1 5 ^ 3 (101 ^ 011) 6 (110)
Bitwise ~ Bitwise NOT - Inverts all bits ~5 -6
Bitwise << Left shift - Shifts bits left, fills with 0 5 << 1 (101 becomes 1010) 10
Bitwise >> Right shift - Shifts bits right 5 >> 1 (101 becomes 10) 2
Membership in Returns True if value exists in sequence 'a' in "apple" True
Membership not in Returns True if value doesn't exist in sequence 'z' not in "apple" True
Identity is Returns True if both variables point to same object x is y True/False
Identity is not Returns True if variables point to different objects x is not y True/False
Quick Tip:
  • == compares values, is compares object identity
  • and and or are logical operators, & and | are bitwise operators
  • Assignment operators (+=, -=) modify variables in place

Python Operators: Complete Theory & Tricky Examples

This section gives short examples, practical use, and tricky interview-level points for each operator category.

1) Arithmetic Operators

Arithmetic operators are used for mathematical calculations. Python follows standard precedence rules.

Arithmetic Operators with practical examples
a = 15
b = 4

print(a + b)          # 19
print(a - b)          # 11
print(a * b)          # 60
print(a / b)          # 3.75 (always float)
print(a // b)         # 3
print(-15 // 4)       # -4 (rounds down)
print(a % b)          # 3
print(a ** b)         # 50625
print(2 ** 10)        # 1024
print(9 ** 0.5)       # 3.0

# Tricky points
print(10 / 2)         # 5.0, not 5
print(2 ** 3 ** 2)    # 512 (right-associative)
print((2 ** 3) ** 2)  # 64
print(-10 % 3)        # 2 (modulus sign follows divisor)

# Practical usage
hours = 25
print(hours % 12)     # 1
temperature_c = 30
print((temperature_c * 9/5) + 32)  # 86.0

2) Comparison (Relational) Operators

Comparison operators compare values and return True or False.

Comparison operators + chaining + type behavior
x = 10
y = 20

print(x == y)   # False
print(x != y)   # True
print(x > y)    # False
print(x < y)    # True
print(x >= 10)  # True
print(x <= 10)  # True

# Chaining
age = 25
print(18 <= age <= 60)   # True
print(10 < 20 < 30)      # True
print(5 > 3 < 10)        # True

# Tricky points
print(5 == 5.0)      # True
print(True == 1)     # True
print(False == 0)    # True
print(5 == '5')      # False
# print(5 > '5')     # TypeError in Python 3

3) Logical Operators

Logical operators combine conditions. Python uses short-circuit evaluation for and and or.

Logical operators and short-circuit behavior
print(True and False)  # False
print(True or False)   # True
print(not True)        # False

age = 25
has_license = True
print(age >= 18 and has_license)  # True

def expensive_check():
    print("Checking...")
    return True

print(True or expensive_check())   # True, function not called
print(False and expensive_check()) # False, function not called

# Logical operators return values, not only booleans
print(0 or 5)   # 5
print(5 and 10) # 10
print(0 and 5)  # 0

4) Assignment Operators

Assignment operators store values and compound operators update values concisely.

Assignment, walrus, swapping, and mutability pitfalls
x = 10
x += 5
x *= 2
x //= 3
print(x)

# Walrus operator (Python 3.8+)
if (n := len([1, 2, 3])) > 2:
    print(f"Length = {n}")

# Elegant swap
a, b = 5, 10
a, b = b, a
print(a, b)  # 10 5

# Tricky mutable assignment
p = q = [1, 2, 3]
p.append(4)
print(q)  # [1, 2, 3, 4] (same object)

5) Bitwise Operators

Bitwise operators work at bit level and are useful in optimization, flags, and low-level logic.

Bitwise operations with tricky negatives
a, b = 10, 6
print(a & b)   # 2
print(a | b)   # 14
print(a ^ b)   # 12
print(~a)      # -11  (~x = -x - 1)
print(a << 1)  # 20
print(a >> 1)  # 5

# Practical even/odd check
def is_even(n):
    return (n & 1) == 0

print(is_even(4))  # True
print(is_even(5))  # False

6) Identity Operators

Identity compares object identity (memory reference), not value equality.

Identity vs equality
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)   # True (same values)
print(a is b)   # False (different objects)
print(a is c)   # True  (same object)

value = None
print(value is None)  # Best practice

7) Membership Operators

Membership checks if a value exists in sequences/collections.

Membership in list, string, and dictionary
fruits = ['apple', 'banana', 'orange']
print('apple' in fruits)      # True
print('grape' not in fruits)  # True

text = "Hello World"
print('Hello' in text)  # True
print('hello' in text)  # False (case-sensitive)

d = {'a': 1, 'b': 2}
print('a' in d)  # True (checks keys)
print(1 in d)    # False (not value)

Operator Precedence in Python

Operator precedence determines the order in which operations are performed when multiple operators are present in an expression.

Precedence Examples
# Operator Precedence Examples
# PEMDAS: Parentheses, Exponents, Multiplication/Division, Addition/Subtraction

result1 = 5 + 3 * 2   # Multiplication before addition
print(f"5 + 3 * 2 = {result1}")  # 11, not 16

result2 = (5 + 3) * 2 # Parentheses change precedence
print(f"(5 + 3) * 2 = {result2}")  # 16

result3 = 2 ** 3 * 2  # Exponentiation before multiplication
print(f"2 ** 3 * 2 = {result3}")   # 16 (8 * 2)

result4 = 2 * 3 ** 2  # Exponentiation before multiplication
print(f"2 * 3 ** 2 = {result4}")   # 18 (2 * 9)

# Complex expression
result5 = 10 + 20 * 3 / 2 - 5 % 3
print(f"10 + 20 * 3 / 2 - 5 % 3 = {result5}")
# Steps: 20*3=60, 60/2=30, 5%3=2, 10+30=40, 40-2=38

Operator Precedence Table (Highest to Lowest)

Precedence Operator Description Associativity
1 () Parentheses (grouping) Left to Right
2 ** Exponentiation Right to Left
3 +x, -x, ~x Unary plus, minus, bitwise NOT Right to Left
4 *, /, //, % Multiplication, division, floor division, modulus Left to Right
5 +, - Addition, subtraction Left to Right
6 <<, >> Bitwise shifts Left to Right
7 & Bitwise AND Left to Right
8 ^ Bitwise XOR Left to Right
9 | Bitwise OR Left to Right
10 ==, !=, >, <, >=, <= Comparison operators Left to Right
11 is, is not, in, not in Identity, membership operators Left to Right
12 not Logical NOT Right to Left
13 and Logical AND Left to Right
14 or Logical OR Left to Right
Best Practice: Use parentheses to make complex expressions clear and avoid precedence confusion. (a + b) * c is clearer than a + b * c.

Key Takeaways

  • Python has 7 types of operators: Arithmetic, Comparison, Logical, Assignment, Bitwise, Membership, Identity
  • Arithmetic operators (+ - * / // % **) perform mathematical operations
  • Comparison operators (== != > < >= <=) compare values and return Boolean
  • Logical operators (and or not) combine Boolean expressions
  • Assignment operators (= += -= *= /=) assign values to variables
  • Bitwise operators (& | ^ ~ << >>) work on binary representations
  • Membership operators (in not in) check if value exists in sequence
  • Identity operators (is is not) check if two objects are same
  • Operator precedence determines evaluation order - use parentheses for clarity