Chapters
Python114 chapters

Exercises

For Loops

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

Print the numbers 0, 3, 6 and 9 using a single range.

Python
# loop with a step
Exercise 2

Print each name and year from the pairs, unpacking in the for line.

Python
pairs = [("Ada", 1815), ("Grace", 1906)]
# print 'Ada: 1815' and so on
Exercise 3

Print 'not in the list' when Alan is not found, without using a flag variable.

Python
names = ["Ada", "Grace"]
for name in names:
    if name == "Alan":
        print("found")
        break
# report not found here