Ready to make Python crunch numbers and make decisions? ๐ค
Operators are the symbols that tell Python what to do โ like +
(add), * (multiply), or > (is bigger than).
An expression is any combo of values and operators that produces a result.
We use them for maths, comparisons, and even clever logic. Let's go!
These are the maths operators you already know โ plus a few extra-cool ones:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Add | 7 + 2 | 9 |
- | Subtract | 7 - 2 | 5 |
* | Multiply | 7 * 2 | 14 |
/ | Divide | 7 / 2 | 3.5 |
// | Floor divide | 7 // 2 | 3 (rounds down) |
% | Modulo (remainder) | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
Python follows the same rules as maths class โ PEMDAS! This decides which part runs first in an expression.
These compare two values and give back a boolean
(True or False). Perfect for making decisions!
| Operator | Means | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal | 5 != 3 | True |
> | Greater than | 7 > 4 | True |
< | Less than | 7 < 4 | False |
>= | Greater or equal | 7 >= 7 | True |
<= | Less or equal | 3 <= 2 | False |
= is for saving a value. == is for comparing. They are NOT the same!
These combine booleans: and, or, and not.
Theyโre how computers make complex decisions.
| Operator | Rule | Example | Result |
|---|---|---|---|
and | Both must be True | True and False | False |
or | At least one True | True or False | True |
not | Flips it | not True | False |
and = both boxes must be ticked.
or = just one box needs a tick.
You already know =. But Python has shortcuts that
update a variable and save the result back into it.
| Shortcut | Same as | Example |
|---|---|---|
+= | x = x + ... | score += 10 |
-= | x = x - ... | lives -= 1 |
*= | x = x * ... | coins *= 2 |
//= | x = x // ... | total //= 2 |
Real programs combine arithmetic, comparison, and logic. Hereโs a mini boss-check for a game:
(player_level >= 15) and has_sword is easier to read than without them.
Create a file called pizza_party.py. Youโre organising a pizza party! Each pizza has 8 slices, and each friend eats 3 slices. Use operators to figure out:
%).// and round up).>= and and).๐ Bonus: print a fun message using an f-string that includes the result of a comparison!
2 ** 64 and see how big it gets!