1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Disassembly:

    >>> dis.dis(lambda choice: choice in ("m","M"))
      1           0 LOAD_FAST                0 (choice)
                  3 LOAD_CONST               3 (('m', 'M'))
                  6 COMPARE_OP               6 (in)
                  9 RETURN_VALUE        
    >>> dis.dis(lambda choice: choice in "mM")
      1           0 LOAD_FAST                0 (choice)
                  3 LOAD_CONST               1 ('mM')
                  6 COMPARE_OP               6 (in)
                  9 RETURN_VALUE        
    >>> dis.dis(lambda choice: choice == 'm' or choice == 'M')
      1           0 LOAD_FAST                0 (choice)
                  3 LOAD_CONST               1 ('m')
                  6 COMPARE_OP               2 (==)
                  9 JUMP_IF_TRUE_OR_POP     21
                 12 LOAD_FAST                0 (choice)
                 15 LOAD_CONST               2 ('M')
                 18 COMPARE_OP               2 (==)
            >>   21 RETURN_VALUE        
    >>> dis.dis(lambda choice: choice.lower() == 'm')
      1           0 LOAD_FAST                0 (choice)
                  3 LOAD_ATTR                0 (lower)
                  6 CALL_FUNCTION            0
                  9 LOAD_CONST               1 ('m')
                 12 COMPARE_OP               2 (==)
                 15 RETURN_VALUE        

Timeit results:

    $ python -m timeit -s 'choice="M"' 'choice in ("m","M")'
    10000000 loops, best of 3: 0.197 usec per loop
    $ python -m timeit -s 'choice="M"' 'choice in "mM"'
    10000000 loops, best of 3: 0.166 usec per loop
    $ python -m timeit -s 'choice="M"' 'choice == "m" or choice == "M"'
    1000000 loops, best of 3: 0.246 usec per loop
    $ python -m timeit -s 'choice="M"' 'choice.lower() == "m"'
    1000000 loops, best of 3: 0.494 usec per loop
    $ python -m timeit -s 'choice="m"' 'choice in ("m","M")'
    10000000 loops, best of 3: 0.137 usec per loop
    $ python -m timeit -s 'choice="m"' 'choice in "mM"'
    10000000 loops, best of 3: 0.151 usec per loop
    $ python -m timeit -s 'choice="m"' 'choice == "m" or choice == "M"'
    10000000 loops, best of 3: 0.154 usec per loop
    $ python -m timeit -s 'choice="m"' 'choice.lower() == "m"'
    1000000 loops, best of 3: 0.48 usec per loop
    $ python -m timeit -s 'choice="s"' 'choice in ("m","M")'
    1000000 loops, best of 3: 0.229 usec per loop
    $ python -m timeit -s 'choice="s"' 'choice in "mM"'
    10000000 loops, best of 3: 0.151 usec per loop
    $ python -m timeit -s 'choice="s"' 'choice == "m" or choice == "M"'
    1000000 loops, best of 3: 0.24 usec per loop
    $ python -m timeit -s 'choice="s"' 'choice.lower() == "m"'
    1000000 loops, best of 3: 0.502 usec per loop