Lesson 1 · Unit 02
Conditions
Comparisons, if, elif and else
What you practise
- Comparisons,
and,or,not,if,elif,else, indentation as structure,=against==, values that count as true
Try a question from this lesson
Pick one
>= means "greater than or equal to". The program asks a question about age. What does it show?
age = 15
print(age >= 18)Correct answer
False
Why
A comparison is a question with a yes-or-no answer. 15 is not greater than or equal to 18, so the answer is no, which Python writes as False. That is a bool value.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Comparisons
A comparison gives a bool: True or False.
== asks "are these equal?" and != asks "are these different?". Then there are <, >, <= and >=.
Careful: one = gives a name a value. Two == compare two values.
print(3 < 5) # True
print(3 == 5) # False
print(3 != 5) # True
print("a" == "a") # TrueConcept
if, elif, else
if asks a question. The indented block under it, four spaces in, runs only when the answer is true.
elif ("else if") asks another question, but only when every question before it was false. else takes whatever is left.
Exactly one block runs: the first one whose question is true.
score = 72
if score >= 90:
print("A")
elif score >= 70:
print("B")
else:
print("C")
# shows BConcept
and, or, not
Join two questions with and or or.
and is true only when both sides are true. or is true when at least one side is. not flips a bool: true becomes false.
age = 20
has_ticket = False
print(age >= 18 and has_ticket) # False
print(age >= 18 or has_ticket) # True
print(not has_ticket) # True9 more exercises in the app
- 1× match the pairs
- 3× multiple choice
- 2× type the answer
- 1× fill the blanks
- 1× true or false
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.