Code:[ZachxPKU/Python Crash Course]
Chapter 4 Working with lists
4.1 Looping through an entire list
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
4.1.1 A closer look at looping
4.1.2 Doing more work within a for loop
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
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")
4.1.3 Doing something after a 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("Thank you, everyone. That was a great magic show!")
4.2 Avoiding Indentation Errors
4.2.1 Forgetting to indent
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
4.2.2 Forgetting to indent additional lines
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")
4.2.3 Indenting unnecessarily
message = "Hello Python world!"
print(message)
4.2.4 Indenting unnecessarily after the 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("Thank you, everyone. That was a great magic show!")
4.2.5 Forgetting the colon
magicians = ['alice', 'david', 'carolina']
for magician in magicians
print(magician)
TRY IT YOURSELF
4.3 Making Numerical Lists
4.3.1 Using the range() function
for value in range(1, 5):
print(value)
for value in range(1, 6):
print(value)
for value in range(6):
print(value)
4.3.2 Using range() to make a list of numbers
numbers = list(range(1,6))
print(numbers)
even_numbers = list(range(2, 11, 2))
print(even_numbers)
squares = []
for value in range(1, 11):
square = value ** 2
squares.append(square)
print(squares)
squares = []
for value in range(1, 11):
squares.append(value ** 2)
print(squares)
4.3.3 Simple statistics with a list of numbers
digits = list(range(0, 10))
min(digits)
max(digits)
sum(digits)
4.3.4 List comprehensions
squares = [value ** 2 for value in range(1, 11)]
print(squares)
TRY IT YOURSELF
- counting_to_twenty.py
- one_million.py
- summing_a_million.py
- odd_numbers.py
- threes.py
- cube.py
- cube_comprehension.py
4.4 Working with part of a list
4.4.1 Slicing a list
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[3:])
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])
You can include a third value in the brackets indicating a slice. If a third value is included, this tells Python how many items to skip between items in the specified range
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[::2])
4.4.3 Looping through a slice
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())
4.4.3 Copy a list
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)
If we had simply set friend_foods equal to my_foods, we would not produce two separate lists.
TRY IT YOURSELF
- slices.py
- my_pizzas_your_pizzas.py
- more_loops.py
4.5 Tuples
4.5.1 Defining a tuple
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])
dimensions = (200, 50)
dimensions[0] = 250
my_t = (3,)
print(my_t)
my_t = (3)
print(my_t)
Tuples are technically defined by the presence of a comma; the parentheses make them look neater and more readable. If you want to define a tuple with one element, you need to include a trailing comma.
4.5.2 Looping Through all in a tuple
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
4.5.3 Writing over a tuple
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)
TRY IT YOURSELF
4.6 Styling Your Code
4.6.1 The style guide
4.6.2 Indentation
4.6.3 Line Length
4.6.4 Blank Lines
4.6.5 Other style guideline
TRY IT YOURSELF
PEP 8
4.7 Summary
|