Chapter 4: Working with lists
- looping – allows you to take the same action, or set of actions, with every item in a list
Looping through an entire list
- you’ll often want to perform the same task with each item
- for example, perform the same statistical operation for every element in a list of numbers or display each headline from list of articles on a website
- for loop – use when you want to do the same action with every item in a list
magicians.py
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(magician)
alice
david
Carolina
- defining a for loop tells Python to pull a name from the list magicians, and associate it with the variable magician
- next, we tell Python to print the name assigned to magician
- Python then repeats the last two lines, once for each name in the list
A closer look at looping
- one of the most common ways a computer automates repetitive tasks
- can use any name you want for temporary variable associated with each value, but it’s helpful to make it meaningful so you can follow the action being done on each item
Doing more work within a for loop
- printing a message for each magician
magicians.py
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(f”{magician.title()}, that was a great trick!”}
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
- inside the loop – includes every indented line following the for loop line
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(f”{magician.title()}, that was a great trick!”}
print(f”I can’t wait to see your next trick, {magician.title()}.\n”)
Alice, that was a great trick!
I can’t wait to see your next trick, Alice.
David, that was a great trick!
I can’t wait to see your next trick, David.
Carolina, that was a great trick!
I can’t wait to see your next trick, Carolina.
Doing something after a for loop
- do not indent to display a message after the for loop
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(f”{magician.title()}, that was a great trick!”}
print(f”I can’t wait to see your next trick, {magician.title()}.\n”)
print(f”Thank you, everyone. That was a great magic show!”)
Alice, that was a great trick!
I can’t wait to see your next trick, Alice.
David, that was a great trick!
I can’t wait to see your next trick, David.
Carolina, that was a great trick!
I can’t wait to see your next trick, Carolina.
Thank you, everyone. That was a great magic show!
- for example, play now button after characters have been drawn to the screen in a game
Avoiding indentation errors
- Python uses indentation to determine how a line, or group of lines, is related to the rest of the program
- whitespace forces you to write neatly formatted code with a clear visual structure
- indentation errors – lines are indented / not indented when they should / shouldn’t be
Forgetting to indent
- indent line or lines immediately after for statement
Forgetting to indent additional lines
- execution will only run for the last element of a list if the additional line is not indented
- logical error – syntax is valid Python code, but does not produce desired results because a problem occurs in its logic
magicians = [‘alice’, ‘david’, ‘carolina’]
for magician in magicians:
print(f”{magician.title()}, that was a great trick!”}
print(f”I can’t wait to see your next trick, {magician.title()}.\n”)
Alice, that was a great trick!
David, that was a great trick!
Carolina, that was a great trick!
I can’t wait to see your next trick, Carolina.
Indenting unnecessarily
- at this point, the only lines you should indent are the actions you want to repeat for each item in a for loop
- Python will inform you about unexpected indents
Indenting unnecessarily after the loop
- final message will loop once for each item instead of once at the end
Forgetting the colon
- the colon at the end of a for statement tells Python to interpret the next line as the start of a loop
- you will get a syntax error if it’s missing
Making numerical lists
- many reasons exist for stoking sets of numbers: player’s high scores, temperatures, distances, population sizes, or latitude and longitude values
Using the range() function
- range() function – generates a series of numbers
first_numbers.py
for value in range(1, 5)
print(value)
1
2
3
4
for value in range(6)
print(value)
- will print numbers 0 to 5
End of study session.