LESSON 04 –
FOR LOOPS I

 

 

LESSON NOTE/GUIDE

 

PROGRAM 1 – PROCESS LIST CONTENTS - OUTPUT

·       Create a list that contains words.  Use a for loop to output each element one word at a time.


THE CODE

 

data = ["apple", "orange", "pink", "carrot"]

 

for x in data:

    print(x)


EXPLANATION

 

The first line creates a list with four strings in it. 

 

The second line creates a loop.  All lines indented under a for statement are inside the loop.  In this program, the loop contains only the print statement. 

 

This loop will be executed four times with x taking on each value in the list. 

 

The first time through, the value of x will be “apple” and that is what will be outputted to screen.

The second time through the loop, x will get the value “orange” and that will be outputted to screen. 

 

The third and fourth times through, the values “pink” and “carrot” will be outputted.

 

Note that in the loop above, x is called the control variable.

 

 

 

PROGRAM 2 – A SIMPLE LOOP

  • Use a for loop and the range(#,#) function to repeat a statement five times.
  • Explore by changing the values 1 and 6 to other numbers to see what happens.

 


THE CODE

 

for x in range(1,6):  

    print("hello")

print("done")

 

EXPLANATION

 

The range(1,6) function in the first line actually creates the sequence of numbers [1,2,3,4,5].  It excludes the last number so 6 is not in the sequence.

 

After the sequence is created, the first line creates a for loop.  The variable x is called the control variable as it controls the loop.  In each pass or iteration in the loop, the variable x will take on one of the values in the sequence.

 

Note that the bottom print statement is not indented so it is not inside the loop.  It will therefore only be executed after the loop is complete.

 

We will therefore print “hello” on screen five times.  Then the loop will end.

 

After the loop is over, the final print statement is executed and “done” is outputted to screen.

 

 

PROGRAM 3 – OUTPUTTING THE CONTROL VARIABLE

  • Output the value of the control variable to screen.
     


THE CODE

 

for x in range(1,6):  

    print("hi", x)

print("done")

 

EXPLANATION

 

This code will do the same as the previous program but will also demonstrate how the control variable x changes from one iteration to another.  Try it out.

 

 

PROGRAM 4 – MORE ON THE range(#,#) FUNCTION

  • Explore with the range(#,#) function.

 


THE CODE

 

# range function details

# arg1 - the lowest number in the list

# arg2 - the value at which the list stops before

# arg3 - the amount by which the list goes up (optional)

 

for x in range(2,9,2):

    print(x)

print("done")

 

EXPLANATION

 

Try changing the three arguments inside the range(#,#,#) function to see what it changes.

 

 

PROGRAM 5 – OUTPUT ALL NUMBERS

  • Use the range(#,#) function to output all numbers from 1 to 100 on the screen.

 


THE CODE

 

# range function details

# arg1 - the lowest number in the list

# arg2 - the value at which the list stops before

# arg3 - the amount by which the list goes up (optional)

 

for x in range(1,101):

    print(x)

 

EXPLANATION

 

The program will output all numbers from 1 to 100 on the screen.

 

 

PROGRAM 6 – OUTPUT EVEN NUMBERS

  • Write the program that will output all even numbers between 1 and 100.

 


THE CODE

 

for x in range(2,101,2):

    print(x)

 

EXPLANATION

 

The program will output all even numbers from 2 to 100.

 

 

PROGRAM 7 – OUTPUT BACKWARDS

  • Write a program that will output all numbers from 1 to 100 backwards, so from 100 down to 1.  Remember that lists can be reversed.

 


THE CODE

 

for x in range(100,0,-1):

    print(x)

 

EXPLANATION

 

The program will output numbers 100 down to 1.

 

 

THE CODE (ALTERNATIVE APPROACH)

 

numbers = list(range(1,101))

numbers.reverse()

 

for x in numbers:

    print(x)

 

EXPLANATION

 

The first line of code uses the range(#,#) function to create a sequence of numbers.  The list function then converts that sequence into a list.  That list is stored inside numbers.

 

The second line reverses the order of elements in the numbers list.

 

The for loop then outputs all elements of the list in order.

 

 

PROGRAM 8 – LOOP OVER A STRING

  • Write a program that creates a string and loops over each character of that string.

 


THE CODE

 

word = "test"

 

for c in word:

    print(c)

 

 

EXPLANATION

 

In this case, the control variable c (which is short for character) gets one letter at a time from the word “test”.  In the first iteration, c will be “t”.  In the second, it will be “e”.  In the third, it will be “s” and in the final iteration it will be “t”.

 

 

PROGRAM 9 – INTRODUCING NESTED LOOPS

  • Part 1 - Write a program that makes us of a for loop to output the word “three” out three times to screen.
  • Part 2 – Place your program from Part 1 into another for loop.  Make that loop repeat twice. 

 


THE CODE (PART 1)

 

for b in range(1,4):

    print("three")

 

 

EXPLANATION

 

The loop will repeat three times and output “three” each time.

 


THE CODE (PART 1)

 

for a in range(1,3):

    for b in range(1,4):

        print("three")

 

 

EXPLANATION

 

When there is one loop inside another, it is called nested loops.

 

The outer loop will loop twice.  In the first iteration of the outer loop, the inner loop will output “three” 3 times.

 

In the second iteration of the outer loop, the inner loop will again output “three” 3 times.

 

In total, the word “three” will be outputted 6 times.

 

 

PROGRAM 10 –  NESTED LOOPS CONTROL VARIABLES

  • Create a program that will has two nested for loops and output their control variable values to the screen.
  • Analyze the order of statements that get executed.

 


THE CODE

 

for a in range(1,4):     # a will get values 1, 2 & 3
   
for b in range(1,3): # b will get values 1 & 2
       
print(a, b)

 

 

EXPLANATION

 

The above code will create the following output:

 

1 1

1 2

2 1

2 2

3 1

3 2

 

The first line creates a for loop with the control variable a set to take on values 1, 2 and 3.  Initially, it has the value 1.

 

The second line of code creates a for loop with control variable b set to take on values 1 and 2.  Initially, it has value 1.

 

The third line of code outputs the values of a and b which are both one.  So it outputs 1 1.

 

We are at the end of the inner loop so we increment b by 1 to the value 2.  We repeat the print line and output 1 2.

 

We are at the end of the inner loop and the control variable be has taken on all of its values so the inner loop is done.  Now we are at the end of the outer loop.  The value of variable a is increased by 1 to the value 2.  We now restart the inner loop with a equal to 2. 

 

Initially, b will be one so the program will output 2 1.  Then, b will be 2 and the program will output 2 2.

 

We are done with the inner loop and therefore a is increased again to 3.  The inner is restarted again. 

 

The print statement will get executed twice, once outputting 3 1 and then 3 2.

 

 

PROGRAM 11 – STAR PATTERN (TIME PERMITING)

  • Create a program that will output a square pattern of stars based on a user inputted size.

 


THE CODE

 

size = int(input("What size?"))

for r in range(0, size):

    print ("*" * size)

 

 

EXPLANATION

 

There user will be asked This will output a patter such as

 

***

***

***