Python Operators
In this page:
result = operand1 operator operand2
result = operator operand
Arithmetic Operators: Doing Math
Arithmetic operators -- +, -, *, /, // (floor division), % (modulo), and ** (exponentiation) -- perform mathematical calculations on numbers, and (for + specifically) also work on strings and lists to mean concatenation or repetition instead of addition.
Example: Arithmetic Operators: Doing Math
print(7 + 3, 7 - 3, 7 * 3, 7 / 3, 7 // 3, 7 % 3, 7 ** 2)
Comparison Operators: Testing Relationships
Comparison operators -- ==, !=, <, >, <=, >= -- compare two values and always produce a boolean result (True or False), forming the building blocks of every condition used in an if statement or a while loop.
Example: Comparison Operators: Testing Relationships
print(5 == 5, 5 != 3, 5 > 3, 5 < 3)
Logical Operators: Combining Conditions
Logical operators -- and, or, not -- combine multiple boolean expressions into one overall result. and requires both sides to be True, or requires at least one side to be True, and not simply inverts a single boolean value.
Example: Logical Operators: Combining Conditions
a, b = True, False
print(a and b, a or b, not a)
Assignment Operators: Storing and Updating Values
Beyond the basic = for assigning a value, Python offers compound assignment operators -- +=, -=, *=, /=, and more -- that update a variable based on its own current value in one concise step, like count += 1 to increment a counter.
Example: Assignment Operators: Storing and Updating Values
total = 10
total += 5 # compound assignment: adds 5 and stores the result back in total
print(total)
Specialized Families: Identity, Membership, and Bitwise
Beyond the everyday operator families, Python has three more specialized ones: identity operators (is, is not) check whether two variables reference the exact same object; membership operators (in, not in) check whether a value exists within a collection; and bitwise operators (&, |, ^, ~, <<, >>) manipulate individual bits of integers directly.
Example: Specialized Families: Identity, Membership, and Bitwise
x = [1, 2]
y = x # y references the same list object as x
print(x is y) # identity operator: True, same object
print(1 in x) # membership operator: True, 1 is in the list
print(2 & 3) # bitwise operator: AND on the bits of 2 and 3
- Confusing = (assignment) with == (comparison) -- a single = sets a variable's value, while == checks whether two values are equal, and mixing them up is one of the most common beginner errors.
- Assuming every operator family behaves the same way across different data types -- + means addition for numbers but concatenation for strings, and understanding this context-dependence matters.
- Forgetting operator precedence and assuming operators are evaluated strictly left to right -- multiplication happens before addition, for instance, unless parentheses say otherwise.
- Python groups operators into families: arithmetic (math), comparison (relationships), logical (combining conditions), assignment (storing values), identity, membership, and bitwise.
- Each operator family solves a different kind of problem -- arithmetic computes values, comparison and logical operators produce True/False results used in conditions.
- Operator precedence determines the order operators are evaluated in a single expression, similar to order of operations in mathematics.
All operator families described here are part of core Python syntax, supported identically in every Python 3 version.
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators