Presentation is loading. Please wait.

Presentation is loading. Please wait.

FINAL WORDS ON LISTS. Sorting items in a list  We discussed how Python has a function for sorting out a list and that this could make our lives so much.

Similar presentations


Presentation on theme: "FINAL WORDS ON LISTS. Sorting items in a list  We discussed how Python has a function for sorting out a list and that this could make our lives so much."— Presentation transcript:

1 FINAL WORDS ON LISTS

2 Sorting items in a list  We discussed how Python has a function for sorting out a list and that this could make our lives so much easier  Especially on that quiz …  Well here it is

3 Sorting items in a list my_list = [‘Donald’, ‘Bryan’, ‘Andrew’, ‘Mike’] my_list.sort( ) print(my_list) >> [‘Andrew’, ‘Bryan’, ‘Donald’, ‘Mike’] ** Notice that a new list is not created, rather the list you called is rearranged in alphabetical order

4 Find the position of an item in a list  You can also find the position (by index) of an item in a list  We will use the “index” method  This method takes one argument, an element to search for and returns an integer value, equivalent to the index of the item  The index method will raise an exception if the item searched for is not found

5 Find the position of an item in a list Example: my_list = [“pizza”, “pie”, “cake”] if “pie” in my_list: location = my_list.index(“pie”) print(“pie is at position #”, location) else: print(“not found!”)

6 Programming Challenge  Given that the following lists match up with one another (the items are listed in the same order of their respective prices), write a product price lookup program. products = [‘peanut butter’, ‘jelly’, ‘bread’] prices = [3.99, 2.99, 1.99]

7 Grabbing the largest/smallest items in a list  Python also has two built in functions that allow you to get the highest and lowest values in a list prices = [3.99, 2.99, 1.99] biggest = max(prices) smallest = min(prices) print(smallest, “up to”, biggest)

8 Grabbing the largest/smallest items in a list  These functions also work with strings, but the max( ) function will return the string with greatest ASCII value (meaning, closest to the end of the alphabet) and the min( ) will return the string that would appear first in the dictionary  Recall that when comparing strings, Python will compare one character at a time  You cannot use the max( ) and min( ) functions when working with lists of varying data types (comparing a float to a string will raise an exception)

9 Grabbing the largest/smallest items in a list products = [‘bananas’, ‘apples’, ‘donuts’, ‘carrots’, ‘asparagus’] biggest = max(products) smallest = min(products) print(smallest, “to”, biggest) >> apples to donuts

10 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)

11 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]

12 Programming Challenge  Somewhere on God’s green earth, there are teachers who exhibit the concept of grace in their classrooms and calculate a student’s average by dropping their lowest test grade  Assuming these are the grades of a student, calculate his average by this God-given gracious method grades = [99, 86, 90, 42, 89, 100]

13 Programming Challenge  Write a weight loss program that prompts the user to enter in 7 days of weight values  At the end of the program, print out the following:  Weight on the first day  Weight on the last day  Change from first day to last day  Average weight for period  Highest weight during period  Lowest weight during period

14 Roulette  Roulette is a game where a ball is rolled around a circular track – eventually the ball will come to a stop in a slot that is labeled with the numbers 0 to 36  Gamblers can bet on an individual number – if they win, the win a large prize (36:1)

15 Roulette  Write a program that asks the user for a number between 0 and 36  Then spin the roulette 1000 times. Find out how many times the user’s # came up  Tell the user the most frequent number and the least frequent number that came up

16 Programming Challenge  Write a program that takes all the elements in a list and reverses them  In other words: prices = [3.99, 2.99, 1.99] newlist = [1.99, 2.99, 3.99]

17 Reversing items in a list  You can also reverse the order of the items in a list by using “reverse”  This will not sort the list in reverse order – it will simply shuffle the elements of a list such that the first will be last and the last will be first prices = [3.99, 2.99, 1.99] prices.reverse( ) print(prices) >> [1.99, 2.99, 3.99]

18 Splitting a string  Sometimes, you are given long strings of information that need to be “parsed” in a particular way  For example: x = “Donald, Seok, Math Teacher”  This string contains three pieces of information that are each separated by a comma

19 Splitting a string  We can use a technique called “splitting” to “unpack” a string and extract it’s individual values. The trick is to isolate a separator character that can be used to delimit the data you want to extract. x = “Donald, Seok, Math Teacher” splitstring = x.split(“, ”) print(splitstring) >> [‘Donald’, ‘Seok’, Math Teacher]

20 Programming Challenge  The following string represents the test scores of a student scores = “95, 100, 67, 33, 88”  Write a program that:  Prints out the average score  Print out the highest and lowest score  Drops the lowest score and prints out the student’s average without that score

21 Joining a list back into a string  You can reverse the “split” process and turn a list back into a string  You must create a new string variable with the separator you wish to have between the elements of the list you are joining x = [‘a’, ‘b’, ‘c’] s = ‘,’ # new variable with comma separator (this can be anything) y = s.join(x) # join method called on string object, with list as argument print(y) >> a,b,c


Download ppt "FINAL WORDS ON LISTS. Sorting items in a list  We discussed how Python has a function for sorting out a list and that this could make our lives so much."

Similar presentations


Ads by Google