《Python Crash Course》导读(Chapter 4)

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

  • pizzas.py
  • animals.py

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

  • buffet.py

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值