Introduction to Python | Conditional Statements| Screen 6

https://app.dataquest.io/c/57/m/313/conditional-statements/6/the-or-operator

What is the difference between

if genre == ‘Social Networking’ or genre == ‘Games’:
games_social_ratings.append(rating)

if genre == ‘Social Networking’ or ‘Games’:
games_social_ratings.append(rating)

The above will return a True if genre is either the string "Social Networking" or the string "Games".

The above is a bit more difficult to understand at first. But the above uses the or operator on the following two -

  • If genre is the string "Social Networking", or
  • If "Games"

Notice the second one carefully. It does not check if genre is the string "Games".

It only checks if "Games". That is, it checks if that string exists or not. Since it’s a valid, non-empty string, the above if condition will always be True irrespective of whether or not genre is the string "Social Networking".

Just to clarify a bit further.

Let’s say genre is "Social Networking". So, the two parts of your if condition equate to -

  • True
  • True

So, the if condition is True.

Now, let’s say genre is not "Social Networking". So, the two parts of your if condition equate to -

  • False
  • True

So, the if condition is True.

Now, let’s say your code is -

if genre == "Social Networking" or "":

Assume that genre is not "Social Networking". So, the two parts of your if condition equate to -

  • False
  • False (being the string is empty now, which Python evaluates to as False)

This can be confusing to understand, so take your time with it.