FAQ: Control Flow - Boolean Operators: not

This community-built FAQ covers the “Boolean Operators: not” exercise from the lesson “Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Boolean Operators: not

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

A post was split to a new topic: Are parenthesis a syle choice?

2 posts were split to a new topic: Why doesn’t this code pass?

3 posts were split to a new topic: Why should I use NOT?

2 posts were split to a new topic: What is an example of the not boolean?

A post was split to a new topic: About example of not operator

Aren’t there multiple solutions to this exercise? e.g. I used the below without using any not operators:

statement_one = False

statement_two = True

def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return “You meet the requirements to graduate!”
if (gpa >= 2.0) and (credits < 120):
return “You do not have enough credits to graduate.”
if (gpa < 2.0) and (credits >= 120):
return “Your GPA is not high enough to graduate.”
if (gpa < 2.0) and (credits < 120):
return “You do not meet either requirement to graduate!”

Yes, but the point of the exercise is to teach what not does, to think about it and apply it. Something like not in or is not may have been better to use Vs a conditional statement where you can just flip a less than / greater than and add an equals to get the same effect as using not.

Hi, the line on 8 and 12 shows “and not” operator, this is never mentioned anywhere. How does this actually work? Also why exactly do i get a syntax error when i try to use “not” only? Thanks!

Consider,

A not B

Let’s enter it into the interactive shell…

>>> A = 1
>>> B = 0
>>> A not B
SyntaxError: invalid syntax
>>> 

As written there is nothing to connect A to B since not is only right looking and does not see left.

Now let’s see if we can connect A to B…

>>> A and not B
True
>>> 

According to operator precedence, the not expression is evaluated first, so we get,

1 and True

and being as 1 is truthy, the final yield is, True.

Hi mtf,

Thank you for helping me out!
I also have some questions just so i can be clear on this topic.

  • Does “and not” just connect left with right or doest it also have a different meaning?
  • Is “not” the only boolean operator that is right-looking?

I thank you in advance!

1 Like

Yes, it is the only one. We know it as a unary operator, as opposed to the binary operation of and and or.

1 Like

and is separate from not. and not is an and operation and a not operation.

See this for the order of operations:
https://docs.python.org/3/reference/expressions.html#operator-precedence

It also explains they are all left to right, apart from exponentiation.

Back to and not. not flips a boolean (or something that will return a boolean) to the opposite values (true to false and false to true). and compares two booleans to see if they are both true.

As and aside, is not and not in are operators in their own right and are not the same as not by itself. They would be the same as not(a is b) or not(a in b).

1 Like

It isn’t the only one, some example of others are - and ~.

1 Like

Yeah, I was going to bring up Bitwise, but opted to wait that one out until it is introduced.

1 Like

Thanks guys! This was really helpful.

1 Like

I am absolutely stuck on this lesson and am 100% sure I am wrong. This is what I wrote out, Thanks for your help in advance!

def graduation_reqs(gpa, credits):
  if (gpa >= 2.0) and (credits >= 120):
    return "You meet the requirements to graduate!"
  if not (gpa >= 2.0) and (credits >= 120):
    return "You do not have enough credits to graduate."
  if not (gpa >= 2.0) and (credits >= 120):
    return "Your GPA is not high enough to graduate."
  if not (gpa >= 2.0) and (credits >= 120):
    return "You do not meet either requirement to graduate!"
``
# Everything is properly indented in the code in the lesson but copy and paste removes them. Thanks.

I am thinking the same thing as guy above, I can do it how he did it but I cannot for the life of me figure out how to incorporate “not” correctly

Hello, @bmp41690, and welcome to the forums.

not simply flips a bool value.

Your if statement below would read in English like so:
if gpa is not greater than or equal to 2.0 and credits is greater than or equal to 120, then return, "You do not have enough credits to graduate." That is not correct. The described condition would mean that you don’t have a high enough gpa to graduate, so how could you fix it? What about moving not?
You need it to read:
if gpa is greater than or equal to 2.0 and credits is not greater than or equal to 120, then return, "You do not have enough credits to graduate." Write that in code.

The last if statement where neither condition is met is also not correct. If you want not to apply to the entire comparison, you can enclose the whole thing in parenthesis. For example:

>>> False and False
False
>>> not False and False #not False is evaluated first, so we have True and False which is False
False
>>> not (False and False) #by using parenthesis, False and False is evaluated first which is False then not flips it to True
True

Thank you so much for your reply! I guess the part that is confusing me the most is the place of not, if I were to add it the first half, which would be the gpa half, then the not flips the whole statement while if I put it after the *and then it only flips the second half ie. the credits side. Is that correct?

1 Like