Operators in Python
Definition: An operator is a symbol that performs an operation on one or more operands(values / variables).Python supports a rich set of operators across several categories.
---
1. Arithmetic Operators
These perform basic mathematical operations.
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division (float) | 10 / 3 | 3.333... |
// | Floor Division | 10 // 3 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 10 ** 3 | 1000 |
Data Science Use Cases:
/and//— Normalization, calculating averages.%— Checking even/odd, batch processing (every nth item).**— Squaring errors in loss functions.
---
2. Comparison (Relational) Operators
These compare two values and return a Boolean (True or False).
| Operator | Name | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 5 < 3 | False |
>= | Greater or equal | 5 >= 5 | True |
<= | Less or equal | 3 <= 5 | True |
Common Mistake: Using = (assignment) instead of == (comparison).
---
3. Logical Operators
These combine multiple conditions and return a Boolean.
| Operator | Description | Example | Result |
|---|---|---|---|
and | True if BOTH are True | (5 > 3) and (10 > 7) | True |
or | True if EITHER is True | (5 > 3) or (10 < 7) | True |
not | Reverses the result | not (5 > 3) | False |
Truth Table:
| A | B | A and B | A or B | not A |
|---|---|---|---|---|
| True | True | True | True | False |
| True | False | False | True | False |
| False | True | False | True | True |
| False | False | False | False | True |
---
4. Assignment Operators
These assign values to variables, often with a shorthand for combined operations.
| Operator | Example | Equivalent To |
|---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
//= | x //= 3 | x = x // 3 |
%= | x %= 3 | x = x % 3 |
**= | x **= 3 | x = x ** 3 |
---
5. Identity Operators
These check whether two variables refer to the same object in memory, not just equal values.
| Operator | Description | Example |
|---|---|---|
is | Returns True if same object | x is y |
is not | Returns True if NOT same object | x is not y |
Key Difference: == vs is:
==checks equality of values.ischecks identity of memory location.
---
6. Membership Operators
These check if a value exists within a sequence (list, string, tuple, etc.).
| Operator | Description | Example | Result |
|---|---|---|---|
in | True if value is in sequence | "a" in "apple" | True |
not in | True if value is NOT in sequence | "z" not in "apple" | True |
---
7. Bitwise Operators
These operate on binary representations of integers. While less common in everyday data science, they are used in image processing, cryptography, and optimization.
| Operator | Name | Example | Result | ||
|---|---|---|---|---|---|
& | AND | 5 & 3 | 1 | ||
| ` | ` | OR | `5 | 3` | 7 |
^ | XOR | 5 ^ 3 | 6 | ||
~ | NOT | ~5 | -6 | ||
<< | Left Shift | 5 << 1 | 10 | ||
>> | Right Shift | 5 >> 1 | 2 |
---
Operator Precedence (Order of Evaluation)
When multiple operators appear in an expression, Python evaluates them in a specific order:
| Priority | Operator | Description | |
|---|---|---|---|
| 1 (Highest) | () | Parentheses | |
| 2 | ** | Exponentiation | |
| 3 | ~, +x, -x | Unary operators | |
| 4 | *, /, //, % | Multiplication, Division | |
| 5 | +, - | Addition, Subtraction | |
| 6 | <<, >> | Bitwise Shifts | |
| 7 | & | Bitwise AND | |
| 8 | ^ | Bitwise XOR | |
| 9 | ` | ` | Bitwise OR |
| 10 | ==, !=, >, <, >=, <= | Comparisons | |
| 11 | not | Logical NOT | |
| 12 | and | Logical AND | |
| 13 (Lowest) | or | Logical OR |
Tip: When in doubt, use parentheses () to make the order explicit.
Summary
- Python has 7 categories of operators: Arithmetic, Comparison, Logical, Assignment, Identity, Membership, and Bitwise.
- Comparison operators return Boolean values, forming the basis of conditional logic.
- Logical operators (
and,or,not) combine conditions. - Identity (
is) checks memory reference, while Equality (==) checks value. - Operator precedence determines the order of evaluation; use parentheses for clarity.