0

Clear my head,What's the difference between "for loop" and "while loop" in python?

3rd Aug 2019, 8:01 AM
Harish Kumar. M
Harish Kumar. M - avatar
2 Answers
+ 7
The for loop executes a piece of code for every item in a container, for example a list, a dictionary, a tuple, a predefined range of numbers... In pseudocode: for fruit in fruitbasket, for word in vocabularyBook, for letter in word, for number in telephoneNumbers... So all items of an iterable are iterated over. A while loop runs as long as a condition is true. for example: while x>5: # And here, you have some code # You change x in the process # and as soon as x is not >5 # the loop stops.
3rd Aug 2019, 8:09 AM
HonFu
HonFu - avatar
0
Right. A for loop iterates through an iterable (such as a list or an array) And a while loop loops as long as the condition is true
4th Aug 2019, 5:59 PM
Lootus Maximus
Lootus Maximus - avatar