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

Logical expression practice and the if…else operator

Lessons 36–37 · 2 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
36

Practical lesson: logical expressions

Textbook: pp. 75–76
GoalIn the practical lesson, finds the value of logical expressions taking the order of operations into account, and checks numeric and string comparisons in a program.
New words
value of an expression: the result after all operations are carried out; for conditions it is True or False · ifoda qiymatiorder of operations: arithmetic first, then comparison, then not, and, or · amallar tartibistring comparison: == is True only if the strings match exactly, including case and spaces · satrlarni taqqoslashtesting: running the expression in the interactive environment to confirm the hand result · tekshirish
Explanation

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.

★Remember
Order: arithmetic → comparison → not → and → or
💡 Where will I use this?
In real life

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.

Who uses it

Event organisers, librarians and programmers break a complicated condition into parts and check them.

Try it today

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.

Worked examples
Python
1x = 9
2print(x % 2 == 1 and x * x > 50)
Output
True
A numeric expression – code above. It prints True because 9 is odd and 81 > 50.
Python
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 ")
Output
True False True
True False False
Order and strings – the first program above. The result is True False True. Strings – the second program above. Only the first matches exactly: True False False.
Class activity

“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.

Practice
1
Find the value of (2 < 5) and not (3 > 4).
2
If a = 8 and b = a ** 2, what is the value of a < b / 10?
3
Write a program that prints True or False for whether the typed number n is two-digit.
4
Why is "Book" == "book" False?
Story of the Week · The Khwarizmi WayThe decision before springClose the week: all of this week’s subjects in one storyRead →