IniziaInizia gratis

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.

Questo esercizio fa parte del corso

Data Types in Python

Visualizza il corso

Istruzioni dell'esercizio

  • Check the truthiness of penguin_305_details sex key.
    • If true, check if sex is True and store it as sex_is_true.
    • Print the sex key and sex_is_true.
  • Check the truthiness of penguin_305_details tracked key.
    • If true, check if tracked is True and store it as tracked_is_true.
    • Print the tracked key and tracked_is_true.

Esercizio pratico interattivo

Prova a risolvere questo esercizio completando il codice di esempio.

# 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"{____['____']}: {____}")
Modifica ed esegui il codice