Brackets around if and for

I am new to python and wondering. Why is this possible if brackets allow and not for.

  • if (1==2):

  • for (i in range(1,10)):

  • while (i<10):

The first and third are valid syntax, but not the second.

File "<stdin>", line 2
    for (i in range(1,10)):
                          ^
+4
source share
2 answers

Because it is for (i in range(1,10))not syntactically correct.

Suppose that he (i in range(1,10))understood anyway, he would return a logical value. So you are trying to say for Trueor for False, and boolean cannot be repeated, and this is not valid syntax.


The reason your other examples work is because they expect a boolean value that is returned from 1 == 2andi < 10

+7
source
  • if (True of False). : (1) + (1)

  • for VAR in LIST - , , :

    • ,

    , Python ?

- (?), for: http://docs.python.org/3/reference/compound_stmts.html#the-for-statement

for_stmt ::=  "for" target_list "in" expression_list ":" suite
              ["else" ":" suite]
+1

Source: https://habr.com/ru/post/1532612/


All Articles