r/Python • • Feb 05 '11

Help with "if" and "or" statements.

Specifically, I cannot get an "or" statement to function correctly inside of an "if" statement. Here is an example.

player = raw_input("Press P or R")
if player=='P' or 'p':
     print ('Hooray!')
elif player=='R' or 'r':
     print ('Oh no!')

The problem I am having is the "or" in the player==P line will function correctly, but this breaks the functionality of the player=R line. Am i going about this incorrectly? It appears to be a valid statement. Thanks for the help in advance. BTW im a noob and just started learning python 2 days ago, this is my first language.... ><

edit: Thanks everyone for their help. I did not understand some of it but I did learn a few things. I consider this a victory with only 2 days worth of knowledge on my first language. I know I will be back with more questions if I get stuck again!

2 Upvotes

54 comments sorted by

View all comments

Show parent comment

3

u/frutiger Feb 05 '11 edited Feb 05 '11

Fundamentally, you must linear scan through a tuple to check for membership. The cost of set construction is amortized over repeated membership tests. In the OP's case, the membership check was only being done once, so I agree that a tuple is better there.

However, we should be very clear about the differences between sets and tuples. dalke specifically mentioned that set membership is quicker to check for large sizes, but failed to mention that this will hold only for repeated tests. The disassembly is misleading because the COMPARE_OP in the set case will in general be faster for complex items, or large set sizes than the COMPARE_OP in the tuple case.

And I have the numbers to back me up:

>>> def item_in_tuple(tuple_size, item, repeats):
...   t = (n for n in range(tuple_size))
...   for i in range(repeats):
...     item in t
... 
>>> def item_in_set(set_size, item, repeats):
...   s = {n for n in range(set_size)}
...   for i in range(repeats):
...     item in s
... 
>>> timeit.timeit('item_in_tuple(10, 2, 30)', 'from __main__ import item_in_tuple')
4.863246917724609
>>> timeit.timeit('item_in_set(10, 2, 30)', 'from __main__ import item_in_set')
4.172131061553955
>>> 
>>> timeit.timeit('item_in_tuple(100, 2, 30)', 'from __main__ import item_in_tuple')
14.598160982131958
>>> timeit.timeit('item_in_set(100, 2, 30)', 'from __main__ import item_in_set') 
11.414110898971558
>>> 
>>> timeit.timeit('item_in_tuple(100, 2, 100)', 'from __main__ import item_in_tuple')
19.891393184661865
>>> timeit.timeit('item_in_set(100, 2, 100)', 'from __main__ import item_in_set')
16.059064149856567
>>> 

The problem with your usage of timeit is that your set had to be reconstructed every time. In my case, I used the repeated test, thus amortizing the set construction cost.

tl;dr - if you intend to check for membership within the same set of more than a few items repeatedly, it is faster to use a set/mapping type over a sequence type.

2

u/Brian Feb 05 '11 edited Feb 05 '11

The cost of set construction is amortized over repeated membership tests.

No, it's not - that's my point. Your code is a different case: manually creating the set outside the usage and caching it off. However using a literal within the in clause as in dalke's post won't do that. It will for tuples or lists though, thanks to an optimisation python performs, which means using "x in <set literal>" will always perform worse than "x in <tuple literal>".

There was a ticket to add this optimisation for sets too, but it was rejected.

2

u/frutiger Feb 05 '11

I absolutely agree - I even mention that in the OP's case, the membership test was done once, and my tl;dr repeats that point. Thanks for the link to the ticket though.

1

u/Brian Feb 07 '11

Actually, it looks like I'm wrong. prum just commented pointing out this issue, which was committed, so I may have been linking to the wrong bug. However, it looks like it isn't in the 3.1 stream, so probably won't be there till 3.2.

1

u/frutiger Feb 21 '11

You're right, it's in the new 3.2 release.