FAQ: Control Flow - Boolean Operators: not

Not quite. If you put it before the expression in front of and, it will flip that side only.

>>> True and not True #same as True and False => False
False
>>> not False and not True #same as True and False => False
False
>>> not (not False and not True) #evaluate inside the parenthesis first, so True and False which evaluates to False thenflip it, so True
True

not will evaluate before and. Just like with math, we can change the order of evaluation by using parenthesis.

Without the parenthesis, the last expression above would be False.

>>> not not False and not True #same as False and False => False
False
1 Like

That definitely helps, but at the same time, I’m back where I was at the start and just can’t get over this hump of this lesson. I’ve been learning so effortlessly and then this wall. EDIT Just finally figured out after re-reading your first response. Thank you so much, you have been immensely helpful!

    return "You meet the requirements to graduate!"
  if (gpa >= 2.0) and not (credits >= 120):
    return "You do not have enough credits to graduate."

  if not ((gpa >= 2.0) and (credits >= 120)):
    return "You do not meet either requirement to graduate!"
1 Like

If you’re having trouble understanding how not affects things, you could try just playing around with it. Sites like pythontutor.com and repl.it are good ‘playgrounds’ for trying things out.

Just saw your edits. Glad you’ve figured that part out! Good work! How about the last condition where neither requirement has been met?

P.S. Please review How do I format code in my posts? This will show you how to keep the forum’s markdown from messing up the format of your code, so it will look like it does now, since I edited your post. :wink:

  if not (gpa >= 2.0) and (credits >= 120):
    return "Your GPA is not high enough to graduate."

kept gives me an error is it a some sort of bug or what idk the page also before this one was the same , code is the exact like the solution

Can we see your full code?

sure

def grade_converter(gpa):
 grade = "3.0"
 if gpa >= 4.0:
   grade = "A"
 elif gpa >= 3.0:
   grade = "B"
 elif gpa >= 2.0:
   grade = "C"
 elif gpa >= 1.0:
   grade = "D"
 return grade
 Print(grade)

Hey all,

This piece of code worked but it doesn’t use the not operator. Is this still OK?

Is this just a unique scenario where you could game it using and, whereas other scenarios would mean you would definitely have to use the not operator?

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!”

Thanks for the help.

The code used for this exercise that’s shown in the next page shows the ff:

def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):
return “You meet the requirements to graduate!”
if (gpa >= 2.0) and not (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.”

In the last four lines, why could it not have been the following instead?

if not (credits >= 120):
return “You do not have enough credits to graduate.”
if not (gpa >= 2.0):
return “Your GPA is not high enough to graduate.”