Python has two types of loops: for
loop and while
loop.
A for
loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
Here's an example:
main.py75 chars4 lines
Output:
main.py20 chars4 lines
A while
loop is used to repeat a set of statements as long as a given condition is True.
Here's an example:
main.py39 chars5 lines
Output:
main.py10 chars6 lines
In the example above, the loop will continue to run as long as i
is less than 5. Inside the loop, we print the current value of i
, and then increment i
by 1.
gistlibby LogSnag