☰ Contents · Computing

Simple task practice and logical expressions

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

Programming logical problems in Python

Textbook: pp. 73–75
GoalKnows the comparison operations (==, !=, <, >, <=, >=), the logical operations (and, or, not) and the in operation, finds their bool result and writes compound conditions.
New words
comparison operation: compares two values and gives True or False · taqqoslash amaliand: True only if both conditions are True · andor: True if at least one condition is True · ornot: changes True to False and False to True · not
Explanation

A comparison operation compares two values and returns a bool result: True or False. The operations are: == (equal), != (not equal), < (less), > (greater), <= (less or equal), >= (greater or equal). Note that = is assignment and == is comparison, so do not mix them up. Logical operations are used for a compound condition: and is True if both conditions are True; or is True if at least one is True; not turns the result to its opposite. The order is: first comparison, then not, then and, and or last; use brackets when in doubt. The in operation checks whether a substring is inside a string, and capital and small letters are different: "kit" in "kitob" is True, "Kit" in "kitob" is False. Strings can also be compared with == and <, and they are compared by the codes of the letters.

Worked examples
Comparison: x = 20 ⏎ print(x == 20, x != 20, x < 10, x >= 20) For x = 20 the results are: True False False True.
Logic and in: a = 14 ⏎ print(a >= 10 and a <= 15, a < 10 or a > 18, not a == 14) ⏎ print("kit" in "kitob", "Kit" in "kitob") For age 14 the condition “from 10 to 15” is True, “less than 10 or greater than 18” is False, not (a == 14) is False; the substring “kit” is present – True, “Kit” is not – False.
Class activity

“True or False”: the teacher says 8 conditions (for x = 6: x > 3 and x < 5, x == 6 or x == 7, not x != 6 and so on). Groups raise cards with True or False; then it is checked in IDLE.

Practice
1
What does print(7 != 7) print on the screen?
2
Write an expression that checks that x = 25 is greater than 10 and less than 30.
3
What is the value of not (3 > 5 or 2 == 2)?
4
Why is == written in a condition and not =?