AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Words that join conditions

Week 8 · 1.4 Conditional statements in text-based programming · 3 lessons
1

Lower-case logic words

Computing 8: Topic 1.4, pp. 70–105
GoalWrite conditions with and, or and not in Python and work out their results.
New words
and · true only when both conditions are trueor · true when at least one condition is truenot · turns true into false and false into trueBoolean expression · a condition that works out to True or False
Explanation

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.

PseudocodePython
IF a > 3 AND b < 9 THENif a > 3 and b < 9:
IF a > 3 OR b < 9 THENif a > 3 or b < 9:
IF NOT (a = 3) THENif not (a == 3):
Worked examples
HeightAgeheight >= 130age >= 9Printed
14010TrueTrueClimb allowed
1408TrueFalseNot yet
12012FalseTrueNot yet
A climbing wall lets you climb when you are at least 130 cm tall and at least 9 years old. The two conditions are joined by and. Both must be true.
Python
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")
Output
Climb allowed
The climbing program in Python. With 135 cm and age 9 typed, both conditions are true.
Python
1day = input("Day? ")
2print(day == "Tuesday" or day == "Friday")
3print(not (day == "Tuesday"))
Output
True
False
A short program using or and not. The kit is needed on Tuesday or Friday. The second line shows not. With Tuesday typed it prints True and False.
Class activity

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.

Practice
1
Rewrite the pseudocode condition in Python: IF score >= 50 AND score <= 100 THEN
2
The variables are x = 6 and y = 2. Work out each answer, then check by running Python if you can.
Python
1x = 6
2y = 2
3print(x > 5 and y > 5)
4print(x > 5 or y > 5)
5print(not (y == 2))
3
This line does not start. Find the two mistakes.
Python
1if n > 3 AND < 9:
2    print("Between")
4
Write the Python condition for: the player has more than 2 lives and has not found the key (variable key holds yes or no).