Download presentation
Presentation is loading. Please wait.
1
Lists
2
Lists Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you've already learned about include strings, numbers, and booleans.) You can assign items to a list with an expression of the form list_name = [item_1, item_2] with the items in between brackets. A list can also be empty: empty_list = []. Lists are very similar to strings, but there are a few key differences.
3
Example 1 zoo_animals = [”monkey", ”giraffe", "sloth", ”elephant” ]; if len(zoo_animals) > 3: print ("The first animal at the zoo is the " + zoo_animals[0]) print ("The second animal at the zoo is the " + zoo_animals[1]) print ("The third animal at the zoo is the " + zoo_animals[2]) print ("The fourth animal at the zoo is the " + zoo_animals[3])
4
List Indexing You can access an individual item on the list by its index. An index is like an address that identifies the item's place in the list. The index appears directly after the list name, in between brackets, like this: list_name[index]. List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]. The second item in a list is at index 1: list_name[1]. Computer scientists love to start counting from zero.
5
Example 2 numbers = [5, 6, 7, 8] print ("Adding the numbers at indices 0 and 2“) print (numbers[0] + numbers[2]) print ("Adding the numbers at indices 1 and 3“) print (numbers[1] + numbers[3])
6
Editing Lists zoo_animals = [”Monkey", ”Snake", "sloth", "tiger"] #Last night our zoo's sloth brutally attacked #the poor tiger and ate it whole. # The ferocious sloth has been replaced by a friendly hyena. zoo_animals[2] = "hyena" # What shall fill the void left by our dear departed tiger? zoo_animals[3] = "rhino"
7
What will this print? letters = ['a', 'b', 'c'] letters.append('d') print len(letters) print letters letters = ['a', 'b', 'c'] letters.append('d') adds d to the end print len(letters) prints amount of letters =4 print letters prints abcd
8
Negative List Indices li = ['a', 'b', 'mpilgrim', 'z', 'example']
Returns 'example' >>> li[-3] 2 Returns 'mpilgrim'
9
Slicing a List >>> li
Returns ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] Returns ['b', 'mpilgrim'] >>> li[1:-1] Returns ['b', 'mpilgrim', 'z'] >>> li[0:3] Returns ['a', 'b', 'mpilgrim']
10
Searching Lists >>> li #update list as shown below
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.index("example") Returns 5 >>> li.index("new") Returns 2
11
Deleting List Elements
Returns ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] >>> li.remove("z") Returns ['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements'] >>> li.remove("new") Returns ['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements'] #only removes the first occurrence >>> li.remove("c") #what will happen if not in list?
12
Task – Rock, Paper, Scissors
Create a program that solves the following, you could write an algorithm to aid your planning: You are required to create a Rock, Paper, Scissors game where the user will input their selection and that will be compared to a random computer response. You should create a list to store the three possible computer responses and then use the random.randint function to select a random number that in turn selects one of the items from the list. Hints – you must store the user and computer choices in variables. To compare the selections, a number of nested IF (IF within an IF) statements are required. Developments – Use a while loop to play again. Use variables to store and display the user and computer scores.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.