Arithmetic Operators
Python supports the standard math operations you're familiar with:
| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 5 | 15 |
| - | Subtraction | 10 - 5 | 5 |
| * | Multiplication | 10 * 5 | 50 |
| / | Division | 10 / 3 | 3.33 (float) |
| // | Floor Division | 10 // 3 | 3 (int) |
| % | Modulo (Remainder) | 10 % 3 | 1 |
| ** | Exponentiation | 10 ** 2 | 100 |
python
Comparison Operators
Use these to compare values and get a boolean (
True or False).| Operator | Description | Example |
|---|---|---|
| == | Equal to | 10 == 10 (True) |
| != | Not equal | 10 != 5 (True) |
| > | Greater than | 10 > 5 (True) |
| < | Less than | 10 < 5 (False) |
| >= | Greater or equal | 10 >= 10 (True) |
| <= | Less or equal | 10 <= 5 (False) |
python
Logical Operators
Used for boolean logic. Note use of English keywords!
| Operator | Description | Example |
|---|---|---|
| and | Both true | (5 > 3) and (2 < 4) (True) |
| or | At least one true | (5 > 3) or (2 > 10) (True) |
| not | Inverts boolean | not (5 > 3) (False) |
python
The Walrus Operator (Assignment Expression)
Introduced in Python 3.8, the walrus operator (
:=) allows you to assign a value to a variable inside an expression.python
Common Use Case (Loops): Read lines from a file until an empty line appears.
python
Assignment and Membership
python
Important: Use==for comparing values andisfor comparing identity (memory address), specifically withNone.
Summary
| Category | Example | Purpose |
|---|---|---|
| Arithmetic | x + y | Math operations |
| Comparison | x == y | Logic checks |
| Logical | a and b | Combined logic |
| Assignment | x = 10 | Store data |
| Walrus | (x := 5) | Inline assignment |