For Loops

  1. Assume a variable groceries has been defined to be a list of grocery items.

    Write code to print out each item on its own line.

    The for loop pattern is:

    for item in list:

    loop-body
  1. Assume a variable groceries has been defined to be a list of grocery items.

    Write code to print out all items in the list on separate lines, prefixed by the number of the item in the list. E.g.,

    1. milk
    2. eggs
    3. chips
    You will have to use an index-based loop, with range(len(groceries)) and indexing groceries as groceries[idx].
  1. Assume a variable groceries has been defined to be a list of grocery items.

    Write code to print out all items in the list on a single line, separated by commas. It is OK to have a comma after the last item.

    Remember that you can use print(..., end='') to keep the cursor on the same line.
  1. Write a for loop to sum up all numbers from 0 to n - 1 into a variable sum. Assume n is already defined.

    Your code should have this format:

    • initialize sum to 0.
    • for loop, using range.
      • add the value to sum
  1. Write a for loop to print out each letter in a str name on one line with a space between.

    A string is iterable, so you can use it right in the for loop:

    e.g.,

    for char in str:

  1. Write a for loop to iterate over a string name and print every other letter – i.e., the letters with indices which are even. That is, only the 0th, 2nd, 4th, etc., letters.

    Use an index-based loop, since you will need to know if you are looking at an even or odd index.

    Use an if statement in the for loop to determine if the index is even.

  1. Write code to create a list rands that contains 20 random numbers from 0 to 100 inclusive.

    Above the for loop, create the variable rands to be an empty list.

    Call randint(0, 100) to get random integers from 0 to 100, inclusive.

    Use append() to add a random number to the list.

    Use range(20) to iterate 20 times.

  1. Write a for loop to create a list tenths containing numbers 0.0, 0.1, 0.2, 0.3, 0.4, …, 9.9

    Above the for loop, create the variable tenths to be an empty list.

    There will be 100 numbers in the list, so use range(100) to iterate 100 times.

  1. Given a list numbers, write code to print Found it! if the list contains the number 7. Do not use the in operator. Use a for loop, with an if statement in it.
    Use item-based iteration. Put a simple if statement in the for loop.
  1. Given a list numbers, write code to create two lists, bignums and littlenums, by adding each number in numbers to bignums if it is greater than 50, and adding all the other numbers to littlenums.

    Create the resulting lists before the for loop.

    Use item-based iteration with an if-else in it.

  1. This code was recently seen in a project:

    block_list = [Block(20,20), Block(20,60), Block(20,100), Block(20,140), Block(20,180), Block(60,20), Block(60,60), Block(60,100), Block(60,140), Block(60,180), Block(100,20), Block(100,60), Block(100,100), Block(100,140), Block(100,180), Block(140,20), Block(140,60), Block(140,100), Block(140,140), Block(140,180), Block(180,20), Block(180,60), Block(180,100), Block(180,140), Block(180,180), Block(220,20), Block(220,60), Block(220,100), Block(220,140), Block(220,180),Block(260,20), Block(260,60), Block(260,100), Block(260,140), Block(260,180), Block(300,20), Block(300,60), Block(300,100), Block(300,140), Block(300,180), Block(340,20), Block(340,60), Block(340,100), Block(340,140), Block(340,180), Block(380,20), Block(380,60), Block(380,100), Block(380,140), Block(380,180), Block(420,20), Block(420,60), Block(420,100), Block(420,140), Block(420,180), Block(460,20), Block(460,60), Block(460,100), Block(460,140), Block(460,180), Block(500,20), Block(500,60), Block(500,100), Block(500,140), Block(500,180), Block(540,20), Block(540,60), Block(540,100), Block(540,140), Block(540,180), Block(580,20), Block(580,60), Block(580,100), Block(580,140), Block(580,180), Block(620,20), Block(620,60), Block(620,100), Block(620,140), Block(620,180), Block(660,20), Block(660,60), Block(660,100), Block(660,140), Block(660,180), Block(700,20), Block(700,60), Block(700,100), Block(700,140), Block(700,180)]

    Rewrite this code to use a doubly-nest for loop.

    Create the resulting list before the for loops.

    Create a for loop to iterate from 20 to 700, by 40s.

    Inside the first for loop, write a for loop to iterate from 20 to 180 by 40s.

    Inside that for loop, call the Block() call, and append the result to your block_list.

  1. Integers are used as indices into sequences, like lists and strings. So, if you want to access each element in a sequence at a certain index, you need to use a for loop to iterate over each index appropriate for accessing the sequence at that index.

    To get the indices appropriate for indexing a sequence, use range(len(sequence)).

    Replace <1> in the following code with a call to range(len(sequence)) so that the for loop prints out all indices useful for indexing into the sequence groceries.

    len(groceries) returns the number of items in the sequence.
  1. Replace <1> in the following code with a call to range(len(sequence)) so that the for loop uses indexes useful for indexing into the sequence groceries.

    Replace <2> with code to access the value in sequence groceries at index idx.

    Your resulting code should print each item in the groceries list, regardless of how long the groceries list is.

    len(groceries) returns the number of items in the sequence.
  1. Replace <1> in the code so that the for loop prints each item except the last in the list groceries.
    len(groceries) returns the number of items in the sequence, but we want to access all items except the last one, so subtract one from that value.
  1. Replace <1> in the code so that the for loop prints each item except the first and the last in the list groceries.

    To skip the first item, start with index 1, not the default of 0.

    len(groceries) returns the number of items in the sequence, but we want to access all items except the last one, so subtract one from that value.

  1. Replace <1> in the code so that the for loop prints each item in the list groceries in reverse order. Use range() with a step of -1.
    You need to use all three parameters to range. The start value needs to be the index of the last element of groceries – so get the length of groceries and subtract 1. The stop value needs to be less than 0, so that 0 is the last index that range generates.