For loop – Each item in a collection

for this_item in my_list:
  #Next item
  print(this_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
  print(count)
  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
  break
Exit and start next iteration
  continue
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.

Comments

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