Unveiling Python Operators: A Must-Know for Every Coder
Most people learning Python only know how to write <span>+ - * /</span> but true experts quietly utilize some “hidden skills” that you might not have noticed:
- Bitwise operations: Allow you to handle numbers at low-level speed.
- Membership operations: Write highly readable conditional statements.
- Identity operations: Distinguish between “looks the same” and “is the same”.
- Operator precedence: Ensures your code runs without relying on luck.
Today, we will get straight to the point and clarify these operators in the simplest way possible.
1. Bitwise Operations: Equipping Numbers with a “Microscope”
If ordinary operations are like a street-side shop, then bitwise operations are like a “quantum processor”. They operate directly on the binary bits of numbers, achieving lightning-fast speeds.
| Operator | Description | Example |
|---|---|---|
| & | Bitwise AND | 2 & 3 = 2 |
| | | Bitwise OR | 2 | 3 = 3 |
| ^ | Bitwise XOR | 2 ^ 3 = 1 |
| ~ | Bitwise NOT | ~2 = -3 |
| << | Left Shift | 2 << 1 = 4 |
| >> | Right Shift | 2 >> 1 = 1 |
Syntax:
Bitwise AND: a & b
Bitwise OR: a | b
Bitwise XOR: a ^ b
Bitwise NOT: ~a
Left Shift: a << b
Right Shift: a >> b
Here, a and b are the two integers on which bitwise operations are performed.
Left shift is like adding a “0 tail” to a number, while right shift is like trimming the tail. A simple analogy that helps you instantly understand bitwise operations.
When you need to manage permissions, encrypt data, perform scientific calculations, or optimize performance—bitwise operations are always that sharp and quiet knife.
2. Membership Operations: Checking if You’re “In the Circle”
The membership operators in Python are straightforward:they check if something is inside another thing.
| Operator | Description | Example |
|---|---|---|
<span>in</span> |
Returns True if present | <span>5 in [1, 2, 3, 4, 5]</span> returns <span>True</span> |
<span>not in</span> |
Returns True if not present | <span>6 not in [1, 2, 3, 4, 5]</span> returns <span>True</span> |
What do these two operators resemble?They are like an old-timer who can instantly tell if you are “one of us”.
🔸 <span>in</span>: Checking if you are a “familiar face”
text = "Hello, World!"
print('Hello' in text)
print('Python' in text)
Output:
True
False
Now let’s look at lists, tuples, sets, and dictionaries…<span>in</span> can handle them all.
name = ['daxiong', 'andy', 'teacher']
'daxiong' in name # True
Dictionaries are particularly interesting:in checks only keys, not values.
dic = {'name': 'andy', 'age': 18}
'name' in dic # True
'gender' in dic # False
🔸 <span>not in</span>: Checking if you are an “outsider”
It works just like <span>in</span>, but in the opposite way:
'Python' not in ['a', 'b'] # True
In summary:
❝
<span>in</span>checks “if present”, while<span>not in</span>checks “if not present”.
These are particularly useful for permission checks, data filtering, and input validation.
3. Identity Operators: Looking the Same ≠ Being the Same
Membership operators check “if inside”. What do identity operators check?They check if you and another object are the same.
Identity operators in Python are used to compare the memory addresses of two objects, determining if they are the same object. The identity operators include is and is not.
| Operator | Description | Example |
|---|---|---|
<span>is</span> |
Returns True if both variables reference the same object, otherwise returns False. | <span>x is y</span> |
<span>is not</span> |
Returns True if both variables reference different objects, otherwise returns False. | <span>x is not y</span> |
Syntax:
x is y # Returns True if x and y reference the same object
x is not y # Returns True if x and y reference different objects
Here, x and y can be any Python objects.
Examples of identity operators:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # Returns True, as a and b reference the same list object
print(a is c) # Returns False, as a and c reference different list objects
print(a is not c) # Returns True, as a and c reference different list objects
Remember this in one sentence:
❝
<span>==</span>compares content, while<span>is</span>compares identity.
4. Operator Precedence: Writing Incorrect Parentheses is Like Missing a Beat
In Python programming, understanding operator precedence is key to writing correct code. When an expression contains multiple operators, precedence determines the order in which these operators are evaluated. Operators with higher precedence are evaluated first, while operators with the same precedence are evaluated from left to right. This article will detail the precedence of various operators in Python to help you avoid potential errors when writing complex expressions.
| Precedence from High to Low | Operator |
|---|---|
| 1 | <span>()</span> Parentheses |
| 2 | <span>**</span> |
| 3 | Unary:<span>+x</span><span>-x</span><span>~x</span> |
| 4 | <span>*</span><span>/</span><span>//</span><span>%</span> |
| 5 | <span>+</span><span>-</span> |
| 6 | <span><<</span><span>>></span> |
| 7 | <span>&</span> |
| 8 | <span>^</span> |
| 9 | ` |
| 10 | Comparison:<span>< <= > >= == !=</span>, <span>in not in</span>, <span>is is not</span> |
| 11 | <span>not</span> |
| 12 | <span>and</span> |
| 13 | <span>or</span> |
| 14 | <span>:=</span> (Walrus operator) |
| 15 | <span>lambda</span> (lowest) |
Keep this key rule in mind:
❝
Mathematics first → Bitwise operations in the middle → Comparisons later → Logic last → Lambda at the bottom.