February 19, 2026 · All About CS

Python Operators — The Complete Guide

Master every category of Python operator — arithmetic, assignment, comparison, logical, and bitwise — with clear examples, truth tables, and a precedence reference.

pythonoperatorsbeginnerfundamentals

Python Operators — The Complete Guide

Operators are the verbs of any programming language. They tell Python what to do with your data — add it, compare it, combine it, or shift it bit by bit. A solid grasp of operators is foundational to writing expressive, bug-free code.

Key Takeaways

  • Python groups operators into five major categories: arithmetic, assignment, comparison, logical, and bitwise
  • Comparison operators always return True or False — they are the backbone of conditional logic
  • Logical operators (and, or, not) let you combine multiple conditions into a single expression
  • Bitwise operators work directly on the binary representation of integers
  • Operator precedence determines evaluation order when multiple operators appear in one expression

Arithmetic Operators

Arithmetic operators perform standard math on numeric values.

OperatorNameExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/Division7 / 32.333…
%Modulus7 % 31
**Exponentiation7 ** 3343
//Floor division7 // 32
Python
a, b = 17, 5

print(a + b)   # 22
print(a - b)   # 12
print(a * b)   # 85
print(a / b)   # 3.4  — always returns a float
print(a % b)   # 2    — remainder after division
print(a ** b)  # 1419857 — 17 raised to the power 5
print(a // b)  # 3    — quotient rounded down to nearest int

🖼️ Visual Suggestion: A diagram showing the difference between / (true division) and // (floor division) on a number line.

Assignment Operators

Assignment operators store — and optionally transform — a value in a variable. Every compound assignment is shorthand for a longer expression.

OperatorEquivalentExample
=x = 10
+=x = x + 3x += 313
-=x = x - 3x -= 37
*=x = x * 3x *= 330
/=x = x / 3x /= 33.33…
%=x = x % 3x %= 31
**=x = x ** 3x **= 31000
//=x = x // 3x //= 33
Python
score = 100
score += 15   # player earned bonus points
score -= 20   # penalty applied
print(score)  # 95

Comparison Operators

Comparison operators evaluate a relationship between two values and return a boolean — True or False.

OperatorMeaningExampleResult
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than5 > 3True
<Less than5 < 3False
>=Greater or equal5 >= 5True
<=Less or equal5 <= 3False
Python
age = 20
print(age >= 18)   # True — old enough to vote
print(age == 21)   # False
print(age != 20)   # False — age is exactly 20

Logical Operators

Logical operators combine boolean expressions. They are essential for building complex conditions.

OperatorDescriptionExampleResult
andTrue if both operands are TrueTrue and FalseFalse
orTrue if at least one operand is TrueTrue or FalseTrue
notInverts the boolean valuenot TrueFalse

Truth Tables

ABA and BA or B
TrueTrueTrueTrue
TrueFalseFalseTrue
FalseTrueFalseTrue
FalseFalseFalseFalse
Python
temperature = 32
is_weekend = True

# Both conditions must be true for a beach day
if temperature > 30 and is_weekend:
    print("Perfect beach day!")

# At least one condition triggers an alert
has_fever = True
has_cough = False
if has_fever or has_cough:
    print("Consider resting today.")

Bitwise Operators

Bitwise operators manipulate integers at the binary level. They are niche but powerful — commonly used in permissions, flags, and low-level algorithms.

OperatorNameDescription
&ANDSets each bit to 1 if both bits are 1
|ORSets each bit to 1 if at least one bit is 1
^XORSets each bit to 1 if exactly one bit is 1
~NOTInverts all bits
<<Left shiftShifts bits left, filling with zeros
>>Right shiftShifts bits right, discarding rightmost bits
Python
# 4 in binary: 100
# 5 in binary: 101
print(4 & 5)   # 4  — binary 100 (only the leftmost bit is shared)
print(4 | 5)   # 5  — binary 101
print(4 ^ 5)   # 1  — binary 001 (only the rightmost bit differs)
print(~4)      # -5 — inverts all bits (two's complement)
print(4 << 1)  # 8  — 100 becomes 1000
print(4 >> 1)  # 2  — 100 becomes 10

🖼️ Visual Suggestion: A step-by-step binary grid showing how 4 & 5 evaluates to 4 bit by bit.

Operator Precedence

When multiple operators appear in a single expression, Python follows a strict precedence hierarchy. Higher rows are evaluated first.

PriorityOperatorsDescription
1 (highest)**Exponentiation
2~, +x, -xUnary NOT, positive, negative
3*, /, //, %Multiplication, division, modulus
4+, -Addition, subtraction
5<<, >>Bitwise shifts
6&Bitwise AND
7^Bitwise XOR
8|Bitwise OR
9==, !=, >, <, >=, <=Comparisons
10notLogical NOT
11andLogical AND
12 (lowest)orLogical OR
Python
# Without parentheses, ** binds tighter than -
result = -2 ** 4    # -(2**4) = -16, NOT (-2)**4
print(result)       # -16

# Use parentheses to make intent explicit
result = (-2) ** 4  # 16
print(result)       # 16

When in doubt, add parentheses. They cost nothing at runtime and make your intent unmistakable to anyone reading your code.


Up next: we'll use these operators inside conditional statements — where Python starts making decisions on its own.