- What operators are and how they form expressions
- Arithmetic operators for math operations
- Assignment operators to assign values
- Comparison operators to compare values
- Logical operators for boolean logic
- Bitwise operators for low-level operations
- Special operators like identity and membership
- Hands-on practice with the interactive editor
What Are Operators and Expressions?
In Python, an operator is a symbol that tells the interpreter to perform a specific mathematical, logical, or other operation. An expression is a combination of values, variables, and operators that evaluates to a single value.
Think of operators as verbs in a sentence — they act on data (operands) to produce results. For example, in a + b, the + is the operator, and a and b are operands.
# Simple expressions
x = 10 + 5 # addition expression
y = x * 2 # multiplication expression
is_greater = x > y # comparison expression
💡 Key insight: Every expression in Python evaluates to a value. Understanding operators is the key to writing powerful, concise code.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. They work on numeric values (int, float, complex).
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.333...
print("Floor Division:", a // b) # 3
print("Modulus:", a % b) # 1
print("Exponent:", a ** b) # 1000
➕ Addition (+)
Adds two operands
➖ Subtraction (-)
Subtracts second from first
✖️ Multiplication (*)
Multiplies two operands
➗ Division (/)
Returns float result
🔢 Floor Division (//)
Integer division
🧮 Modulus (%)
Returns remainder
2. Assignment Operators
Assignment operators are used to assign values to variables. The most common is =, but there are also compound assignment operators that combine arithmetic with assignment.
x = 5 # simple assignment
x += 3 # same as x = x + 3 → x = 8
x -= 2 # x = x - 2 → 6
x *= 2 # x = x * 2 → 12
x /= 4 # x = x / 4 → 3.0
x //= 2 # x = x // 2 → 1
x **= 3 # x = x ** 3 → 1
3. Comparison (Relational) Operators
Comparison operators compare two values and return a Boolean (True or False). They are fundamental in decision-making and loops.
a = 10
b = 20
print("Equal:", a == b) # False
print("Not Equal:", a != b) # True
print("Greater:", a > b) # False
print("Less:", a < b) # True
print("Greater or Equal:", a >= b) # False
print("Less or Equal:", a <= b) # True
4. Logical Operators
Logical operators (and, or, not) are used to combine conditional statements. They return Boolean values.
x = True
y = False
print("x and y:", x and y) # False
print("x or y:", x or y) # True
print("not x:", not x) # False
5. Bitwise Operators
Bitwise operators act on bits and perform operations bit-by-bit. They are useful in low-level programming, cryptography, and performance optimization.
a = 5 # binary: 0101
b = 3 # binary: 0011
print("Bitwise AND:", a & b) # 1 (0001)
print("Bitwise OR:", a | b) # 7 (0111)
print("Bitwise XOR:", a ^ b) # 6 (0110)
print("Bitwise NOT:", ~a) # -6
print("Left Shift:", a << 1) # 10 (1010)
print("Right Shift:", a >> 1) # 2 (0010)
6. Special Operators
Python has two special operators: identity (is, is not) and membership (in, not in). They are used to check object identity and membership in sequences.
# Identity operators
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a is b) # False (different objects)
print(a is c) # True (same object)
# Membership operators
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
Try It Yourself!
Experiment with operators directly in your browser. Modify the code and see the results in real time.
ARITHMETIC OPERATORS
========================================
10 + 3 = 13
10 - 3 = 7
10 * 3 = 30
10 / 3 = 3.3333333333333335
10 // 3 = 3
10 % 3 = 1
10 ** 3 = 1000
========================================
COMPARISON OPERATORS
========================================
10 == 3: False
10 != 3: True
10 > 3: True
========================================
LOGICAL OPERATORS
========================================
True and False: False
True or False: True
not True: False
========================================
BITWISE OPERATORS
========================================
5 & 3 = 1
5 | 3 = 7
5 ^ 3 = 6
✅ Experiment with different operators!
🎉 You've Mastered Python Operators!
You understand arithmetic, assignment, comparison, logical, bitwise, and special operators. These are essential tools for every Python programmer.
Quick Quiz – Test Your Knowledge
Let's see what you've learned about Python operators:
5 & 3 in Python?Frequently Asked Questions
🤔 What's the difference between == and is?
▼
== checks value equality, while is checks object identity (whether two references point to the same object).
⚡ What is operator precedence? ▼
📐 What is floor division? ▼
//) returns the largest integer less than or equal to the division result. It's different from true division (/) which returns a float.
📚 Where to Go From Here
Now that you understand Python operators, here are some related topics to explore:
📊 Data Types
Review Python data types
📝 Constants
Learn about constants in Python
📋 Assignment Statements
Understand assignment in Python