Unit 1 · Operators in Python
Operators in Python
Operators are the symbols that let you perform calculations, comparisons, and logical checks on your data — the verbs of the Python language.
Introduction
An operator is a special symbol that performs an operation on one or more values (called operands). Python provides a rich set of operators for performing arithmetic, comparisons, logical checks, assignments, and more.
Table of Contents
Arithmetic Operators
Used to perform mathematical calculations on numeric values.
| Operator | Meaning | Example |
|---|---|---|
+ |
Addition | 5 + 2 = 7 |
- |
Subtraction | 5 - 2 = 3 |
* |
Multiplication | 5 * 2 = 10 |
/ |
Division (float result) | 5 / 2 = 2.5 |
// |
Floor Division | 5 // 2 = 2 |
% |
Modulus (remainder) | 5 % 2 = 1 |
** |
Exponentiation | 5 ** 2 = 25 |
Relational (Comparison) Operators
Used to compare two values. Every relational operation returns a boolean (True or False).
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | 5 == 5 → True |
!= |
Not equal to | 5 != 3 → True |
> |
Greater than | 5 > 3 → True |
< |
Less than | 5 < 3 → False |
>= |
Greater than or equal to | 5 >= 5 → True |
<= |
Less than or equal to | 5 <= 3 → False |
Logical Operators
Used to combine multiple conditions.
and
True only if both conditions are true.
or
True if at least one condition is true.
not
Reverses the result of a condition.
Assignment Operators
Used to assign values to variables, often combined with an arithmetic operation.
x = 10 x += 5 # same as x = x + 5 → 15 x -= 3 # same as x = x - 3 → 12 x *= 2 # same as x = x * 2 → 24 x //= 5 # same as x = x // 5 → 4
Bitwise Operators
Operate directly on the binary representation of integers.
| Operator | Meaning |
|---|---|
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise NOT |
<< |
Left Shift |
>> |
Right Shift |
Membership and Identity Operators
Membership: in, not in
Checks whether a value exists inside a sequence. Example:
'a' in 'apple' → True
Identity: is, is not
Checks whether two variables point to the same object in memory.
Operator Precedence
When an expression contains multiple operators, Python evaluates them in a fixed order — from highest to
lowest precedence: () →
** →
* / // % →
+ - →
comparison operators → not →
and →
or.
Tip
When unsure, use parentheses () to make the intended order of evaluation explicit.
Code Example
a = 10 b = 3 print(a + b) # 13 print(a % b) # 1 print(a > b and b > 0) # True
Common Beginner Mistakes
- Confusing
=(assignment) with==(comparison). - Confusing
/(float division) with//(floor division). - Forgetting operator precedence and getting unexpected results.
- Using
and/orwhere a bitwise&/|was intended.
Quick Revision
| Category | Examples |
|---|---|
| Arithmetic | + - * / // % ** |
| Relational | == != > < >= <= |
| Logical | and or not |
| Membership / Identity | in, not in, is, is not |
Summary
Operators let you perform calculations, comparisons, and logical checks on data. Python provides arithmetic, relational, logical, assignment, bitwise, membership, and identity operators — each with a defined precedence that determines the order of evaluation in complex expressions.