Lesson 7 of 30
In progress
Comparisons, and, or and not
Compare values with confidence, combine conditions with and, or and not, and understand why some values count as false.
Two conditions that look the same can behave differently: if name: and if name == "Ana": are not the same question. Getting comparisons right is what makes a decision trustworthy.
What you will learn
- Use
==,!=,<,>,<=and>=correctly. - Combine conditions with
and,orandnot, and know which one wins. - Recognise the values Python treats as false.
Equality is not identity
== asks whether two values are equal. is asks whether they are the same object in memory. For numbers, strings and booleans, use == almost always.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(a == b)
print(a == c)
print(a is b)
Press Run to see the output.
10 == 10.0 is true because the numeric values are equal: Python widens the integer for the comparison. 10 == "10" is false because a number is not text. Do not "fix" that with int(c) unless you actually want to convert; comparing different types is often a sign that a value arrived from input() without conversion.
Text comparison is case-sensitive: "Ana" == "ana" is false. Normalise with .lower() or .casefold() when the comparison should ignore case.
Ordering operators
<, >, <= and >= work on numbers, text and dates. Text is compared character by character using Unicode order, which is rarely what a person means by alphabetical.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
Press Run to see the output.
The last line is true because uppercase letters sort before lowercase ones. When sorting names for display, use sorted(names, key=str.lower) later in the track.
You can chain comparisons, which reads better than joining two of them:
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
if 13 <= age <= 19:
print("Teenager")
Press Run to see the output.
Combining conditions
and is true only when both sides are true. or is true when at least one side is true. not flips a boolean.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
print(age >= 13 and has_permission)
print(age >= 18 or has_permission)
print(not has_permission)
Press Run to see the output.
Python stops evaluating as soon as the answer is certain. In age >= 18 and has_permission, if the first part is false, the second is never checked. This short-circuit behaviour is useful, not just an optimisation: it lets you guard an expression.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
if name and len(name) > 3:
print("Long enough")
Press Run to see the output.
The name and part prevents the rest from mattering when the name is empty. It is not needed here because len("") is safe, but the pattern protects calls that would fail on empty input.
and binds tighter than or, just as multiplication binds tighter than addition. a or b and c means a or (b and c). Add brackets when a reader could hesitate.
Truthiness: what counts as false
Python treats these as false: False, None, 0, 0.0, "", [], {}, set() and range(0). Everything else is true.
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
for value in values:
print(repr(value), bool(value))
Press Run to see the output.
"0" is true because it is a non-empty string. [0] is true because it is a non-empty list. This trips people up when they mean "the value is zero" but write if value: and catch every non-empty thing.
When the difference matters, be explicit: if count > 0: rather than if count:. If a function expects a list, an empty list is still a list; test is not None when presence, rather than length, is the question.
Writing if answer == "yes" or "y" looks reasonable and is always true. Python evaluates "y" as a non-empty string, which is truthy, so the condition succeeds for every answer. Write if answer in ("yes", "y"): instead.
A learner can enter the lab if they are at least 13 and either have a permission slip or are accompanied by a guardian. Write the condition and test it with three different combinations of values.
Show solution
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.
if age >= 13 and (has_slip or with_guardian):
print("Allowed in")
else:
print("Not allowed")
Press Run to see the output.
The brackets matter. Without them, and runs before or, so a 10-year-old with a slip would be allowed. Change the three values and run it again to see each branch.
Recap
==compares values;iscompares identity and is rarely what you want for text or numbers.andrequires both sides,orrequires one,notflips, and brackets control the order.- Empty and zero values are false, so write explicit comparisons when zero is meaningful.
Next, you will repeat work without copying lines, starting with for loops and range.
Why is '0' true in Python?
Does and always check both sides?
Progress is saved in this browser only. No account, no email.