Boolean operators Python

6

Hello my question is a very simple question, I have these two expressions and let me analyze them to see if I do it correctly:

False or not (True and True)
False

In this first expression following the recommended order, I first analyze not and I have to compare it with the result of the two operators in parentheses True and True that would be True, so not True is False, and finally I compare it with or, would be False or False is False.

The doubt is in this:

not not True or False and not True
False

I'm noticing True, not True is False, not False is True, not True is False, True and False is False and False or False is False.

The question is why the second one in the Python console gives me True. Thanks

    
asked by Vicente 01.10.2016 в 17:19
source

3 answers

4

Your logical reasoning is correct and you apply the order well, your mistake seems to me to be that you have just messed up between True and False:

You say the following:

  

I analyze not True, not True is False, not False is True, not True   is False, True and False is False and False or False is False.

Let's see,

  

not not True, not True is False, not False is True ...

Correct, therefore what is before the or is a True .

  

not True is False, True and False is False ..

Here is an error, not True is False, but what is before and is a False so it is False and False, so what is after the or is False

  

and False or False is False.

Here is the error, before or there is a True as you indicate.

Step by step it would be like this: As you know, the logical operators have an order of priority:

1º not
2º and
3º or

Your sentence is:

not not True or False and not True

First we have to resolve the denials:

not not True or False and not True
not False or False and not True >> Dado que not True = False
True or False and not True >> Dado que not False = True
True or False and False >> Dado que not True = False

Now we evaluate the and:

True or False and False 
True or False >> Dado que False and False = False

Finally we evaluate the or:

True or False
True
    
answered by 01.10.2016 / 22:03
source
3

The expression not not True or False and not True gives True , simply because you have a or .

or , denotes that only one of the two main expressions needs to be true. That said, Python will execute the first expression not not True , then as this results in True , python will no longer check the next expression; False and not True So the final result is True , regardless of whether the other parts are False .

The order of reasoning would be like this:

not not True or False and not True
              ↓
not False or False and not True
              ↓
# Python encuentra que la primera expresion
# ya es True, así que deja de ejecutar sigue
# despues del operador or.
True or False and not True
              ↓
True

Above I speak of main expression, because basically the original code is equivalent to:

>>> (not not True) or (False and not True)

Where we can see that there are two groups separator by or . Then in the second group there are two sub groups (False) and (not True) .

>>> (not not True) or ((False) and (not True))

All these expressions are the same, and should return True .

    
answered by 01.10.2016 в 21:01
2

It is because of the operational priority of Python that it follows the following order:

1st not (Denial) 2nd and (Conjunction) 3rd or (disyuncion)

    
answered by 01.10.2016 в 18:00