Operators

Difficulty: Beginner

Operators are special symbols that perform operations on values and variables. Python provides a rich set of operators organized into several categories: arithmetic, comparison, logical, and assignment operators. Understanding these is fundamental to writing any Python program.

Arithmetic operators perform mathematical calculations. Python supports the standard operators: + (addition), - (subtraction), * (multiplication), / (division), // (floor division), % (modulus/remainder), and (exponentiation). One important distinction is between / and //: the / operator always returns a float, while // returns the largest integer less than or equal to the result.

Comparison operators compare two values and return a boolean (True or False). They include == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). These are essential for conditional logic and loops. Python also supports chained comparisons like 1 < x < 10, which is equivalent to 1 < x and x < 10.

Logical operators combine boolean expressions. Python uses the English words and, or, and not instead of symbols like && and ||. The and operator returns True only when both operands are True, or returns True when at least one operand is True, and not negates a boolean value. Python uses short-circuit evaluation: in an and expression, if the first operand is False, the second is never evaluated.

Assignment operators provide shorthand for updating variables. Beyond the basic = operator, Python supports augmented assignment operators like +=, -=, *=, /=, //=, %=, and =. These modify a variable in place. For example, x += 5 is equivalent to x = x + 5. Note that Python does not have ++ or -- increment/decrement operators like C or Java.

Code examples

Arithmetic Operators

a = 17
b = 5

print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a // b)
print(a % b)
print(a  b)

Demonstrates all 7 arithmetic operators. Note that / returns 3.4 (float) while // returns 3 (floor division). The % operator gives the remainder, and raises to a power.

Comparison Operators

x = 10
y = 20

print(x == y)
print(x != y)
print(x > y)
print(x < y)
print(x >= 10)
print(x <= 5)

Each comparison returns a boolean. These operators are the building blocks of conditional statements (if/else) that we will cover in later lessons.

Logical Operators

a = True
b = False

print(a and b)
print(a or b)
print(not a)
print(not b)

# Chained comparison
x = 15
print(10 < x < 20)
print(20 < x < 30)

Logical operators combine booleans. Python's chained comparisons (10 < x < 20) are a clean alternative to writing 10 < x and x < 20.

Assignment Operators

x = 10
print(x)

x += 5
print(x)

x -= 3
print(x)

x *= 2
print(x)

x //= 4
print(x)

x = 3
print(x)

Augmented assignment operators modify a variable in place. x += 5 is shorthand for x = x + 5. The sequence: 10 -> +5=15 -> -3=12 -> *2=24 -> //4=6 -> 3=216.

Key points

Concepts covered

Arithmetic operators, Comparison operators, Logical operators, Assignment operators, Operator precedence