Python Crash Course: A Hands-On, Project-Based Introduction to Programming
Rate it:
Kindle Notes & Highlights
5%
Flag icon
Every programming language evolves as new ideas and technologies emerge, and the developers of Python have continually
8%
Flag icon
A method is an action that Python can perform on a piece of data.
9%
Flag icon
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.
11%
Flag icon
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.
11%
Flag icon
The pop() method removes the last item in a list, but it lets you work with that item after removing it.
11%
Flag icon
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:
11%
Flag icon
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)
12%
Flag icon
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.
12%
Flag icon
➊ 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)
12%
Flag icon
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.
12%
Flag icon
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.
12%
Flag icon
> cars = ['bmw', 'audi', 'toyota', 'subaru'] >>> len(cars) 4