Help With Not Boolean Operators

I’m on the introduction of not boolean operators and I’m not quite understanding the language formatting for lesson one.

Below is the lesson.

= = = = =

if not credits >= 120:
print (“You do not have enough credits to graduate.”)

if not gpa >= 2.0:
print (“Your GPA is not high enough to graduate.”)

if not (credits >= 120) and not (gpa >= 2.0):
print (“You do not meet either requirement to graduate!”)

= = = = =

:point_down: Below’s how I coded the third if not statement. :point_down:

if not credits >= 120 and not gpa >= 2.0:
print (“You do not meet either requirement to graduate!”)

But it said the formatting was wrong. So I had to add the parenthesis.

I thought because they were variables and not a print function, they didn’t need parenthesis in the third boolean. But they did. Why?

Is it because the first two were independent if not booleans, but because they were combined and being compared against one another, with an “and” they required parenthesis?

For extra study look up the Operator Precedence to determine which comes first. Does >= have precedence to not? If it does then the parens are not needed, but if not has greater precedence the brackets are needed to allow the comparison to come ahead of not.

In the third condition, ask yourself, “How would the thing running the code know where the first sub-condition ends and the second subcondition begins?”

Remember, computers rely on us, through programming, to tell them what to do and how to do it. The computer doesn’t know the above unless you very explicitly tell it. Imagine, for example, if we didn’t have an order of operations in math, and I told you to compute: 12 * 4 + 7(2/3). You would come up with a different answer than someone else unless I told you what the order of operations was.

Study the table in the docs:

6. Expressions — Python 3.12.0 documentation

Note that not is at the same precedence level as the comparison operators. Given that, Python will evaluate the expression from left to right. That means not gpa will be evaluated first, and not the comparison. For this reason, the parenthesis are definitely required so that not is applied to the evaluation of the comparison, and not the first operand.

not gpa >= 2

will always be falsy since not anything is always a boolean and boolean integers have only two possible values, 0 or 1. Yes, in Python True and False are also int type.

>>> isinstance(True, int)
True
>>> isinstance(False, int)
True
>>> int(True)
1
>>> int(False)
0
>>> 

It won’t matter what the actual value of gpa is. Now,

not (gpa >= 2)

will be operating on a boolean, which it will then toggle. The comparison operation must be completed first.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.