AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ 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.

SignMeaning
==equal
!=not equal
<less
>greater
<=less or equal
>=greater or equal
💡 Where will I use this?
In real life

A pupil wrote a rule for a class quiz: a team goes through to the next round if its score is more than 20 and it made fewer than 3 mistakes. The program judged each team’s result as True or False, so there was no argument.

Who uses it

Programmers, testers and system designers judge conditions as True or False to control access.

Try it today

Write one permission at school or home with two conditions, such as “age ≥ 10 and permission given”. Mark True or False for five different cases.

Worked examples
Python
1x = 20
2print(x == 20, x != 20, x < 10, x >= 20)
Output
True False False True
Comparison – code above. For x = 20 the results are: True False False True.
Python
1a = 14
2print(a >= 10 and a <= 15, a < 10 or a > 18, not a == 14)
3print("kit" in "kitob", "Kit" in "kitob")
Output
True False False
True False
Logic and in – code above. 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 =?
Story of the Week · The Khwarizmi WayWhat a simple agreement must sayClose the week: all of this week’s subjects in one storyRead →