python for loop start and end code example

Example 1: python type for loop

key: str
for key in cache:
  	print(key)

Example 2: python loop back to start

def main(): #defines the area in indents that will be triggered with main()#
  print('hi')
  yn = input('Wanna loop back to the start? ')
  if yn = 'yes':
    main() #loops back to where we defined main#
    
main() #This starts the main loop, without this, main would just be defined but not run#

Example 3: python loop certain number of times

# To loop n times, use loop over range(n)
for i in range(n):
  # Do something

Example 4: how to end a loop in python

#the loop
for x in (1, 10, 1):
  #type break to end loop before loop stops
  break

Example 5: how to use iteration in python

n = 5
while n > 0:
    print n
    n = n-1
print 'Blastoff!'

Tags:

Misc Example