Exercises on Python While loop

The following exercises cover scenarios on While loop statement in Python.

Exercise 1

Print ‘Hello World’ 5 times using While loop.

i = 0
 i < 5
    print('Hello World')

Exercise 2

Iterate over the items in the list mylist.

mylist = ['apple', 'banana', 'cherry']

i = 0
 i < len():
    print(mylist[i])
    i += 1

Exercise 3

Iterate from i=1 to i=10, but break the loop if i equals 4.

i = 1
while i <= 10:
    if i == 4:
        
    print(i)
    i += 1

Exercise 4

Iterate fromi=1 to i=10, but if i equals 4, skip the next statements in the loop and continue with the next iteration.

i = 1
while i <= 10:
    if i == 4:
        
    print(i)
    i += 1

Exercise 5

Print 'Hello World' when the condition in While loop becomes false, using while-else.

i = 1
while i <= 10:
    print(i)
    i += 1

    print('Hello World')
Code copied to clipboard successfully 👍