3
Hello,
I’m learning Python. A question arose in a repetition exercise (for). The exercise is to identify all odd numbers and at the same time multiples of 3, in the range of 0 to 500.
The solution that I initially created was to jump from 3 to 3, thus identifying the multiples of 3. And later identify the different numbers of pairs (ending in: 0, 2, 4, 6 and 8) to remain only the odd:
for c in range(0, 501, 3):
c = str(c)
if c[-1] != str(0) or c[-1] != str(2) or c[-1] != str(4) or c[-1] != str(6) or c[-1] != str(8):
print(c)
However, I noticed that using "or" did not work. Only replacing with "and" did it work. But I did not understand this logic.
In my reading it would be: if the last digit does not end with 0 or 2 or 4 or 6 or 8, then print. But it only worked using: if the last digit does not end with 0 and 2 and 4 and 6 and 8, then print:
for c in range(0, 501, 3):
c = str(c)
if c[-1] != str(0) and c[-1] != str(2) and c[-1] != str(4) and c[-1] != str(6) and c[-1] != str(8):
print(c)
Can anyone explain to me why I use "and" and not "or"? I know there are other ways to solve it, but I need to understand this. Thank you
If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.
– Augusto Vasques