A brief note on Making Decisions: the six comparisons, if, elif and else, the words and, or and not, ranges and in, decisions inside decisions, and testing every branch. With a worked example, practice, a program and questions to check yourself.
A brief note on Making Decisions. Read it before a test. Try each question at the end before you open its answer.
| Sign | Asks | Example | Gives |
|---|---|---|---|
== | equal? | 5 == 5 | True |
!= | not equal? | 5 != 5 | False |
< | less than? | 3 < 8 | True |
> | more than? | 3 > 8 | False |
<= | less, or equal? | 8 <= 8 | True |
>= | more, or equal? | 7 >= 9 | False |
True or False. One = stores a value; two == ask a question.can_vote = age >= 18.if condition: is followed by indented lines, four spaces in. They run only when the condition is True.else: runs when the condition is False. Only one of the two parts runs.elif means "else if". Python asks the conditions from the top and stops at the first True.else at the end catches everything that is left. Put the most special condition first.number % 2 == 0 is True for an even number.A and B needs both to be True. A or B needs at least one. not A turns the answer around.not first, then and, then or. Use brackets when you mix and with or.and, or a chain: 13 <= age <= 19.x in ("red", "blue") asks whether x is one of the values in the group.if is asked only when the outer condition is True. The indent shows which else belongs to which if.| A | B | A and B | A or B |
|---|---|---|---|
| True | True | True | True |
| True | False | False | True |
| False | True | False | True |
| False | False | False | False |
not (A and B) is the same as (not A) or (not B). not (A or B) is the same as (not A) and (not B).else, and = written for ==.A school library fines late books. A book returned on time costs nothing. Up to 7 days late costs 2 rupees a day. More than 7 days late costs 5 rupees for every day.
days_late <= 0 true? 9 is not, so Python moves on.days_late <= 7 true? 9 is not, so Python moves on again.else runs. The fine is 9 * 5, which is 45 rupees.| Days late | Expected fine |
|---|---|
| 0 | 0 rupees |
| 3 | 6 rupees |
| 7 | 14 rupees |
| 8 | 40 rupees |
The edge rows catch a slip at once. With days_late < 7 in place of days_late <= 7, a book 7 days late would cost 35 rupees, not 14 rupees.
The program gives advice for the walk to school from the temperature and the rain.
= for ==. if x = 5: stops with SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?if x == 5 with no colon stops with SyntaxError: expected ':'.if that is not pushed in gives IndentationError: expected an indented block after 'if' statement.if age > 10 and < 15: is a SyntaxError. Each comparison needs both sides: age > 10 and age < 15.day == "Sat" or "Sun". It is always True, because "Sun" on its own counts as true. Write day == "Sat" or day == "Sun", or day in ("Sat", "Sun").marks >= 50 comes before marks >= 80, nobody gets the top grade."Mango" < "apple" True or False? Why?if size == "S" or "M": to find small and medium shirts. Every size passes the test. Why, and how should she write it?if t > 30: for hot, elif t > 20: for warm and else: for cool, with t in degrees Celsius. Which values of t should its test table hold?not (x >= 5 and y >= 5) without the word not.if a > 0: holds an inner if b > 0: that prints P, with an inner else that prints Q. The outer else prints R. What is printed when a is -2 and b is 5?Log in to join the discussion.
Loading…