How to Boost your Logic Skills (Series)? The “in” and “is” Keywords.

What is the output of this puzzle?

 

meal_1 = "Meat"
meal_2 = "Flaxseeds"
meal_3 = "Marshmallows"

healthyFoods = ["Kale",
                "Apple",
                "Strawberry",
                "Banana",
                "Flaxseeds"]

def isHealthy(food):
    return food in healthyFoods

m_1 = isHealthy(meal_1)
m_2 = isHealthy(meal_2)
m_3 = isHealthy(meal_3)

print((not m_1 or m_2) and
      (meal_2 is "Flaxseeds") and
      isHealthy("Kale"))

 

This puzzle shows two new Python concepts: the ‘in’ and the ‘is’ keywords. Their intuitive use contributes heavily to the success of the Python programming language.

The keyword ‘is’ takes two arguments A and B and is used in the form ‘A is B’. It evaluates to True, if both arguments point to the same object. For immutable data types like strings and integers, it is enough if they have the same value. For example, the expression ‘5 is 5’ evaluates to True. Yet, the expression ‘[5] is [5]’ evaluates to False because the arguments point to a different mutable list object.

In a similar manner, the keyword ‘in’ takes two arguments A and B and is used in the form ‘A in B’. It evaluates to True, if the first argument A is contained in the second sequence argument B. For example, the expression ‘5 in [1,2,5]’ evaluates to True. Yet, the expression ‘5 in [1,2,3]’ evaluates to False.

The final obstacle in the puzzle is the boolean logic confusion with the simple purpose of advancing your logic skills. With the information provided previously (and your best intuition), you should be able to solve it. Note that you must evaluate the expression within the brackets first.


Are you a master coder?
Test your skills now!

Related Video

Solution

True

 

Leave a Comment

Your email address will not be published. Required fields are marked *