Operators in Depth
Operators are the verbs of the programming world. They take data (operands) and perform actions on them to produce new values. While basic math is straightforward, Python’s operators have unique behaviors—like floor division and short-circuit logic—that are essential for writing robust code.
1. Arithmetic Operators
Section titled “1. Arithmetic Operators”Python handles math with high precision, but you must understand the difference between the two types of division.
| Symbol | Operation | Behavior | Example |
|---|---|---|---|
+ | Addition | Sums values. | 5 + 2 = 7 |
- | Subtraction | Finds difference. | 5 - 2 = 3 |
* | Multiplication | Finds product. | 5 * 2 = 10 |
/ | True Division | Always returns a float. | 5 / 2 = 2.5 |
// | Floor Division | Returns the integer part (rounds down). | 5 // 2 = 2 |
% | Modulus | Returns the remainder. | 5 % 2 = 1 |
** | Exponentiation | Power of (x to the y). | 5 ** 2 = 25 |
Floor Division with Negatives
Section titled “Floor Division with Negatives”A common pitfall is assuming // just “chops off” the decimal. It actually rounds down towards negative infinity.
print(-7 // 2) # Output: -4 (not -3!)2. Comparison & Chaining
Section titled “2. Comparison & Chaining”Comparison operators evaluate the relationship between two values and return a bool.
| Operator | Meaning |
|---|---|
== | Equal value |
!= | Not equal |
> / < | Greater / Less than |
>= / <= | Greater / Less or equal |
Chained Comparisons
Section titled “Chained Comparisons”Python allows you to write mathematical ranges just like you would on paper. This is more readable than using and.
age = 25# Standard way: if age >= 18 and age <= 65:# Pythonic way:if 18 <= age <= 65: print("Working age adult")3. Logical Operators & Short-Circuiting
Section titled “3. Logical Operators & Short-Circuiting”and, or, and not allow you to build complex decision trees.
Short-Circuit Evaluation (Deep Dive)
Section titled “Short-Circuit Evaluation (Deep Dive)”Python is efficient. It stops evaluating an expression as soon as the result is certain.
A and B: IfAis False, Python returnsAand does not even look atB.A or B: IfAis True, Python returnsAand does not even look atB.
def log_and_return(): print("This ran!") return True
# Since the first part is False, the function NEVER runs.result = False and log_and_return()4. Bitwise Operators (Under the Hood)
Section titled “4. Bitwise Operators (Under the Hood)”Bitwise operators treat integers as strings of binary 0s and 1s. These are primarily used in low-level systems programming, cryptography, and performance optimization.
| Operator | Name | Action | Example (5 is 0101, 3 is 0011) |
|---|---|---|---|
& | AND | 1 if both bits are 1. | 5 & 3 -> 0001 (1) |
| ` | ` | OR | 1 if either bit is 1. |
^ | XOR | 1 if bits are different. | 5 ^ 3 -> 0110 (6) |
~ | NOT | Inverts all bits. | ~5 -> -6 (due to 2’s complement) |
<< | L-Shift | Moves bits left (multiplies by 2). | 5 << 1 -> 1010 (10) |
>> | R-Shift | Moves bits right (divides by 2). | 5 >> 1 -> 0010 (2) |
5. Operator Precedence
Section titled “5. Operator Precedence”When you mix operators, Python follows a strict hierarchy (order of operations).
- Parentheses
()(Always use these to be explicit!) - Exponentiation
** - Multiplication/Division
*,/,//,% - Addition/Subtraction
+,- - Comparisons
==,>,<, etc. - Logical
not - Logical
and - Logical
or
# Is it (10 + 2) * 5 or 10 + (2 * 5)?result = 10 + 2 * 5# Multiplication is higher: 10 + 10 = 20