Skip to content

ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ

and, or, not

3 min read

Real decisions often depend on more than one thing: you can board the bus if you have a ticket and it hasn't left; you get a discount if you're a student or over 60. In this lesson you'll combine yes-or-no questions with and, or, and not, and meet the most famous bug in beginner Python on purpose, so it never catches you.

Two questions at once

Hana has a ticket, but she's late. Predict both lines.

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

has_ticket = True
on_time = False
print(has_ticket and on_time)
print(has_ticket or on_time)
Pick the output

Python prints

False
True

and needs both sides to be true; on_time is false, so the first is False. or needs at least one side to be true; has_ticket is, so the second is True.

and is True only when both sides are True. or is True when at least one side is. Each side is a full question of its own, like age >= 18 or balance > 0.

has_ticket = True
minutes_late = 0
if has_ticket and minutes_late == 0:
    print("You can board")
else:
    print("Sorry, not this bus")

In the editor, press Escape then Tab to move on.

Change minutes_late to 5, then change and to or, and see which answer you get.

The or that is always true

This is the classic Python bug. The user typed "no". What happens?

ውጤቱን ገምቱ

Decide before you look. Guessing wrong is how this sticks.

answer = "no"
if answer == "yes" or "y":
    print("Let's go!")
Pick the output

Python prints

Let's go!

It prints, and it would print whatever the answer was. The next paragraphs show why.

It reads like English, "if the answer is yes or y", but Python doesn't read English. It reads or as joining two separate questions: answer == "yes", and just "y". Watch it work in two steps:

answer = "no"
print(answer == "yes")
print(answer == "yes" or "y")

In the editor, press Escape then Tab to move on.

Step one: answer == "yes" is False. Step two: False or "y" gives "y", and an if treats any non-empty text as true (the next lesson explains why). So the condition is true for every answer. Each side of or must be a complete question:

answer = input("Ready? ")
if answer == "yes" or answer == "y":
    print("Let's go!")

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

Try no in the Input box. In Module 4 you'll meet a shorter way to write this: answer in ("yes", "y").

Python stops as soon as it knows

Python checks the left side first, and if that already decides the answer, it never looks at the right side. False and anything is False, so the right side is skipped. That's useful. Here's a program that averages exam scores, before anyone has taken the exam:

not, and clear conditions

not flips an answer: not True is False, and not age < 18 is the same as age >= 18. Use it when it makes a condition clearer, but prefer questions that are already positive. if is_open: reads better than if not is_closed:, and if age >= 18: beats if not age < 18:.

ጥያቄ

Which condition means n is between 1 and 10, including both?

መልመጃ

Half-price tickets

A ticket costs 200 birr (an example price). Children under 12 and adults over 60 pay half. Ask for the age and print the price. Test 11, 12, 60, and 61: which pay half?

Output
Age: 65
Ticket: 100 birr

Your code

age = int(input("Age: "))
price = 200
# Halve the price for children under 12 and adults over 60

In the editor, press Escape then Tab to move on.

One answer per line, given to input() in order.

መፍትሄውን አሳይ

This is one way to solve it, not the only one. If yours prints the same thing, it works.

age = int(input("Age: "))
price = 200
if age < 12 or age > 60:
    price = price // 2
print("Ticket:", price, "birr")

ዋና ዋና ነጥቦች

  • and is true only when both sides are; or is true when at least one side is.
  • Each side of and / or is a complete question: write answer == "yes" or answer == "y", never answer == "yes" or "y".
  • Python stops as soon as the left side decides, so count > 0 and total / count > 50 never divides by zero.
  • not flips an answer; prefer conditions that are positive to begin with.