Course 3.2Lesson 1 of 5
and, or and not join conditions. A and B is True only when both are True. A or B is True when at least one is True. not A turns True into False and False into True. In a long condition, Python does not first, then and, then or, and brackets change the order. Write each comparison in full: age >= 18 and age <= 60 is right, and age >= 18 and <= 60 is a SyntaxError.
Try the code and the simulations beside the lesson.
After this lesson you can
and and with or,not,To enter a club, a person must be 18 or older and must have a ticket. To get a discount, a person must be a student or a senior citizen. To go out to play, it must not be raining.
Each of these needs a combined condition. Python has three words for it: and, or and not. They work on values that are True or False, and they give a new True or False.
A and B is True only when both A and B are True. If one of them is False, the answer is False. Think about the club again: if you are old enough but have no ticket, you cannot enter. The word and is strict.
A or B is True when at least one of them is True. It is False only when both are False. The word or is generous. In everyday English, "or" sometimes means "one of them but not both" (tea or coffee). In Python, or also allows both.
A | B | A and B | A or B | not A |
|---|---|---|---|---|
True | True | True | True | False |
True | False | False | True | False |
False | True | False | True | True |
False | False | False | False | True |
Look at the column of and: only one True, in the first row. In the column of or: only one False, in the last row. This is the whole idea. A table like this is called a truth table, and in a later lesson you write them yourself.
Each side of and and or is a full comparison: age >= 10 and height >= 140, never age and height >= 140.
What does a or b and c mean? There is an order, like in arithmetic where multiplication goes before addition: not goes first, then and, then or. And brackets go before all of them.
A safe habit: when you mix and with or in one condition, always use brackets. It costs nothing, and a reader of your program (you, next week) does not need to remember the order.
A very common mistake: age >= 18 and <= 60. In English we say "18 or more and 60 or less", but Python needs each comparison to have both sides: age >= 18 and age <= 60. You will see this again in the next lesson, with a shorter form.
x > 1 and < 5. Write x > 1 and x < 5.and and or without brackets, and trusting your memory of the order.&&, || or ! from other languages. Python uses the English words.or means one but not both.= instead of == inside the combined condition.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
A and B needs both. A or B needs at least one. not A turns it around.not, then and, then or. Use brackets when you mix and with or.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
True and False? And True or False? And not False?A or B False?if age > 12 and < 20:.Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.