IMPORTANT
this is an incredibly crucial lesson. and requirs A LOT of excersizes to really get the hang of. please do all the excersizes, and if you found them challenging, keep doing more excersizes and even doing the same excersizes again and again until you can reliably solve them easily.
Where to find more excercises?
For more exercizes on loops, see this link which contains 44 exercises with solutions:
for loops
iterating over a word list with a for
loop
Here’s a line from some old movie
quote = "Strange women lying in ponds, distributing swords, is no basis for a system of government!"
- use the .split() function to split it into a list of words.
- use the
for
loop to iterate over each word in the list- for each word, print the word and its
len()
- for each word, print the word and its
expected output:
Strange 7
women 5
lying 5
in 2
ponds, 6
distributing 12
swords, 7
is 2
no 2
basis 5
for 3
a 1
system 6
of 2
government! 11
quote = "Strange women lying in ponds, distributing swords, is no basis for a system of government!"
words = quote.split()
for word in words:
print(word, len(word))
comments
- Sometimes we want to write something that will explain the code we write, for this we can use comments.
# this is a comment x = 10 + 90 x = x * 2 # this is a comment at the end of a line print(x) # will print 200
- comments start with the
#
character and continue until the end of the line- comments are just for reading by humans, the python interepreter completely ignores them
write helpful comments
copy this code to the answer below, and write short comments between the lines that explain the code
hamburger = 59
pizza = 39
steak = 150
soup = 42
salad = 35
chips=18
dinner = [
hamburger,
hamburger,
salad,
chips
]
cost_of_dinner = 0
for food_item_cost in dinner:
cost_of_dinner = cost_of_dinner + food_item_cost
print(cost_of_dinner)
# put the cost of each item in the menu in a variable
hamburger = 59
pizza = 39
steak = 150
soup = 42
salad = 35
chips=18
# create a list of all the item costs for a particular dinner
dinner = [
hamburger,
hamburger,
salad,
chips,
steak,
salad,
soup
]
# calculate the cost of the dinner :
cost_of_dinner = 0 # before people ordered food, the cost of the dinner was 0
for food_item_cost in dinner: # now go through each item people ordered for dinner
cost_of_dinner = cost_of_dinner + food_item_cost # add the cost of this item to the total cost
# now we have the full cost of the dinner, let's print it
print(cost_of_dinner)
a nice dinner for two
- copy this code below
dinner = [ # Sashimi plate 60, # some maki 30, 35, 39, # agadashi 28, # miso 18, 18, # lots of sake 33, 39, 33, ]
- use a
for
loop to calculate the cost of the dinnerthis should take 3 lines
- this is a dinner for two. what is the average cost per person for this dinner?
hint: divide by 2
dinner = [
# Sashimi plate
60,
# some maki
30, 35, 39,
# agadashi
28,
# miso
18, 18,
# lots of sake
33, 39, 33,
]
cost_of_dinner = 0
for food_item_cost in dinner:
# we use += because it is just a shorter way of writing
# cost_of_dinner = cost_of_dinner + food_item_cost
cost_of_dinner += food_item_cost
number_of_people = 2
average_per_person = cost_of_dinner/ number_of_people
print(average_per_person)
iterating over letters in a string
- what does the following code do? try it
monty = "This is an ex parrot" for letter in monty: print(letter, end=" ")
- what does the
end=" "
part do? try to chage it to different values:" @ "
""
" --- "
"\n"
- can you write a loop that prints each letter twice?
expected output:
TThhiiss iiss aann eexx ppaarrrroott
monty = "This is an ex parrot"
for letter in monty:
print(letter, end='')
print(letter, end='')