Presentation is loading. Please wait.

Presentation is loading. Please wait.

More on Lists.

Similar presentations


Presentation on theme: "More on Lists."— Presentation transcript:

1 More on Lists

2 Programming Challenge
Write a program that continually asks the user for the price of an item, until the user enters “end” You should also ensure that the program will never crash (try/except) At the end, print out the total cost of the items, the total number of items bought, and a list of the price of each item

3 Slicing Lists Sometimes, you need to extract multiple items from a list Python contains functions that allow you to do this list_1 = [‘zero’, ‘one’, ‘two’, ‘three’, four’, ‘five’] list_2 = list_1[1:5] print(list_2) >> [‘one’, ‘two’, ‘three’, ‘four’]

4 Slicing Lists The rules and notation for slicing a list are more or less the same as slicing a string new_list = old_list[start : end : step] If you do not specify a start, Python will assume index 0 and if you do not specify end, Python will assume the last element Reminder that Python will not grab the element equivalent to the end index, it will go up to that index

5 Let’s Practice Given the following list:
my_list = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘I’, ‘j’, ‘k’, ‘l’] Write a program that: Extracts the first 7 elements into a new list Extracts the characters b, c, and d into a new list Extracts the last 5 characters into a new list

6 Programming Challenge
Write a program that creates a list of all integers between 1 and 100 and then extract all numbers divisible by 6 Try this for all numbers divisible by 9

7 Finding items in a list You can easily find a particular item in a list by using the “in” operator my_list = [‘pie’, ‘cake’, ‘pizza] if ‘cake’ in my_list: print(“I found cake!”) else: print(“Sorry, there’s no cake”) The “in” operator will return a Boolean value the indicates whether an item was found or not

8 Programming Challenge
Given these two lists: by6 = [6,12,18,24,40,46,42,48,54,60,66,72,78,84,90,96] by9 = [9,18,27,36,45,54,63,72,81,90,99] Write a program that finds all elements that exist in both lists. Then store your results in a new list and print it out to the user.

9 Adding items to a list We have already observed two ways in which we can add to a list: Repeating the list by using the “*” operator Concatenating lists by using the “+” operator Another way we can do this is by using the “append” method The append method allows you to add an item to the end of a list (recall that methods are specific to objects)

10 Adding items to a list mylist = [“Charis”, “Dan”, “Jin”, “Sam”] mylist.append(“AJ”) print(mylist) >> [“Charis”, “Dan”, “Jin”, “Sam”, “AJ”]

11 Removing items from a list
You can remove items from a list by using the “remove” method prices = [3.99, 2.99, 1.99] prices.remove(2.99) print(prices) >> [3.99, 1.99] Note that you will raise an exception if you try to remove an item that is not found in the list (this would be a good place to use try/except)

12 Removing items from a list
There’s one more way to remove items from a list but you will need to know the index of the item you wish to remove We will use the “del” method prices = [3.99, 2.99, 1.99] del prices[0] print(prices) >> [2.99, 1.99]

13 Programming Challenge
There are teachers who calculate a student’s average by dropping their lowest test grade Assuming these are the grades of a student, calculate his “normal” average first and then calculate his average after dropping the lowest score grades = [99, 86, 90, 42, 89, 100]

14 Programming Challenge
Write a program that continually prompts the user for a name and store all those names in a list Then, sort the names in alphabetical order and store them in a new list Finally, print out the new list of names in alphabetical order

15 Sorting items in a list Fortunately, there’s a method for that in Python my_list = [‘Donald’, ‘Bryan’, ‘Andrew’, ‘Mike’] my_list.sort( ) print(my_list) >> [‘Andrew’, ‘Bryan’, ‘Donald’, ‘Mike’]


Download ppt "More on Lists."

Similar presentations


Ads by Google