Launch School Python Basics, conditionals exercise 1:
Without looking at your notes or the official documentation, try to recall all values that count as falsy in Python.
Oh boy. And if you’re a software engineer and know Python: can you recall all?
After three minutes I had this:
None
0
Fаlse
negative something? no...
negative absolute value? is that a thing?
emptiness that isn't 'None'?
Answer:
False
None
0, 0.0, 0j, and other numeric zero-values
'' (an empty string)
[] (an empty list)
{} (an empty dictionary)
() (an empty tuple)
set() (an empty set)
frozenset() (an empty frozen set)
range(0) (an empty range)
I’ll give myself a 3.5 out of 9.
0 is falsy, 1 is true-y. And the shortest way to write the following if statement isn’t this:
import random
random_number = random.randint(0, 1)
if random_number == 1:
print('Yes!')
else:
print('No.')
You don’t need the “ == 1 “. This is correct:
if random_number:
print('Yes!')
else:
print('No.')
True is the “default” state, so we do not need to specify under which conditions something is True.
This syntax, even on the fourth look at it, I don’t completely like; I know I’m a super-beginner, etc.
This randint randomizes between the integers 0 and 1. We give that value to a variable. If it’s 0, then “if random_number” is False, and we process “else”.
But that kind of makes it look like there is no random_number variable at all any more (though it exists, with the value 0).
(But this is a programming language, not English; you can’t complain too much.)
>This syntax, even on the fourth look at it, I don’t completely like; I know I’m a super-beginner, etc.
Nah, don’t beat yourself up. It just sucks in general.
The stages usually go as follows:
I don’t know how it works so it must be good -> i finally understand it. it’s good! -> i mastered it, and it’s bad.