Python control flow

hey guys! I’m working on python control flow now and I just wanted to now why sometimes you need to use parentheses when having multiple statements for if and then and sometimes you don’t?

def applicant_selector(gpa, ps_score, ec_count):
if gpa >= 3.0 and ps_score >= 90 and ec_count >= 3:

VS

def graduation_reqs(gpa, credits):
if (gpa >= 2.0) and (credits >= 120):

Hello @raqueldevore :grinning:

This usually has to do with the order of operations, or the order in which the comparison operators will be evaluated, or that arithmetic operators are run.

You can see the order here:
codecademy.com/courses/learn-python/lessons/conditionals--control-flow/exercises/this-and-that-or-this-but-not-that

codecademy.com/courses/learn-python/lessons/python-syntax/exercises/arithmetic


The reason for using () is usually if you need to override this normal order.
In this example:

n = 7 + 8 * 4 - 9

n will equal 30.

However here:

n = (7 + 8) * (4 - 9)

n will equal -75

It can also be used to find the negative of a negative number:

n = -(-5)

Or to simply improve readability, which I believe is the case in your example.

1 Like

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