《Python Crash Course》导读(Chapter 3)

发布于:2022-12-18 ⋅ 阅读:(472) ⋅ 点赞:(0)

Code:[ZachxPKU/AcceleratedCPlusPlus]

Chapter 3 Introducing Lists

3.1 What is a list?


bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

3.1.1 Accessing elements in a list


bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())

3.1.2 Index Positions Start at 0, Not 1


bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])

bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])

3.1.3 Using Individual Values from a List


bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."

print(message)

3.2 Changing, Adding, and Removing Elements

3.2.1 Modifying Elements in a List


motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)

motorcycles[0] = 'ducati'
print(motorcycles)

3.2.2 Adding Elements to a List

Appending elements to the end of a list


motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)

motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

Inserting elements into a list


motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)

3.2.3 Removing elements from a list

Removing an item using the del statement


motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)

Removing an item using the pop() method


motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print(f'The last motorcycle I owned was a {last_owned.title()}.')

Popping items from any position in a list


motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print(f'The first motorcycle I owned was a {first_owned.title()}.')

Removing an item by value


motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)

motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f'\nA {too_expensive.title()} is too expensive for me.')

The remove() method deletes only the first occurrence of the value you specify. If there’s a possibility the value appears more than once in the list, you’ll need to use a loop to make sure all occurrences of the value are removed.

TRY IT YOURSELF

  • guest_list.py
  • changing_guest_list.py
  • more_guests.py
  • shrinking_guest_list.py

3.3 Organizing a list

3.3.1 Sorting a list permanently with the sort() method


cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)

cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)

3.3.2 Sorting a list temporarily with the sorted() function


cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)

print("\nHere is the sorted list:")
print(sorted(cars))

print("Here is the original list again:")
print(cars)

3.3.3 Printing a list in reverse order


cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)

3.3.4 Finding the length of a list


cars = ['bmw', 'audi', 'toyota', 'subaru']
len(cars)

TRY IT YOURSELF

  • seeing_the_world.py
  • dinner_guests.py
  • every_function.py

3.4 Avoiding Index Errors When Working with Lists


motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])

motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[-1])

motorcycles = []
print(motorcycles[-1])

TRY IT YOURSELF

  • intentional_error.pu

3.5 Summary