LESSON 04 – FOR LOOPS I

 


WORK

PROGRAM 1 – MESSAGING

 

Using a for loop, output the message I will not allow robots to take over the world eight times.

 

PROGRAM 2 – NUMBERS 10 to 30

 

Using a for loop, output all numbers from 10 to 30 to screen.

 

PROGRAM 3 – EVERY THIRD NUMBER BETWEEN 100 and 130

 

Using a for loop, output every third number starting with 100 and not exceeding 130.

 

PROGRAM 4 – LOVE

 

Create a list that contains at least 4 things that you love.

 

Example list:

 

myloves = ["eating steak",  "playing hockey", "reading a book", "playing video games"]

 

Use a for loop to go through the list and output the message “I love item” where item comes from the list.

 

Example output:

 

I love eating steak.

I love playing hockey.

I love reading a book.

I love playing video games.

 

PROGRAM 5 – CODE ANALYSIS

What will the following programs output to screen?

 

a)

for a in range(0,2):
    print(a)

b)

for x in range(0,20,5):
    print(x)

c)

for x in range(10,5,-1):

    print(x)

d)

mylist = ["a","b","c"]
for val in mylist:
    print(val)

e)

mylist = ["a","b","c"]

for i in range(0,3):

    print(mylist[i])

f)

for j in range(3,6):

    for k in range(0,2):

        print(j,k)

g)

for j in range(3,6):

    for k in range(0,2):

        print(j,k)

h)

for j in range(3,6):

    for k in range(0,2):

        print(j + k)