Truthy, True, Falsey, and False
While comparisons check for truthiness, something being truthy is not the same as it being True
. The inverse of that statement is also true about falsey values and them not being False
. So we need to be vigilent when we are checking is something is True
or False
vs truthy or falsey. In Python, we have the is
operator to check if two things are identical. This time we'll be using a penguin details record dictionary which has the same keys as the prior exercise (species
, flipper_length
, body_mass
, sex
) with the tracked
key that has a boolean value.
We loaded a dictionary, penguin_305_details
, with all the details of a singular penguin's data.
This exercise is part of the course
Data Types in Python
Exercise instructions
- Check the truthiness of
penguin_305_details
sex
key.- If true, check if
sex
isTrue
and store it assex_is_true
. - Print the
sex
key andsex_is_true
.
- If true, check if
- Check the truthiness of
penguin_305_details
tracked
key.- If true, check if
tracked
isTrue
and store it astracked_is_true
. - Print the
tracked
key andtracked_is_true
.
- If true, check if
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Check the truthiness of penguin_305_details sex key
if ____["____"]:
# If true, check if sex is True and store it as sex_is_true
sex_is_true = penguin_305_details["sex"] ____ ____
# Print the sex key's value and sex_is_true
print(f"{____['____']}: {____}")
# Check the truthiness of penguin_305_details tracked key
if ____["____"]:
# If true, check if tracked is True and store it as tracked_is_true
tracked_is_true = penguin_305_details["tracked"] ____ ____
# Print the tracked key and tracked_is_true
print(f"{____['____']}: {____}")