+ 2
please clear my doubt
In C program: for(.....) statements In above for loop only first statement will be executed,because these statements aren't between curly braces. In Python: How does it work?
2 Answers
+ 5
In Python, The loops work with indentation. After the loop, if you are not using an indent, it will not be considered a part of the loop. For Example:-
for i in range(0,3):
print(i)
print("End")
This code will output something like this.
0
1
2
End
The loop works for print(i) because there was indent before the statement. And print("End") was not in an indent, therefore, it was out of the loop and was printed after the loop was over. If I had also used the indent before the second statement something like this:-
for i in range(0,3):
print(i)
print("End")
This would have print first The values like this:-
0
End
1
End
2
End
So it depends on the indent in python whether a loop statement will execute or not,