Programming 1: Python Chapter 5
Why Use Tuples at All
1. Tuples are faster than lists. 2. Tuples' immutability makes them perfect for creating constants because they can't change. 3. Sometimes tuples are required (In some cases, Python requires immutable values.) *Because lists are so flexible, you're probably better off using them rather than tuples the majority of the time.
.append()
Adds a new element to the end of list. The list becomes one element longer. score = int(input("What score did you get? ")) scores.append(score)
.remove()
Checks to see whether the value is in the list. If it is, the list method .remove() is invoked and the element is deleted from the list. If the value is in the list more than once, only the first occurrence is removed. The .remove() method doesn't delete an element based on a position, but rather on a value. If you try to remove a value that isn't in the list, you'll generate an error. score = int(input("Delete which score? ")) if score in scores: scores.remove(score) else: print (score, "isn't in the high scores list.")
in Operator
Checks whether an element is a member of a sequence and returns a boolean.
Concatenating Lists
Combining two lists (note: you can only concatenate the same types of sequences.) chest = ["gold", "gems"] inventory += chest print (inventory)
Creating Nested Sequences
Create a nested list or tuple like always, type each element, followed by a comma scores = [("Moe", 1000), ("Larry", 1500), ("Curly", 3000)]
Appending a Nested Tuple
Create a tuple and append the tuple to the list. name = input("What is the player's name?") score = int(input("What score did the player get? ")) entry = (score, name) scores.append(entry)
Unpacking a Sequence
If you know how many elements are in a sequence, you can assign each to its own variable in a single line of code. Also used to display nested tuples. name, score = ("Shemp", 175) print (name) print (score)
Slicing Lists
Make copies of continuous sections of elements. begin = int(input("Enter the index number to begin a slice: ")) end = int(input("Enter the index number to end the slice: ")) print (inventory[begin:end])
List Methods
Methods that allow you to manipulate lists in different ways.
Lists
Mutable sequences of any type.
.reverse()
Reverses the list order: scores.reverse() print (scores)
Nested Sequences
Sequences inside other sequences. Nested sequences are a great way to organize more complex collections of information.
.sort()
Sorts the elements in the list in ascending order: scores.sort() print (scores)
Indexing Lists
Specify a position (or index) number in a sequence and get the element at that position. index = int(input("Enter the index number for an item in inventory: ")) print ("At index", index, "is", inventory[index])
Creating a List
Use an assignment statement with the list in square brackets. inventory = ["sword", "armor", "shield", "healing potion"]
Accessing Nested Elements
You access elements of a nested sequence through indexing. print (scores[0]) You can also access on of the elements of one of the tuples: Assign the Tuple to a Variable: a_score = scores[2] print (a_score ) print (a_score[0]) Direct Access: print (scores [2][0])
Deleting a List Slice
You can also delete a slice from a list. The length of the list shrinks and the remaining elements form a new, continuous list, starting from position 0. del inventory [0:2] print (inventory)
Assigning a New List Slice
You can assign a new value to a single element or a slice inventory [4:5] = ["orb of future telling", "invisible powers"] print (inventory)
Assigning a New List Element by Index
You can assign an existing list element a new value with indexing. However, you can't create a new element this way. inventory[0] = crossbow print (inventory)
Deleting a List Element
You can delete an element from a list with del by designating the element after del. Deleting an element doesn't create a gap in a sequence. The length of the list shrinks by one and all the elements after the deleted on "slide down" one position. del inventory[2] print (inventory)
len() Function with Lists
len() function will tell you the number of elements in a list: print (len(inventory))