Operators are the verbs of programming — they do things to values: add them, compare them, combine conditions. Python has several families of operators, and this chapter runs through all of them with a playground so you can test every result yourself.
1Arithmetic operators
These do maths. Three of them surprise beginners, so look closely:
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * | add, subtract, multiply | 7 * 2 | 14 |
/ | division — always gives a float | 7 / 2 | 3.5 |
// | floor division — drops the fraction | 7 // 2 | 3 |
% | modulo — the remainder | 7 % 2 | 1 |
** | exponent (power) | 2 ** 3 | 8 |
% (modulo) operator is a workhorse: n % 2 == 0 tests if n is even, and it's everywhere in real programs. Run the playground and change the numbers to feel how /, // and % differ.Click Run to execute.
/always gives a float (7/2 = 3.5);//is floor division (7//2 = 3).%(modulo) gives the remainder;n % 2 == 0tests for even numbers.**is exponent (2**3 = 8).
2Comparison and logical operators
Comparison (relational) operators compare two values and always produce a Boolean — True or False:
| Operator | Meaning |
|---|---|
== | equal to |
!= | not equal to |
< > | less than, greater than |
<= >= | less than or equal, greater than or equal |
= (assignment — put a value into a variable) with == (comparison — ask if two values are equal). Mixing them up is a classic bug.Logical operators combine Boolean values:
and— True only if both sides are True.or— True if either side is True.not— flips True to False and vice versa.
Click Run to execute.
- Comparison operators (
==,!=,<,>,<=,>=) always return a Boolean (True/False). =assigns a value;==checks equality — don't confuse them.- Logical:
andneeds both True,orneeds either True,notflips the Boolean.
3Assignment, identity and membership
Three more handy families:
- Assignment:
=stores a value. The shortcuts+=,-=,*=,/=update a variable in place —x += 5meansx = x + 5. - Membership:
inandnot intest whether a value appears inside a string, list, etc. —'a' in 'cat'isTrue. - Identity:
isandis nottest whether two names refer to the same object in memory (not just equal values). It's most commonly used asx is None.
** first, then * / // %, then + -, then comparisons, then not, and, or. When in doubt, use parentheses to make the order explicit and readable.Click Run to execute.
=assigns; shortcuts like+=update in place (x += 5 means x = x + 5).- Membership
in/not intest if a value is inside a string/list. - Identity
is/is nottest if two names are the same object (oftenx is None). - Precedence: ** then * / // % then + - then comparisons then not/and/or; use parentheses to be clear.
★ Practical: predict, then run
First predict each result on paper, then check it in the playground:
- What do 17 / 5, 17 // 5 and 17 % 5 each give?
- Is 4 == 4.0 True or False? What about 4 is 4.0?
- Evaluate: (3 > 2) and (5 != 5).
- Start with x = 8, then do x *= 2 twice. What is x?
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.