Can you please help!

Hi!

How come the ( ) is necessary on line two after the “if”?
Example: if( num>= lower…
Ifs don’t usually require parenthesis, do they?

Thank you very much,
Josh

The parens are not needed since comparison operators take precedence to logical operators. There is a definite syntax error, though, and the arrow points right at it.

if a <= x and x <= b:

This might come up in the lessons, but if not you will likely read about somewhere… Conditional chaining:

if a <= x <= b:

is valid in Python.

1 Like

Hi MTF,

Thank you very much!

Are you saying that the “and” isn’t necessary?
I thought “and” and “or” were used in python as for example:
if a <= x and x <=b:
return True

Can you please give me an example?

Thank you very much!
Josh

It is less necessary in this instance where we are testing the same value in two comparisons. Above, x will be one of, a...b.

from random import randint

x = randint(1, 10)
print (3 < x < 7 or x)

If x is more than 3 and less than 7, it will print True, else it will print x.

1 Like

Hello [board4185331766], I can give you an example
Lets make some rules so we can both follow up the example.
lets say x is an integer (example of integer numbers: 0, 1, 2, 3, 4, 5)
lets say we want to check if x is >= 3 and x is <= 5
Now lets stop here. We actually want to check if x is 3 or 4 or 5
Or x is between 3 and 5.

Now lets write code:
one way is to say
if (x>=3 and x<=5)
another way is:
if (x==3 or x==4 or x==5)
another way is:
if 3<=x<=5

all 3 are legit
Lets stop here again and think about if we have to pick one and consider it as most useful, that will be the last one.

To answer you initial question and give you the best answer I can…
Try it (that’s my answer)… if u fell like u need or u want to remove parenthesis just do it, and see what will happen, if nothing happens then parenthesis might be redundant and unnecessary
Also keep in mind that programs like Pycharm (example) with PEP8 preinstalled and suggestions on will mark the line and suggest you, to remove redundant parenthesis if they are indeed with no reason there…!!!

2 Likes

Hey Kommas,

Thank you very much. Great explanation. I totally get it now.

Thank you again!

Respectfully,
Josh