More on this book
Community
Kindle Notes & Highlights
by
Eric Matthes
Read between
January 27, 2018 - December 9, 2021
Every programming language evolves as new ideas and technologies emerge, and the developers of Python have continually
A method is an action that Python can perform on a piece of data.
Floats Python calls any number with a decimal point a float. This term is used in most programming languages, and it refers to the fact that a decimal point can appear at any position in a number.
Removing an Item Using the pop() Method Sometimes you’ll want to use the value of an item after you remove it from a list.
The pop() method removes the last item in a list, but it lets you work with that item after removing it.
The sort() method, shown at ➊, changes the order of the list permanently. The cars are now in alphabetical order, and we can never revert to the original order:
You can also sort this list in reverse alphabetical order by passing the argument reverse=True to the sort() method. The following example sorts the list of cars in reverse alphabetical order: cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort(reverse=True) print(cars)
To maintain the original order of a list but present it in a sorted order, you can use the sorted() function. The sorted() function lets you display your list in a particular order but doesn’t affect the actual order of the list.
➊ print("Here is the original list:") print(cars) ➋ print("\nHere is the sorted list:") print(sorted(cars)) ➌ print("\nHere is the original list again:") print(cars)
Notice that the list still exists in its original order at ➍ after the sorted() function has been used. The sorted() function can also accept a reverse=True argument if you want to display a list in reverse alphabetical order.
The reverse() method changes the order of a list permanently, but you can revert to the original order anytime by applying reverse() to the same list a second time.
> cars = ['bmw', 'audi', 'toyota', 'subaru'] >>> len(cars) 4