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

Making Games with Python and Pygame · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 3.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

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

    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')
    

    This page titled 5.10: “Equal To One Of” Trick with the in Operator was last modified on Sat, 02 Nov 2019 23:07:09 GMT and is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Sweigart via source content that was edited to the style and standards of the LibreTexts platform.