For loops

<< Previous: Practical challenges 3Next: Practical Challenges 4 >>

Iteration

Iteration is the name given to making code run more than one time. There are two key types of iteration:

  1. For loops
  2. While loops

For loops

For loops are what we call count-controlled iteration, meaning we know how many times they will execute when they start. There are 2 key types of FOR loop:

  • for X in Y
  • for i in range( )
  • for X in Y

    The for X in Y loop can be used with any iterable. An iterable is a data type we can move through one item after the next. We can go character by character through a string or item by item through a list or dictionary.

    The X in a for X in Y loop is a temporary variable that stores the first value and then the second and then the third etc until it reaches the end.

    word = "computer"
    for letter in word:
        print(letter)

    In the code above the X is letter. The first go through the loop letter holds the value 'c', next it is an 'o', then a 'm' etc. The loop ends when there are no more characters to iterate through. The Y in the code above is the variable word which stores a string we can iterate through.

    for X in Y syntax rules:

    Syntax checker

    Fix the code below so it counts and outputs the number of instances of the letter s are in a persons name they entered.





    name = input("Enter your name: "):
    num_s = 0
    for letter in name:
        if letter == "s":
            num_s = num_s + 1
    print(f"There are {num_s} instances of the letter s in your name.")

    for i in range()

    In a for i in range loop we create our own iterator. This is a variable that holds a number that goes up by 1 each time through the loop. We need to pass at least one value to a for i in range loop to tell it how many times to run but have the option to pass it more than one value.

    for i in range with one parameter

    When we specify one value the iterator starts with a value of 0 and goes until the iterator is one less than the specified number.

    for i in range(5):
        print(f"This time through the loop the iterator is {i}")

    The code above will run the loop 5 times in total, but the value of the iterator never reaches 5. The last line output would be 'This time through the loop the iterator is 4'.

    for i in range with two parameters

    When we specify two values the iterator starts at the first value and goes until the iterator is one less than the specified number.

    for i in range(5,8):
        print(f"This time through the loop the iterator is {i}")

    The code above will run the loop 3 times in total. The outputs would be:

    for i in range with three parameters

    When we specify three values the iterator starts at the first value and goes until the iterator is one less than the specified number, but it increments by the 3rd value each time.

    for i in range(0,100,5):
        print(f"This time through the loop the iterator is {i}")

    The code above will run the loop 20 times in total. The outputs would be:

    for i in range syntax rules:

    Syntax checker

    Fix the code below so it counts and outputs the reuested numbers.





    for i in range(1,21):
        print(i)
    for i in range(5,11):
        print(i)
    for i in range(100,0,-1):
        print(i)

    Knowledge check


    Questions:
    Correct:

    Question text


    << Previous: Practical challenges 3Next: Practical Challenges 4 >>

    © All materials created by and copyright S.Goff