Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brackets around if and for

Tags:

python

I am a python newbie and have a question. Why can a if allow a brackets and not for.

  1. if (1==2):

  2. for (i in range(1,10)):

  3. while (i<10):

First one and third one are valid syntax but not second one.

File "<stdin>", line 2
    for (i in range(1,10)):
                          ^
like image 655
aWebDeveloper Avatar asked Feb 14 '23 11:02

aWebDeveloper


1 Answers

Because for (i in range(1,10)) isn't syntactically correct.

Lets assume (i in range(1,10)) was parsed anyway, it would return a boolean. So then you're trying to say for True or for False, and booleans can't be iterated, and it's invalid syntax.


The reason why your other examples work is because they expect a boolean, which is what is returned from 1 == 2 and i < 10

like image 65
TerryA Avatar answered Feb 16 '23 04:02

TerryA