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)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
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)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
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)
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.
Exercise 4
Print 'Hello World'
for 5 times using For loop and range() built-in function.
for i in : print('Hello World')
Submit Answer
↻ Reset
Show Answer
Fill the fields with missing code.