Programming Fundamentals

Operators and Booleans

↩ Back
01

Arithmetic

Reference
Seven Symbols

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.

SymbolNameExampleResult
+Addition7 + 310
-Subtraction7 - 34
*Multiplication7 * 321
/True division7 / 32.3333333333333335
//Floor division7 // 32
%Modulo7 % 31
**Power7 ** 3343
/ always answers with a float, even when the division comes out even
Detail
Division, Three Ways

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 /.

# 7 items, boxes of 3 print(7 / 3) # 2.3333333333333335 the decimal answer print(7 // 3) # 2 full boxes print(7 % 3) # 1 items left over
Two habits worth keeping
Even or oddn % 2 gives 0 for even numbers and 1 for odd ones, which is the standard way to test parity.
Division by zeroDividing by 0 with /, // or % stops the program with a ZeroDivisionError.
Interactive
Arithmetic Bench

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.

a + b10
a - b4
a * b21
a / b2.3333333333333335
a // b2
a % b1
a ** b343
-a-7

Every result carries a type: integer numbers stay int, and a single decimal anywhere makes the answer float.

02

Comparisons

Reference
Six Questions

A relational operator asks a question about two values, and the answer is a boolean: True or False.

SymbolQuestionExampleResult
==Same value?5 == 5.0True
!=Different value?5 != 3True
<Smaller?3 < 5True
<=Smaller or equal?5 <= 5True
>Larger?3 > 5False
>=Larger or equal?3 >= 5False
a comparison is an expression, so its answer can be stored: ok = age >= 18

Chained comparisons

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.

10 < 14 < 20 READS AS 10 < 14 and 14 < 20
Warning
One Equal vs Two Equals

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.

# two instructions with nothing in common age = 18 # store 18 in age print(age == 18) # True, a question about age print(age == 21) # False
Telling them apart while reading
One equalRead it as becomes. It is an instruction, it changes the program, and it has no answer to print.
Two equalsRead it as is equal to. It is a question, it changes nothing, and its answer is a boolean.
What Python does with the mistake

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.

Detail
Comparing Text

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.

print("apple" < "banana") # True, a comes before b print("apple" < "applesauce") # True, the shorter one runs out first print("apple" == "Apple") # False, case counts as a difference print("Zoe" < "amy") # True, Z is a capital

Sorting names as typed puts every capitalised name above every lowercase one, which is rarely what a reader expects.

Ignoring case on purpose
print("Zoe".lower() < "amy".lower()) # False, z after a
compare the lowercase versions when case should not decide the order
Detail
Comparing Across Types

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.

ComparisonAnswerWhy
5 == 5.0✓ TrueAn int and a float are compared by value, and 5 sits at the same point as 5.0.
2 < 2.5✓ TrueOrdering across int and float works the same way.
True == 1✓ TrueTrue is the number 1 and False is the number 0.
"5" == 5✕ FalseA string and a number are never equal, and no error is raised.
"5" < 5✕ TypeErrorOrdering has no meaning between text and a number, so the program stops.
Detail
Character Codes

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.

control 0 to 31 symbols 32 to 47 0 ... 9 48 to 57 symbols 58 to 64 A ... Z 65 to 90 symbols 91 to 96 a ... z 97 to 122 symbols 123 to 126 á ñ ü ... 128 and up ASCII · 0 TO 127 THE REST

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.

print(ord("A"), ord("a")) # 65 97 print("?" < "A" < "_" < "a") # True, left to right along the strip print("árbol" > "zoo") # True, á is 225
digits, then capitals, then lowercase, then everything accented
Interactive
Comparison Bench

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.

5 == 3False
5 != 3True
5 < 3False
5 <= 3False
5 > 3True
5 >= 3True

Two numbers of different types still compare by value, so 5 and 5.0 count as equal.

03

Logical Operators

Definition
and, or, not

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.

WordTrue whenExampleResult
andBoth sides holdTrue and FalseFalse
orAt least one side holdsFalse or TrueTrue
notThe single value given is falsenot TrueFalse
age = 20 member = False print(age >= 18 and member) # False, member sinks it print(age >= 18 or member) # True, the age is enough print(not member) # True
write conditions the way you would say them out loud
04

Order of Operations

Reference
The Ladder

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.

1**Power
2-xNegation of a single value
3* / // %Multiplication and the three divisions
4+ -Addition and subtraction
5== != < <= > >=Comparisons
6notLogical negation
7andLogical conjunction
8orLogical disjunction
arithmetic first, then comparisons, then logic
Parentheses override the whole ladder.
Interactive
Reduce an Expression

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.

05

Check Yourself

Question
What Does This Print?
The code
print(not 5 > 3 and 2 > 1)
Pick one.
Challenge
Build Four Expressions

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.

1
Test whether 2026 is divisible by 4, using % and a comparison.
2
Split 145 minutes into whole hours with //, then the leftover minutes with %.
3
Ask whether 17 sits strictly between 10 and 20, in a single chained comparison.
4
Write a condition that is True when a number is even and larger than 100. Use 250 as the number.
Editable expressions
one expression per line
Python console
press Run to evaluate your expressions

Numbers, strings, True, False, parentheses and every operator on this page are accepted.

06

Summary

Recap
What You Can Do Now
1
Pick the right division for the question being asked: / for the decimal answer, // for whole parts, % for the leftover.
2
Predict the type of an arithmetic result, and name the two ways a division can stop a program.
3
Turn a question into a comparison, and keep = and == apart.
4
Order two strings by their character codes, and predict which comparisons across types answer and which stop the program.
5
Combine conditions with and, or and not, and reproduce their truth tables from memory.
6
Apply the precedence ladder to a mixed expression, and add parentheses wherever they help a reader.