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 ▸

Describe both For-loops and While-loops and explain how you can simulate the effect of a for loop with a while loop with an example.


How do I use for loops in Python?


Create a program that takes in two numbers and returns the highest of the two


Write a function that checks whether a number is prime or not.