Conditions
So far the program ran every line in turn. A condition lets it choose: do something, or skip it.
If
if checks a condition. The lines below it, indented by four spaces, run only when the condition
is true.
temperature = 25 if temperature > 20: say("It is warm today")Open in Studio
It is warm today
The colon at the end of the line opens a block, and the indent shows what belongs to it. You can
compare with > greater, < less, >= greater or equal, <= less or equal, == equal and
!= not equal.
One = puts a value into a name; two == compare. They are different things.
Else, else if
When the condition is false, the block under else runs. Between them you may put as many
else if blocks as you like: Yu takes the first one whose condition is true.
score = 7 if score >= 10: say("Excellent") else if score >= 6: say("Good") else: say("Almost there")Open in Studio
Good
And, or, not
Conditions can be combined. and is true when both parts are true, or when at least one is,
and not turns the answer around.
age = 12 if age > 6 and age < 18: say("Goes to school") say("Not grown up yet:", not (age >= 18))Open in Studio
Goes to school Not grown up yet: true
True, false, nothing
A comparison gives one of two values: true or false. There is also nothing, which is how Yu
says that there is no value.
rain = false say("Rain:", rain) found = nothing say("Found:", found)Open in Studio
Rain: false Found: nothing
Try it yourself
- Check whether a number is even. A hint: an even number divides by 2 with no remainder.
Show the answer
number_to_check = 10 if number_to_check % 2 == 0: say("Even") else: say("Odd")Open in Studio
Even
- A ticket costs 50, and children under 12 go free. Print the price for the age 10.
Show the answer
age = 10 if age < 12: say("Price: 0") else: say("Price: 50")Open in Studio
Price: 0