ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
Truthy and falsy
In the last lesson, answer == "yes" or "y" was always true because an if treated
"y" as true. That wasn't an accident: Python can treat any value as true or false.
By the end of this lesson you'll know which values count as false, and you'll use that
to write short, clear checks like "if the user typed something".
Which values count as false
bool() shows how Python treats a value in a condition. You met it in lesson 1.6,
where bool("False") surprised you by being True. Predict all five.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
print(bool(0))
print(bool(""))
print(bool(" "))
print(bool("0"))
print(bool(None))Python prints
False False True True False
Zero is false. Empty text "" is false. But " " holds a space, so it isn't empty: true. "0" is text with one character in it, not the number zero: true. None, the value that means "nothing here", is false.
The rule is short. Zero (0 and 0.0), empty text (""), and None count as false;
so do empty collections, which you'll meet in Module 4. Everything else counts as true,
including negative numbers, "0", and "False". Values that count as true are called
,
and the others
.
ጥያቄ
Which of these is falsy?
If the user typed something
When someone presses Enter without typing, input() gives you empty text, "", which
is falsy. So if name: means "if name isn't empty". The Input box below has one
empty line: the user pressed Enter without typing.
name = input("Your name: ")
if name:
print(f"Selam, {name}!")
else:
print("You didn't type a name.")In the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
Now type Meron on that line and run it again. if name: is the usual Python way to
write if name != "":. Both work; the short one is what you'll see in real code.
Checking for None
None means "no value yet". A name that is still None can't be used like a number:
To check for None, write is None, not == None. is asks "is it the very same
thing?", and there is only one None, so is asks exactly the right question. For
now, use is only with None; for everything else, use ==.
Why not just if not discount:? Because a discount of 0 is falsy too, and 0 is a
real value, not a missing one. is None tells the two apart.
A default with or
A preview for later, one example only. or gives back the first truthy value it finds
(that's why False or "y" gave "y" in the last lesson). So name or "Guest" gives
name if it's not empty, and "Guest" if it is:
name = ""
print(name or "Guest")In the editor, press Escape then Tab to move on.
መልመጃ
Greet the guest
Ask for a name. Greet the person by name, or greet them as Guest if they typed nothing. The Input box starts with an empty answer; try a name too.
Your name:
Selam, Guest!Your code
name = input("Your name: ")
# Greet by name, or as Guest when name is emptyIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
name = input("Your name: ")
if name:
print(f"Selam, {name}!")
else:
print("Selam, Guest!")Using the preview, the last four lines could be one: print(f"Selam, {name or 'Guest'}!").
ዋና ዋና ነጥቦች
- Any value can be used as a condition. Zero, empty text
"",None, and empty collections are falsy; everything else is truthy. "0"," ", and"False"are truthy: they're text with something in it.if name:means "ifnameisn't empty".- Check for
Nonewithis None, and useisonly forNonefor now. name or "Guest"gives a default whennameis empty.