for - while

for

Use the Python for command and iterate over a range. For example, to repeat a command 10 times:

for i in range(1, 11):
   print(i)

1
2
3
4
5
6
7
8
9
10

===================================

while


The while statement repeats its nested commands until its condition becomes false. The following example will stay in the loop until the user enters X for exit:

answer = ""
while answer !="X":
   answer = raw_input("Enter command:")

Enter command:A
Enter command:B
Enter command:X

No comments:

Post a Comment