For Loops¶
-
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
-
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.,
- milk
- eggs
- chips
You will have to use an index-based loop, withrange(len(groceries))
and indexing groceries asgroceries[idx]
.
-
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 useprint(..., end='')
to keep the cursor on the same line.
-
Write a for loop to sum up all numbers from 0 to
n - 1
into a variablesum
. Assumen
is already defined.Your code should have this format:
- initialize sum to 0.
- for loop, using
range
. - add the value to sum
- for loop, using
-
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:
-
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.
-
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.
-
Write a for loop to create a list
tenths
containing numbers 0.0, 0.1, 0.2, 0.3, 0.4, …, 9.9Above 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.
-
Given a list
numbers
, write code to printFound it!
if the list contains the number 7. Do not use thein
operator. Use a for loop, with an if statement in it.Use item-based iteration. Put a simple if statement in the for loop.
-
Given a list
numbers
, write code to create two lists,bignums
andlittlenums
, by adding each number innumbers
tobignums
if it is greater than 50, and adding all the other numbers tolittlenums
.Create the resulting lists before the for loop.
Use item-based iteration with an if-else in it.
-
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 yourblock_list
.
-
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 torange(len(sequence))
so that the for loop prints out all indices useful for indexing into the sequencegroceries
.len(groceries)
returns the number of items in the sequence.
-
Replace
<1>
in the following code with a call torange(len(sequence))
so that the for loop uses indexes useful for indexing into the sequencegroceries
.Replace
<2>
with code to access the value in sequencegroceries
at indexidx
.Your resulting code should print each item in the
groceries
list, regardless of how long thegroceries
list is.len(groceries)
returns the number of items in the sequence.
-
Replace
<1>
in the code so that the for loop prints each item except the last in the listgroceries
.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.
-
Replace
<1>
in the code so that the for loop prints each item except the first and the last in the listgroceries
.To skip the first item, start with index
1
, not the default of0
.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.
-
Replace
<1>
in the code so that the for loop prints each item in the listgroceries
in reverse order. Userange()
with a step of -1.You need to use all three parameters torange
. Thestart
value needs to be the index of the last element ofgroceries
– so get the length ofgroceries
and subtract 1. Thestop
value needs to be less than 0, so that 0 is the last index thatrange
generates.