for loop code block

后端 未结 1 1348
夕颜
夕颜 2021-01-13 17:39

I was reading the tutorial about python here and was wondering that if the for loop does not have a block like this {}, how would we know which block of code is in the for l

相关标签:
1条回答
  • 2021-01-13 17:58

    Python runs everything on indentation. The indentation level is how it knows what goes with what.

    For example, this works:

    for i in range(10):
        print i
    

    But this blows up with an IndentationError:

    for i in range(10):
    print i
    

    From docs:

    Leading whitespace (spaces and tabs) at the beginning of a logical line is used to compute the indentation level of the line, which in turn is used to determine the grouping of statements.

    0 讨论(0)
提交回复
热议问题