There are two ways to implement loops in python:
For loop is used to iterate over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). Here's the general syntax for a for loop:
main.py65 chars3 lines
Example:
main.py116 chars5 lines
Output:
main.py10 chars6 lines
While loop is used when we don't know the number of iterations we need to perform. It keeps iterating until a certain condition is met. Here's the general syntax for a while loop:
main.py57 chars3 lines
Example:
main.py97 chars6 lines
Output:
main.py10 chars6 lines
gistlibby LogSnag