Lower-case logic words
Python uses the three Boolean operators you met in pseudocode, but it writes them in lower case: and, or and not. Capital letters such as AND are a syntax error. Each side of and or or must be a complete condition, so we write n > 3 and n < 9, never n > 3 and < 9. The rules are the same as before: and is true only when both sides are true, or is false only when both sides are false, and not reverses the result. Python works out not first, then and, then or, and brackets are worked out before everything. Use brackets whenever the meaning could be unclear. A condition that uses these operators goes after if and ends with a colon, just like a simple one.
| Pseudocode | Python |
|---|---|
| IF a > 3 AND b < 9 THEN | if a > 3 and b < 9: |
| IF a > 3 OR b < 9 THEN | if a > 3 or b < 9: |
| IF NOT (a = 3) THEN | if not (a == 3): |
| Height | Age | height >= 130 | age >= 9 | Printed |
|---|---|---|---|---|
| 140 | 10 | True | True | Climb allowed |
| 140 | 8 | True | False | Not yet |
| 120 | 12 | False | True | Not yet |
1height = int(input("Height in cm? ")) 2age = int(input("Age? ")) 3if height >= 130 and age >= 9: 4 print("Climb allowed") 5else: 6 print("Not yet")
Climb allowed
1day = input("Day? ") 2print(day == "Tuesday" or day == "Friday") 3print(not (day == "Tuesday"))
True False
Play the computer (no computer). Write the conditions height >= 130, age >= 9, height >= 130 and age >= 9, height >= 130 or age >= 9, and not (age >= 9) on five cards. Ask a partner for a height and an age. Work out True or False for each card and say the results aloud. Use the numbers (125, 10), (131, 8), (130, 9) and (100, 6). Then write two new conditions of your own using one and, one or and one not. If you have a computer or tablet, ask an adult to run a few of the conditions with print.
1x = 6 2y = 2 3print(x > 5 and y > 5) 4print(x > 5 or y > 5) 5print(not (y == 2))
1if n > 3 AND < 9: 2 print("Between")