What is the difference between a for and a while loop?

A for loop iterates over set commands within the loop (need to be indented), executing this block of code in order until prompted to stop (as specified). A while loop is used when a condition needs to be checked each iteration before the loop can run through the block of code.

TD

Related Python Mentoring answers

All answers ▸

How might we implement the bubble sort algorithm in Python


Explain why creating a list of several instances of one element using something like my_list = [a] * 5 can result in strange behaviour


What is the difference between using range() and a list of values?


Explain how python programs are structured and give an example of how methods are initiated