← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 16 of 16

Python Operators

Python groups its operators into several families based on what they do: arithmetic operators do math, comparison operators test relationships between values, logical operators combine boolean conditions, and more specialized families (identity, membership, bitwise) handle less common but still important jobs. This overview maps out the whole family before diving into each one individually.

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.

Note: Remember that / always produces a float result in Python 3, even when dividing two integers evenly -- use // specifically when you need an integer (floor-divided) result.

Warning: Confusing / (true division, always returns a float) with // (floor division, rounds down to an integer) is a common source of unexpected float results.

Example: Arithmetic Operators: Doing Math

python
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.

Note: Use == for comparing values, reserving a single = strictly for assignment -- mixing these up is one of the most common syntax mistakes for beginners coming from other contexts.

Warning: Comparing values of very different, incompatible types (like a string and an integer) with < or > raises a TypeError in Python 3, unlike some looser languages.

Example: Comparison Operators: Testing Relationships

python
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.

Note: Use parentheses to group parts of a complex logical expression explicitly, even when not strictly required, to make the intended logic obvious to a reader.

Warning: Python short-circuits and/or -- if the left side of and is False, the right side is never evaluated at all, which matters if that right side has a side effect like calling a function.

Example: Logical Operators: Combining Conditions

python
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.

Note: Use compound assignment (like total += price) whenever updating a variable based on its own current value, since it is both more concise and clearer in intent than the fully spelled-out version.

Warning: total += price is exactly equivalent to total = total + price -- forgetting this equivalence can make the compound form feel more mysterious than it actually is.

Example: Assignment Operators: Storing and Updating Values

python
total = 10
total += 5
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.

Note: Use == to compare VALUES for equality, and reserve is specifically for checking whether two variables refer to the literal same object in memory -- these answer genuinely different questions.

Warning: Confusing == (value equality) with is (identity/same-object check) is a subtle but real source of bugs, especially with mutable objects like lists.

Example: Specialized Families: Identity, Membership, and Bitwise

python
x = [1, 2]
y = x
print(x is y)
print(1 in x)
print(2 & 3)
Common Mistakes
  1. 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.
  2. 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.
  3. Forgetting operator precedence and assuming operators are evaluated strictly left to right -- multiplication happens before addition, for instance, unless parentheses say otherwise.
Chapter Summary
  • 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.
Browser Support

All operator families described here are part of core Python syntax, supported identically in every Python 3 version.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.