Tuples checking a letter in a string

I have this code:

prefixes = "JKLMNOPQ" suffix = "ack" for letter in prefixes: if letter in ("O", "Q"): print letter + "u" + suffix else: print letter + suffix 

It works great, but I have a problem understanding one thing. I suppose that:

 if letter in ("O", "Q"): 

creates a new tuple with two letters: O and Q and checks if a letter of value is present.

I am not sure why this will not work correctly:

 if letter == "O" or "Q": 

This code will add ā€œuā€ to all prefixes, not just ā€œOā€ and ā€œQā€.

+4
source share
5 answers

All this does the same:

 if letter == "O" or letter == "Q": if letter in ("O", "Q"): if letter in "OQ": 

Your line if letter == "O" or "Q": evaluates to if (letter == "O") or "Q": and "Q" evaluates to True , so this expression always returns True .

+6
source

Equality comparison takes precedence over the "or" operator: http://docs.python.org/reference/expressions.html#summary

if letter == "O" or "Q":

succeeds if 1) the letter "O" or 2) "Q"

Since "Q" is True, this if statement always holds.

+2
source
 if letter == "O" or "Q": 

This can be rewritten to:

 if (letter == "O") or ("Q"): 

And the last part, a string that is not empty, is always evaluated as True , so your whole expression is True .

So you write it like this:

 if letter == "O" or letter == "Q": 

Or use the code of your tuple (better).

+1
source

The problem is here:

 if letter == "O" or "Q": 

It should be:

 if letter == "O" or letter == "Q": 

The reason you got ā€œuā€ added to all the prefixes was because ā€œQā€ is always true.

+1
source

It is worth noting that even if the priority was different, this would not work:

 if letter == ("O" or "Q"): 

This is not good, since "O" is true-ish *, so "O" or "Q" is ā€œOā€, so we just compare the letter with "O" and completely ignore "Q" .

In English, we consider "O" or "Q" abstract concept that is non-deterministic not "O" or "Q" according to what logic requires. Computers, however, deal with deterministic quantities. Closest to all, can we move on to the English phrasing Is the letter "O" or "Q"? (and more precisely, while we are on it :)) is Is the letter one of the following: "O", "Q"? that we can formalize a bit as Is the letter in the following set: {"O", "Q"}?

Python allows us to easily create sets (with the set keyword), but we still need to be explicit in that we check if there is a letter in this set (i.e. we check for set membership , not equality). Fortunately, we strictly do not need a membership verification kit. (If we had a huge number of elements for testing, and we only needed to build the set once, but I had many letters for testing, then we could save time in general by formally creating set , as they are optimized for test membership; with tuple like ("O", "Q") , Python should check each element one at a time, but here tuple works just fine.)

In short: you fix the problem as others have said, but their explanation of the problem should leave you asking more questions (which I hope I answered here).

+1
source

All Articles