+ 2
Can anyone give an example of while loop code in python?
G
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.
+ 2
I mean where can i use it (what is its perpose?)