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 fine but I have problem understanding one thing. I assume that:
if letter in ("O", "Q"):
creates new tuple with 2 letters: O and Q and checks if value letter is present.
What I’m unsure about is why this won’t work correctly:
if letter == "O" or "Q":
This code will add “u” to all prefixes and not just those with “O” and “Q”.
All these do the same thing:
Your line
if letter == "O" or "Q":is evaluated likeif (letter == "O") or "Q":, and"Q"evaluates toTrue, so this expression always returnsTrue.