Python Basics — Assignment & Bitwise Operators
Assignment & Bitwise Operators
Assignment operators store (and optionally modify) values in variables. Bitwise operators work on the individual bits of integer values.
Assignment Operators
| Operator | Example | Equivalent To |
|---|---|---|
= |
x = 5 |
Assign 5 to x |
+= |
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 |
Examples
x = 10
print(x) # 10
x += 5 # x = x + 5
print(x) # 15
x -= 3 # x = x - 3
print(x) # 12
x *= 2 # x = x * 2
print(x) # 24
x //= 5 # x = x // 5
print(x) # 4
x **= 3 # x = x ** 3
print(x) # 64
x %= 10 # x = x % 10
print(x) # 4
Augmented assignment also works with strings and lists:
text = "Hello"
text += " World"
print(text) # Hello World
items = [1, 2]
items += [3, 4]
print(items) # [1, 2, 3, 4]
Walrus Operator (:=)
The walrus operator (:=), introduced in Python 3.8, assigns a value to a variable as part of an expression. It’s called the walrus operator because := looks like a walrus on its side.
# Without walrus operator
data = input("Enter a number: ")
if len(data) > 0:
print(f"You entered: {data}")
# With walrus operator — assign and test in one step
if (data := input("Enter a number: ")):
print(f"You entered: {data}")
Practical Use Cases
# Reading lines from a file
# while (line := file.readline()):
# process(line)
# Avoiding repeated computation
import random
numbers = [random.randint(1, 20) for _ in range(10)]
# Filter and capture at the same time
big = [n for n in numbers if (square := n ** 2) > 100]
Note: The walrus operator is optional. If it feels confusing, stick with regular assignment. Readability matters more.
Bitwise Operators
Bitwise operators work on the binary representation of integers:
| Operator | Name | Example | Result | Binary Explanation |
|---|---|---|---|---|
& |
AND | 5 & 3 |
1 |
101 & 011 = 001 |
\| |
OR | 5 \| 3 |
7 |
101 \| 011 = 111 |
^ |
XOR | 5 ^ 3 |
6 |
101 ^ 011 = 110 |
~ |
NOT | ~5 |
-6 |
Inverts all bits |
<< |
Left shift | 5 << 1 |
10 |
101 → 1010 |
>> |
Right shift | 5 >> 1 |
2 |
101 → 10 |
Understanding Binary
# View binary representation
print(bin(5)) # 0b101
print(bin(3)) # 0b11
print(bin(10)) # 0b1010
# Convert binary string to integer
print(int("101", 2)) # 5
print(int("1010", 2)) # 10
Bitwise AND (&)
Returns 1 only where both bits are 1:
a = 5 # 101
b = 3 # 011
print(a & b) # 1 (001)
# 101
# & 011
# -----
# 001 = 1
Bitwise OR (|)
Returns 1 where at least one bit is 1:
a = 5 # 101
b = 3 # 011
print(a | b) # 7 (111)
Bitwise XOR (^)
Returns 1 where bits are different:
a = 5 # 101
b = 3 # 011
print(a ^ b) # 6 (110)
Bit Shifting
# Left shift — multiply by 2^n
print(5 << 1) # 10 (5 × 2)
print(5 << 2) # 20 (5 × 4)
print(1 << 10) # 1024 (2^10)
# Right shift — divide by 2^n (floor)
print(20 >> 1) # 10 (20 ÷ 2)
print(20 >> 2) # 5 (20 ÷ 4)
When to Use Bitwise Operations
For beginners, bitwise operators are rarely needed. They show up in:
| Use Case | Example |
|---|---|
| Permissions/Flags | File permissions: rwx = 0b111 |
| Performance | x << 1 instead of x * 2 |
| Networking | IP address masks, subnet calculation |
| Cryptography | XOR encryption |
| Low-level programming | Hardware registers, embedded systems |
Augmented Bitwise Assignment
x = 5
x &= 3 # x = x & 3 → 1
x |= 6 # x = x | 6 → 7
x ^= 2 # x = x ^ 2 → 5
x <<= 1 # x = x << 1 → 10
x >>= 2 # x = x >> 2 → 2
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
& instead of and |
if x & y: (bitwise) |
Use and for logic |
\| instead of or |
if x \| y: (bitwise) |
Use or for logic |
| Walrus in wrong context | x := 5 (bare statement) |
Use in expressions: if (x := 5): |
Summary
- Augmented assignment (
+=,-=, etc.) modifies and reassigns in one step - The walrus operator (
:=) assigns within an expression (Python 3.8+) - Bitwise operators (
&,|,^,~,<<,>>) work on binary bits - Use
and/orfor logical operations,&/|for bitwise operations - Bitwise operations are advanced — focus on assignment operators first
Next up: Identity & Membership Operators →