# What is the order of operations for logical operators?

Is there an order of operations for control flow? Like does the and come before the multiplication or is there an order for and, or, not, etc. ?

Logical operators have operator precedence the same as other operators (relational, arithmetic, etc.). The highest precedence belongs to `not`, followed by `and`, and finally by `or`.

Like other operations, grouping takes precedence, so we must evaluate bracketed expressions first, if they exist.

http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html

66 Likes

Excuse me but how this operator precedence can be useful? What are some examples of using it actually?

6 Likes
``````print(True or False and False)     # and has precedence
print((True or False) and False)   # parentheses change precedence

print(not False or True)        # not has precedence
print(not (False or True))      # parentheses change precedence
``````

Output:

``````True
False
True
False
``````
51 Likes

Thank you very much. Each time one of my questions is answered i get that "Ooooh now i get it " feeling!!!

27 Likes

Hello, would you explain to me why it prints only one statement?

If youâ€™re referring to the order of operations performed by @patrickd314, then only output is gained after evaluating through the input expressions. So for example,

`print (not False or True)`

Not False is evaluated first to become â€śnot Falseâ€ť aka True. After that the `or` operator takes two True values to output a single outcome of `True`.

6 Likes

Yes, i understand it now, thank you. But for me the first part of

``````print(True or False and False)     # and has precedence
print((True or False) and False)   # parentheses change precedence
``````

was more confusing.
It wasnâ€™t obvious a little that these are not just expressions but parts that require certain answers to be â€śTrueâ€ť or â€śFalseâ€ť. For example:
â€śTrue and Trueâ€ť is True but â€śTrue and Falseâ€ť is False.
So expression with â€śandâ€ť needs to be only â€śTrue and Trueâ€ť to be True. And expression with â€śorâ€ť only requires one element to be â€śTrueâ€ť for it to be True.
In my experience at first i just looked at them as simple comparisons similar to what you can find in math and you just need to solve them. So i thought that â€śFalse and Falseâ€ť had to be â€śTrueâ€ť expression )

4 Likes

Gotcha. Yep, I was confused on this just like you mentioned for like the longest time until I briefly went through the introduction to logic course offered free from stanford:

http://intrologic.stanford.edu/public/index.php

I did not finish the whole thing as the thing becomes very theoretical and very complex towards the end, but covered enough to understand how the boolean operators work within logic. Itâ€™s a pretty good course to check out in general!

9 Likes

Wow, thank you very much!

Good question. Was wondering the same thing.

Thanks, for sharing.

well personally when i first saw i compared to what I know in math. Being that â€śandâ€ť has precedence. In the first expression â€śFalse and Falseâ€ť had to be answered first which is True and whatâ€™s left to process would be â€śTrue or Trueâ€ť which is True. In the second expression, parentheses has precedence so â€śTrue or Falseâ€ť would be answered first which is â€śTrueâ€ť and then â€śTrue and Falseâ€ť, which will answer â€śFalseâ€ť.

Hmmmâ€¦

Yeahâ€¦ but you did the same mistake as i did )
False and False = â€śFalseâ€ť, not â€śTrueâ€ť.
Thatâ€™s what i was talking about, mathematical approach ))

Hi
For the second expression, why would â€śTrue or Falseâ€ť be evaluated to True and not False? Since it is an or statement, wonâ€™t the system have a 50/50 chance of evaluating it to either True or False?

Ahh I just moved on to the next lesson and it mentions that an or statement requires only one component to be True for the entire statement tp be True. Hence, True or False is evaluated as True.

2 Likes

Yea, that was what I was confused about too. Boolean Logic Operations.

I just done this program, but I do not know why it is showing me an error!?

PROGRAM:-

``````                           statement_one = False
``````

statement_two = True

credits = 120
gpa = 3.4

if credits >= 120:
print(â€śYou meet the requirements to graduate!â€ť)
break

if gpa >= 2.0:
print(â€śYou meet the requirements to graduate!â€ť)

I think we use break to make the separation.
And here I m just done the separation of 2 if statements.

``````        **ONE MORE THING**
After removing "break" which is under the first if statements ,
then I got the output.
**BUT STILL I AM NOT ALLOWED TO DO NEXT ATTEMPT! WHY?**
``````

OUTPUT:-
You meet the requirements to graduate!
You meet the requirements to graduate!

if credits >= 120 and gpa >= 2.0:
print(â€śYou meet the requirements to graduate!â€ť)

This is how you should be writing this. If you want to make it clearer you can perhaps add parentheses around the two variables/conditions needing to be met.

1 Like