Skip to main content
Engineering LibreTexts

5.10: “Equal To One Of” Trick with the in Operator

  • Page ID
    14501
  • The expression event.key in (K_LEFT, K_a) is just a Python trick to make the code simpler. It is a way of saying "evaluate to True if event.key is equal to one of K_LEFT or K_a". The following two expressions will evaluate the exact same way:

    event.key in (K_LEFT, K_a)
    event.key == K_LEFT or event.key == K_a
    

    You can really save on some space by using this trick when you have to check if a value is equal to one of multiple values. The following two expressions will evaluate the exact same way:

    spam == 'dog' or spam == 'cat' or spam == 'mouse' or spam == 'horse' or spam == 42 or spam == 'dingo'
    spam in ('dog', 'cat', 'mouse', 'horse', 42, 'dingo')
    
    • Was this article helpful?