Basic Python repetition - doubt in "and" and "or"

Asked

Viewed 179 times

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.

3 answers

5

The or indicates that if any of the conditions are true, that is enough. For example, if the last digit is 2, the first condition (which tests if it is non-zero) is true and it already enters the if.

With and, all conditions must be true. That is, the number must be non-zero and also different from 2, and different from 4, etc. So it only works with and.


That said, a simpler way is to start from 3, and jump from 6 to 6, so you will always fall into a 3 multiple that is also odd:

for i in range(3, 501, 6):
    print(i)

Of course, if you want to test all multiples of 3, use the rest of the division by 2 to know if it’s odd (you don’t need to convert the number to string):

for i in range(0, 501, 3):
    if i % 2 != 0:
        print(i)

That is, if the rest of the division by 2 is non-zero, it is because the number is odd.

3

Complementing what has already been said, the logical operator or returns True one of its premises being true and the logical operator and returns True only when both premises are true, here are the descriptions and truth tables of the logical operators in python, some more trivial logic operations used in programming language:

Conjunction(AND):
The logical operator and, applied to two operands and returns the logical constant True if both operands are true.

to b a and b
True True True
True False False
False True False
False False False

Disjunction(OR):
The logical operator or, applied to two operands and only returns the logical constant True if an operand is true.

to b a or b
True True True
True False True
False True True
False False False

Denial(NOT):
The logical operator not, applied to only one operand and returns the logical constant contrary to the logical value of the operand.

to not a
True False
False True

Exclusive Disjunction (XOR):
No direct python equivalent.
It can be performed through the expression (a and not b) or (not a and b), applied to two operands and only returns the logical constant True when the operands are not equal.

to b (a and not b) or (not a and b)
True True False
True False True
False True True
False False False

Disjunctive negation (NOR):
No direct python equivalent.
It is performed through the expression not(a or b), applied to two operands and only returns the logical constant True when the operands are False.

to b not(a or b)
True True False
True False False
False True False
False False True

0

To identify different numbers of pairs, you can use the function below:

for c in range(1, 501, 2): # Somente números ímpares
if c % 3 == 0: # Verifica se numero ímpar é divisível por 3
    print(c, end=' ')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.