Python interpreter typically runs code sequentially first statement then the second .. from top to bottom, one alternative way of running code, is executing the same statements repeatedly, this term is known in programming as a loop not Lupe :)
Python loop types
Python supports two type of loop for and while loops
- while loop which is a simple loop used mainly for infinite loops aka loops that doesn't need to stop, like listening for incoming connection on a server socket
while condition: expression expression
- for loop is a little less a simple than the while one, for is used mainly to iterate over a items in an object, for example lists, tuples, and dictionaries.
for item in items: print(item)In order to loop over a dictionary you can use the following for to get both keys and values
for k, v in items: print(k, v)
- for loop
#!usr/bin/python3 host = 'coderme.com' for c in host: print(c)
- The following example is equivalent to the for loop one, but it uses while loop instead.
#!usr/bin/python3 host = 'coderme.com' c = 0 while c < len(host): print(host[c]) c += 1