The Six Comparisons

You rarely type True by hand. Booleans usually arrive as the answer to a comparison. Six operators produce them:

> greater than · < less than

>= greater or equal · <= less or equal

== equal to · != not equal to

✦ The one that bites everybody

One equals sign (=) ASSIGNS a value. Two equals signs (==) COMPARE two values and give back a Boolean. age = 18 stores 18. age == 18 asks a question. Mixing them up is the single most common beginner bug in every language.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Add a fifth line that checks whether age is at least 21. Then change age to 18 and run it again to see which lines flip.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Equality

PYTHON

age == 18   # one way to compare

JAVASCRIPT

// strict: value AND type must match
age === 18
// loose: converts types first, avoid it
age == 18
Compare → This is a genuine LANGUAGE-SPECIFIC difference, not just syntax. JavaScript has two equality operators. The loose == will say "5" == 5 is true, converting the string into a number behind your back. Always use === in JavaScript. Python's == compares sensibly and needs no strict variant.
EXAMPLE
loading editor…
YOUR TURN
Your turn → Compare the number 10 with the text "10", using === in JavaScript and == in Python, and print the result. Then compare 10 with 10.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: AND, OR, NOT: Combining Conditions →
← Back