The following exercises cover scenarios on For loop statement in Python.
Exercise 1
Iterate over the items in the list mylist, using Python For loop.
mylist = ['apple', 'banana', 'cherry'] item mylist: print(item)
Exercise 2
Iterate over the items in the list x, using For loop, and if the item equals 'banana', break the loop using Python Break statement.
mylist = ['apple', 'banana', 'cherry'] for item in mylist: if item == 'banana': print(item)
Exercise 3
Iterate over the items in the list x, using For loop, and if the item equals 'cherry', continue with the next items using Python Continue statement.
mylist = ['apple', 'banana', 'cherry', 'mango'] for item in mylist: if item == 'cherry': print(item)
Exercise 4
Print 'Hello World' for 5 times using For loop and range() built-in function.
for i in : print('Hello World')