Practical lesson: logical expressions
When finding the value of a compound logical expression the order of operations matters: first the arithmetic operations, then comparison, then not, and, and finally or. So in True or False and False the and works first (False and False = False) and then the or: True or False = True. Brackets change the order: (True or False) and False is False. Splitting the expression into parts and writing the value of each part in the notebook reduces mistakes. When comparing strings == gives True only on an exact match: letter case and a trailing space count too. Check the answer you found by hand in the interactive environment of IDLE; if they differ, recalculate the expression step by step.
At a school concert the door rule was “(has a ticket or is a performer) and has no large bag”. The monitor checked each person part by part, in the right order, and nobody was let in or turned away by mistake.
Event organisers, librarians and programmers break a complicated condition into parts and check them.
Write a three-part condition, such as “no rain and not too hot and time available”, and find True or False for five cases on paper.
1x = 9 2print(x % 2 == 1 and x * x > 50)
True
1print(True or False and False, (True or False) and False, not False or False) 2 3name = "Toshkent" 4print(name == "Toshkent", name == "toshkent", name == "Toshkent ")
True False True True False False
“Expression chain”: the group writes each other a logical expression with and without brackets (3–4 operations); the partner calculates it by hand in parts and then in IDLE.