For loop – Each item in a collection

for this_item in my_list:
  #Next item

#You can do a loop in 1 line if you wish:
for this_item in my_list: print(this_item)

For loop – Range

for index in range(6):    #Loop 6 times, from value 0 to 5
  #Next index
  print("Loop # " + str(index))

While loop

count = 0
while count <= 4:
  #Next loop
  count += 1

#A 1 line while loop (the ; character denotes a seperate line of code)
count = 0
while count <= 4: print(count); count += 1

Loop Control

Exit loop
Exit and start next iteration
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.


Your email address will not be published. Required fields are marked *