In Python, you can create loops using the for
and while
statements. The for
loop is used when you want to iterate over a sequence of values, while the while
loop is used when you want to loop while a certain condition is true.
Here is an example of a for
loop that iterates over a list of names and prints each one:
main.py71 chars4 lines
Output:
main.py18 chars4 lines
Here is an example of a while
loop that prints all the numbers from 0 to 4:
main.py43 chars5 lines
Output:
main.py10 chars6 lines
In both cases, the loop continues until all elements in the list have been processed or until the condition is no longer true.
gistlibby LogSnag