+ 2

Can anyone give an example of while loop code in python?

G

27th Oct 2020, 12:53 PM
zerea biruk
3 Answers
+ 1
Loops are used to execute parts of a code repeatedly. Lets say you have a list with numbers, and you want to print only the even numbers from that list, you can use a loop. This can be a for loop or a while loop. ( in this case a for loop would be the better solution, so the code does only illustrate how it works. You need to have a counter variable, that is also used for indexing the list. lst = [1,2,3,6,0,4,1,8] x = 0 while x < len(lst): if lst[x] % 2 == 0: print(lst[x]) x += 1 As QTWizard already mentioned, you should work through the python tutorial to get familiar with this basic things.
27th Oct 2020, 3:44 PM
Lothar
Lothar - avatar
+ 2
I mean where can i use it (what is its perpose?)
27th Oct 2020, 1:03 PM
zerea biruk