1 / 18

COMPSCI 101 S1 2014 Principles of Programming

COMPSCI 101 S1 2014 Principles of Programming. 18 Iterating through sequences using index values. Recap. A list stores data as a sequence len () returns the number of elements in a list Each element of the list can be accessed We use a for loop to iterate through the contents of a list

zyta
Download Presentation

COMPSCI 101 S1 2014 Principles of Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMPSCI 101 S1 2014Principles of Programming 18 Iterating through sequences using index values

  2. Recap • A list stores data as a sequence • len() returns the number of elements in a list • Each element of the list can be accessed • We use a for loop to iterate through the contents of a list • Index • Elements are the individual items in each sequence • Lists can be indexed to get individual entries. Value of an index can go from 0 to (length of list - 1). • Negative indices can be used to index the list from the right. COMPSCI101

  3. Learning outcomes • At the end of this lecture, students should be able to: • Iterate through sequences using index values • Examples • Case Study 1: Printing in reverse order • Case Study 2: Calculating difference between adjacent elements • Case Study 3: Is the list sorted? • Exercise • Printing elements of a list using negative indices COMPSCI101

  4. 18.1 Iterating sequences Using an Element Variable • Many times you need to do something to every element in a sequence. • You can do a traversal using a for loop as follows: for ELEMENT_VAR in SEQUENCE: STATEMENT my_list = [10, 20, 30, 40, 50] If no more items in sequence Element variable 10 20 30 40 50 for element in my_list: print(element)

  5. 18.1 Iterating sequencesUsing Index Values • Iterate through the elements of a list using index values • Example: • index: 0, element: my_list[0], (10) • index: 1, element: my_list[1], (20) • … • Index: index, element: my_list[index] • Therefore, available index values: 0, 1, 2, 3, 4 my_list = [10, 20, 30, 40, 50] Index position 10 20 30 40 50 for index in ????: print(my_list[index]) COMPSCI101

  6. 18.1 Iterating sequencesUsing Index Values • Using index values: • It is not convenient to use a list of numbers • A better approach: 10 20 30 40 50 Available index values for index in [0,1,2,3,4]: print (my_list[index]) elements i.e. range(0,5) Range object: [0, 1, 2, 3, 4] for index in range(len(my_list)): print (my_list[index]) COMPSCI101

  7. Case Study 1Printing in reverse order Demo 1 • Using the range function() • Example: • Positive indices: 4, 3, 2, 1, 0 • Negative indices: -1, -2. -3, -4, -5 50 40 30 20 10 my_list = [10, 20, 30, 40, 50] for index in range(4, -1, -1): print (my_list[index]) for index in range(-1, -6, -1): print (my_list[index]) COMPSCI101

  8. Case Study 1Printing in reverse order Demo 2 • Task: • Complete the print_reverse_list()function which prints a list in reverse order using positive indices • Arguments: an integer list my_list = [10, 20, 30, 40, 50] my_list = [10, 20, ...] for index in range(4, -1, -1): print (my_list[index]) for index in range(start, -1, -1): print (my_list[index]) COMPSCI101

  9. Case Study 1Printing in reverse order Demo 2 • Task: • Complete the print_reverse_list_neg()function which prints a list in reverse order using negative indices • Arguments: an integer list my_list = [10, 20, 30, 40, 50] my_list = [10, 20, ...] for index in range(-1, -6, -1): print (my_list[index]) end = -len(my_list) -1 for index in range(-1, end, -1): print (my_list[index]) COMPSCI101

  10. Exercise 1Printing elements of a list using… • Odd indices • Task: • Complete the print_odd_list()function which prints elements of a list using odd indices • Arguments: an integer list 10 30 50 print_odd_list([10, 20, 30, 40, 50]) 10 30 print_odd_list([10, 20, 30, 40]) for index in range(0, len(numbers), 2): print (numbers[index]) COMPSCI101

  11. Case Study 2Calculating the difference between adjacent items • Task: • Complete the calc_difference_list()function which calculates the difference between adjacent items in a list • Arguments: an integer list • Case: 14 13 5 14 2 13 calc_difference_list([ 0, 5, 19, 21, 34]) 5 2 COMPSCI101

  12. Case Study 2Calculating the difference between adjacent items Demo 3 • Algorithm: • Iterate through the elements of a list using index values • A for loop + range • Calculate the difference: calc_difference_list([ 0, 5, 19, 21, 34]) for index in range(0, len(numbers)-1): numbers[index+1]-numbers[index] COMPSCI101

  13. Case Study 3Is the list sorted? • Task: • Complete the is_sorted()function which returns true if the list is increasing, false otherwise. • Arguments: an integer list • Returns: true if it is in increasing order, false otherwise • Cases: True is_sorted([3, 5, 9, 11]) False is_sorted([3, 9, 5, 11]) COMPSCI101

  14. Case Study 3Is the list sorted? • Algorithm: • for each iteration • Compare adjacent values, if they are not in order, stop and return False • return True 3 > 5? 5 > 9? 9 > 11? COMPSCI101

  15. Case Study 3Is the list sorted? • Algorithm: • for each iteration • Compare adjacent values, if they are not in order, stop and return False • return True 3 > 9? 9 > 5? STOP COMPSCI101

  16. Case Study 3Is the list sorted? Demo 4 • Algorithm: • Iterate through the elements of a list using index values • A for loop + range • Sorted? is_sorted([3, 5, 9, 11]) for index in range(len(numbers)-1): numbers[index] numbers[index+1] numbers[index] > numbers[index+1] COMPSCI101

  17. Exercise 2 • Task: • Complete the mult3()function which takes a list of integers as a parameter and prints only the multiples of 3, one per line • Use the range function to access all elements in a list. • Arguments: an integer list • Returns: a sub-list of number Cases: mult3([3, 1, 6, 2, 3, 9, 4, 9]) 3 6 3 9 9 COMPSCI101

  18. Summary • A list stores data as a sequence • len() returns the number of elements in a list • Each element of the list can be accessed using the index operator • Positive index: count from the left, starting with 0 • Negative index: count from right, starting with –1 • We use a for loop to iterate through the contents of a list COMPSCI101

More Related