Python Operators and Expressions
In Python, operators are special symbols or keywords that perform operations on variables and values. When combined with variables or literals, these form expressions, which are the core building blocks of logic and computation in your code.
Example:
x = 10
y = 5
result = x + y # Expression: x + y
Here, + is the operator, x and y are operands, and x + y is the expression.
Types of Python Operators
Python supports several categories of operators. Let’s explore each one with examples:
Arithmetic Operators
Used for mathematical calculations.
| Operator | Description | Example | Result |
|---|---|---|---|
+
|
Addition |
10 + 5
|
15 |
-
|
Subtraction |
10 - 5
|
5 |
*
|
Multiplication |
10 * 5
|
50 |
/
|
Division |
10 / 5
|
2.0 |
//
|
Floor Division |
10 // 3
|
3 |
%
|
Modulus (Remainder) |
10 % 3
|
1 |
**
|
Exponentiation |
2 ** 3
|
8 |
Example:
a = 7
b = 2
print(a + b) # 9
print(a ** b) # 49
print(a % b) # 1
Assignment Operators
Used to assign or update values in variables.
| Operator | Example | Equivalent To |
|---|---|---|
=
|
x = 10
|
Assign 10 to x |
+=
|
x += 5
|
x = x + 5
|
-=
|
x -= 2
|
x = x - 2
|
*=
|
x *= 3
|
x = x * 3
|
/=
|
x /= 2
|
x = x / 2
|
Example:
x = 5
x += 3 # Now x is 8
print(x)
Comparison Operators
Used to compare values. They return True or False.
| Operator | Description | Example |
|---|---|---|
==
|
Equal to |
x == y
|
!=
|
Not equal to |
x != y
|
>
|
Greater than |
x > y
|
<
|
Less than |
x < y
|
>=
|
Greater or equal |
x >= y
|
<=
|
Less or equal |
x <= y
|
Example:
a = 10
b = 5
print(a > b) # True
print(a == b) # False
Logical Operators
Used to combine multiple conditions.
| Operator | Description | Example |
|---|---|---|
and
|
True if both are true |
x > 5 and x < 10
|
or
|
True if one is true |
x > 5 or x == 2
|
not
|
Inverts result |
not(x > 5)
|
Example:
x = 7
print(x > 5 and x < 10) # True
print(not(x == 7)) # False
Membership Operators
Check if a value exists in a sequence (like a list or string).
| Operator | Description | Example |
|---|---|---|
in
|
True if exists |
"a" in "apple" → True
|
not in
|
True if not exists |
3 not in [1, 2, 4] → True
|
Example:
word = "banana"
print("a" in word) # True
print("x" not in word) # True
Identity Operators
Check if two variables refer to the same object in memory.
| Operator | Description | Example |
|---|---|---|
is
|
Same identity |
x is y
|
is not
|
Different identity |
x is not y
|
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True
print(a is c) # False
What Are Expressions?
An expression is a combination of variables, literals, and operators that evaluates to a value.
a = 5
b = 3
c = (a * b) + 2 # This is an Expression: (5 * 3) + 2 = 17
print(c)
Expressions can be used in assignments, conditions, loops, and function calls.
Operator Precedence (Order of Operations)
Python follows standard math rules (PEMDAS) for evaluating expressions.
| Order | Operator Types |
|---|---|
| 1 |
() – Parentheses
|
| 2 |
** – Exponentiation
|
| 3 |
*, /, //, % – Multiplication/Division
|
| 4 |
+, - – Addition/Subtraction
|
| 5 |
Comparison (==, <, >, etc.)
|
| 6 |
Logical (and, or, not)
|
Use parentheses to ensure the desired evaluation order.
Example:
a = 10
b = 4
c = 2
result = (a + b) * c / 2 - b
print("Result:", result)
Mini Quiz
- What is the output of 5 + 2 * 3?
- What does not(True and False) return?
- Is "p" in "Python" True or False?
- What’s the difference between == and is?
Summary
- Operators perform actions on variables and values.
- Expressions combine operators and values to compute results.
- Use parentheses to control the order of operations.
- Know the difference between equality (==) and identity (is).
- Python supports arithmetic, comparison, logical, assignment, membership, and identity operators.
Coming Up Next
In the next lesson, we’ll dive into Control Flow Statements like if, else, and elif, and learn how to make decisions in Python programs.