Arithmetic operators work on numbers and give back numbers. Six of them match school notation; the two extra ones, // and %, split a division into its whole part and its leftover.
| Symbol | Name | Example | Result |
|---|---|---|---|
| + | Addition | 7 + 3 | 10 |
| - | Subtraction | 7 - 3 | 4 |
| * | Multiplication | 7 * 3 | 21 |
| / | True division | 7 / 3 | 2.3333333333333335 |
| // | Floor division | 7 // 3 | 2 |
| % | Modulo | 7 % 3 | 1 |
| ** | Power | 7 ** 3 | 343 |
One division question has three useful answers, and Python gives each of them a symbol. Asking how many whole boxes of 3 fit inside 7 is //, asking what is left over is %, and asking for the full decimal answer is /.
n % 2 gives 0 for even numbers and 1 for odd ones, which is the standard way to test parity./, // or % stops the program with a ZeroDivisionError.Type any two numbers and watch all seven operators answer at once. Try a negative value for b, a decimal for a, and a zero to see which operators refuse to work.
Every result carries a type: integer numbers stay int, and a single decimal anywhere makes the answer float.
A relational operator asks a question about two values, and the answer is a boolean: True or False.
| Symbol | Question | Example | Result |
|---|---|---|---|
| == | Same value? | 5 == 5.0 | True |
| != | Different value? | 5 != 3 | True |
| < | Smaller? | 3 < 5 | True |
| <= | Smaller or equal? | 5 <= 5 | True |
| > | Larger? | 3 > 5 | False |
| >= | Larger or equal? | 3 >= 5 | False |
Two comparisons about the same value can be written as one chain, the way mathematics writes it. Python reads it as the two halves joined by and, with the middle value named once.
A single = stores a value in a name. A double == asks whether two values match and answers True or False. Swapping one for the other is the most common beginner mistake in Python.
Writing if age = 18: is a SyntaxError, so that version of the slip is caught at once. The dangerous case is the other direction, age == 18 on a line of its own: the value is computed, nothing is stored, and the program runs on with the old value.
Two strings compare in dictionary order. Python walks both strings together, stops at the first pair of characters that differ, and decides there by comparing the code number of those two characters.
Sorting names as typed puts every capitalised name above every lowercase one, which is rarely what a reader expects.
Numbers of different types share one number line, so an int, a float and a bool compare against each other without any conversion. Text sits outside that line, and Python has no rule for placing it there.
| Comparison | Answer | Why |
|---|---|---|
| 5 == 5.0 | ✓ True | An int and a float are compared by value, and 5 sits at the same point as 5.0. |
| 2 < 2.5 | ✓ True | Ordering across int and float works the same way. |
| True == 1 | ✓ True | True is the number 1 and False is the number 0. |
| "5" == 5 | ✕ False | A string and a number are never equal, and no error is raised. |
| "5" < 5 | ✕ TypeError | Ordering has no meaning between text and a number, so the program stops. |
Every character is stored as a number, and comparing two characters compares those numbers. The first 128 codes are the ASCII set, and this is the order of its blocks.
Symbols fill every gap between the letter blocks, so : sits at 58 and _ at 95. Their exact codes are worth a lookup rather than memorising; the order of the blocks is the part to keep.
Pick a value on each side and read all six answers at once. Mixing a string with a number is the interesting case: equality still answers, while the ordering operators refuse.
Two numbers of different types still compare by value, so 5 and 5.0 count as equal.
Logical operators combine conditions into a single answer. Python writes them as words rather than symbols, so each one means on the page what it means in a sentence.
| Word | True when | Example | Result |
|---|---|---|---|
| and | Both sides hold | True and False | False |
| or | At least one side holds | False or True | True |
| not | The single value given is false | not True | False |
When several operators sit in one expression, Python applies them from the top of this ladder downwards. Operators on the same level run from left to right, with ** as the one exception, which runs from right to left.
Press Step to apply one operator at a time, always the one highest on the ladder. The part about to be evaluated is boxed, and the value it leaves behind is marked in gold.
The multiplication goes first, since nothing in this expression outranks it.
The console below evaluates one expression per line, the way an interactive Python session does. Replace each comment with an expression that answers the task, then press Run. If one of them resists, the hint button opens the answers one at a time.
% and a comparison.//, then the leftover minutes with %.Numbers, strings, True, False, parentheses and every operator on this page are accepted.
/ for the decimal answer, // for whole parts, % for the leftover.= and == apart.and, or and not, and reproduce their truth tables from memory.