DOC PREVIEW
USF CS 110 - Lists and Tuples

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Lists/TuplesExample•Write a program to keep track of all students’ scores on exam 1.•Need a list of everyone’s score•Use 14 doubles•What about next semester?Lists•Ordered set of valuesscores=[90.5, 73, 87]•Each value accessed by using [ ] and indexscores[1] #would be 73•A list can contain different types -- including other listsscores=[90.5, 73, 87, “Mickey”, [67, 89]]•A list can be emptyscores=[]Examplescores=[90.5, 73, 87]90.573 87scores:Subscripts•Subscript describes which box of the array you are dealing with•List of size N has subscripts –0, 1, 2, … (n-1)–list of size 3 – 0, 1, 2•Subscript can be a variable or expression which produces an integer–scores[i]–scores[i+5]•If subscript specified does not exist – runtime errorRange•Lists of consecutive integers common•Use range() to generate–range(5) #0, 1, 2, 3, 4–range(1,5) #1, 2, 3, 4–range(1, 10, 2) #1, 3, 5, 7, 9Accessing List Elements•Print all elements of the list scores–Use only one print statementAccessing List Elements•Print all elements of the list scores–Use only one print statementscores = [90.5, 73, 82]i=0while(i<3):print scores[i]i = i+1Accessing List Elements•Using 3 as length is error pronescores = [90.5, 73, 82]i=0while(i<len(scores)):print scores[i]i = i+1Accessing List Elements•Another method -- short cut•Membershipscores[90.5, 73, 82]if 100 in scores:print “There is a perfect score!”scores = [90.5, 73, 82]for i in scores:print iOperators -- A lot like strings•Concatenationfirst_scores=[90,56,87]second_scores=[67,100,89]all_scores = first_scores+second_scores•Slicesall_scores[2:4] #yields [87,67] more_scores=all_scores[:] #makes a copy of all_scoresMutability•Lists are mutable - they can be changedscores[90.5, 73, 82]scores[1] = 100 #scores is now [90.5, 100, 82]•Lists can grow - using append function–append takes 1 argumentscores.append(12)scores.append([56, 78, 34])•Lists can shrink - using del function–del takes an element or slicedel scores[2]del scores[1:3]•Other functions - sort, reverseAliasing•Reference assignmentscores=[90.5, 73, 82]new_scores = scoresscores[0] = 100 #new_scores[0] changesscoresnew_scores90.5 73 82Cloningscores=[90.5, 73, 82]new_scores = scores[:]scores[0] = 100 #new_scores[0] DOES NOT changescoresnew_scores90.5 73 8290.5 73 82Exercises1. Use the interpreter to help you determine the outcome of each of the following statements. Why are the invalid statements invalid?list1 = [1,2,3]list2 = list1print list2list2 = list1[2]print list2list2.append(8)print list2list2 = list1list2.append(8)print list2list1[9] = "hello"print list2list1[2] = "hello"print list2Exercises1. Implement a program that prompts the user for a series of strings (using “quit” to end input) and stores them in a list. Then, for every string in the list, display it for the user and ask if he/she would like to delete it. Delete the appropriate strings and display the final list for the user.List Elements as Parameters•Write a function to add two elements of a listList Elements as Parameters•Write a function to add two elements of a list–How is the function called?def adder(num1, num2):return (num1 + num2)List Elements as Parameters•Write a function to add two elements of a list–How is the function called?adder(scores[0], scores[1])Passing Lists•Would like to pass an entire list into a function–Sum all elements, prompt user for valuesExamplesum(scores)def sum(list):sum = 0for i in list:sum += ireturn sumNested Listsexam_scores=[[90, 67, 34], [46, 99, 87], [89,78,67],[87,87,85]]#could represent three scores for four students •Access second score of third studentNested Listsexam_scores=[[90, 67, 34], [46, 99, 87], [89,78,67],[87,87,85]]#could represent three scores for four students •Access second score of third studentexam_scores[2][2]90 67 3446 99 8789 78 6787 87 8501230 1 2Exercises1. Implement a function that takes as input a list of integers and adds 5 to every element.2. Implement a program that prints all elements in a matrix.Tuples•Very similar to lists, but immutable–You can copy, but you cannot modify (append, del, assign new values)•Typically enclosed in ( )scores=(90.5, 73, 87)•An integer - scores = (70)•A tuple of one element - scores = (70,)•Convert a list to a tuple tuple_scores = tuple(list_scores)•Convert a tuple to a list - list_scores = list(tuple_scores)Swapping•Typical swap requires a temp variabletmp = bb = aa = tmp•Tuple assignmenta, b = b, ax, y, z = 4, 6, 8Return Valuesdef getnums() :n1 = input(“Enter number 1:”)n2 = input(“Enter number 2:”)return n1, n2x, y = getnums()print x #prints first number entered print y #prints second number enteredExercises1. Can you think of a way to write a function that takes as input a tuple of integers, adds 5 to every element, and returns a


View Full Document

USF CS 110 - Lists and Tuples

Download Lists and Tuples
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lists and Tuples and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lists and Tuples 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?