Skip to content Skip to sidebar Skip to footer

For Loop Code Block

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 loop. Ar

Solution 1:

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.

Post a Comment for "For Loop Code Block"