Operators
## Learning Objectives
- Master arithmetic operators
- Understand comparison operators
- Learn logical and bitwise operators
- Work with assignment operators
## Arithmetic Operators
### Basic Operations
```python
a, b = 10, 3
print(a + b) # 13 (addition)
print(a - b) # 7 (subtraction)
print(a * b) # 30 (multiplication)
print(a / b) # 3.333... (division - always float)
print(a // b) # 3 (floor division)
print(a % b) # 1 (modulus - remainder)
print(a ** b) # 1000 (exponentiation)
```
### Division Behavior
```python
# Python 3: / always returns float
print(10 / 2) # 5.0
print(10 / 3) # 3.3333333333333335
# // for integer division (floor)
print(10 // 3) # 3
print(-10 // 3) # -4 (floors toward negative infinity)
```
### Modulus with Negative Numbers
```python
print(10 % 3) # 1
print(-10 % 3) # 2 (always positive result)
print(10 % -3) # -2
```
## Comparison Operators
### Basic Comparisons
```python
a, b = 5, 10
print(a == b) # False (equal)
print(a != b) # True (not equal)
print(a < b) # True (less than)
print(a > b) # False (greater than)
print(a <= b) # True (less than or equal)
print(a >= b) # False (greater than or equal)
```
### Chained Comparisons
```python
x = 5
print(1 < x < 10) # True
print(1 < x < 3) # False
print(x > 0 and x < 10) # Same as above
```
### String Comparison
```python
print("apple" == "apple") # True
print("apple" == "Apple") # False (case sensitive)
print("apple" < "banana") # True (alphabetical)
print("a" < "A") # False (ASCII order)
```
## Logical Operators
### and, or, not
```python
x, y = True, False
print(x and y) # False
print(x or y) # True
print(not x) # False
print(not y) # True
```
### Truth Values
```python
# and returns first falsy value or last value
print(0 and 5) # 0
print(1 and 5) # 5
print(None and 0) # None
# or returns first truthy value or last value
print(0 or 5) # 5
print(None or 0) # 0
print(1 or 5) # 1
# Short-circuit evaluation
x = 5
y = 0
result = y != 0 and x / y > 0 # No division by zero!
```
### Boolean to Int
```python
print(True + True) # 2
print(False + 1) # 1
```
## Bitwise Operators
### Binary Operations
```python
a, b = 5, 2 # 101 and 010 in binary
print(a & b) # 0 (AND: 101 & 010 = 000)
print(a | b) # 7 (OR: 101 | 010 = 111)
print(a ^ b) # 7 (XOR: 101 ^ 010 = 111)
print(~a) # -6 (NOT: ~101 = ...010 = -110)
print(a << 1) # 10 (left shift)
print(a >> 1) # 2 (right shift)
```
### Common Uses
```python
# Check if bit is set
flags = 0b1010 # decimal 10
print(flags & 0b1000 != 0) # True (8 is set)
# Set a bit
flags = flags | 0b0001 # Set bit 0
# Clear a bit
flags = flags & ~0b0010 # Clear bit 1
# Toggle a bit
flags = flags ^ 0b0100 # Toggle bit 2
```
## Assignment Operators
### Simple Assignment
```python
x = 5
```
### Compound Assignment
```python
x = 5
x += 3 # x = 8
x -= 2 # x = 6
x *= 2 # x = 12
x /= 3 # x = 4.0
x //= 2 # x = 2.0
x %= 3 # x = 2.0
x **= 2 # x = 4.0
```
### Bitwise Assignment
```python
x = 5 # 0101 in binary
x &= 3 # 0101 & 0011 = 0001, so x = 1
x |= 3 # 0001 | 0011 = 0011, so x = 3
x ^= 3 # 0011 ^ 0011 = 0000, so x = 0
x <<= 2 # 0000 << 2 = 0000, so x = 0
x >>= 1 # 0000 >> 1 = 0000, so x = 0
```
## Identity Operators
### is vs ==
```python
a = [1, 2, 3]
b = [1, 2, 3]
c = a
print(a == b) # True (values equal)
print(a is b) # False (different objects)
print(a is c) # True (same object)
# Singleton values
x = None
print(x is None) # True
print(x is not None) # False
```
## Membership Operators
### in, not in
```python
numbers = [1, 2, 3, 4, 5]
print(3 in numbers) # True
print(6 in numbers) # False
print(3 not in numbers) # False
# Strings
text = "Hello, World!"
print("Hello" in text) # True
print("hello" in text) # False (case sensitive)
print("World" not in text) # False
```
## Operator Precedence
### From Highest to Lowest
```python
# 1. Parentheses
# 2. Exponentiation **
# 3. Unary +x, -x, ~x
# 4. *, /, //, %
# 5. Binary +, -
# 6. <<, >>
# 7. &
# 8. ^
# 9. |
# 10. Comparisons (==, !=, <, >, <=, >=)
# 11. Boolean NOT: not
# 12. Boolean AND: and
# 13. Boolean OR: or
```
### Examples
```python
print(2 + 3 * 4) # 14 (not 20)
print((2 + 3) * 4) # 20
print(2 ** 3 ** 2) # 512 (right to left: 2 ** 9)
print((2 ** 3) ** 2) # 64
print(not True or True) # True (not True = False, False or True = True)
print(not (True or True)) # False
```
## Summary
- Arithmetic: `+`, `-`, `*`, `/`, `//`, `%`, `**`
- Comparison: `==`, `!=`, `<`, `>`, `<=`, `>=`
- Logical: `and`, `or`, `not`
- Bitwise: `&`, `|`, `^`, `~`, `<<`, `>>`
- Assignment: `=`, `+=`, `-=`, etc.
- Identity: `is`, `is not`
- Membership: `in`, `not in`
- Precedence: use parentheses to be clear
Comments
Comments powered by Giscus
To enable comments, add your Giscus embed code here.
Learn more about Giscus →