Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,

Similar presentations


Presentation on theme: "CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,"— Presentation transcript:

1 CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu 1

2 Objectives In this chapter, you will do some exercises about: – Sequences String List Tuple – Dictionary 2

3 Multiple Choice ______ are "associated arrays" that consist of key-value pairs. – A. sequences B. lists C. arrays D. dictionaries Function ____ returns the length of a sequence. – A. len() B. length() C. size() D. Length() The last element in a sequence (with more than one element), seq, can always be accessed with subscript ____. – A. 0 B. len(seq) C. len(seq)-1 D. -1 Selecting a portion of a sequence with the operator [1:5] returns the range: – A. [1, 5] B. [0, 4] C. [0, 5] D. [1, 4] Dictionary method ______ returns a list of key-value pairs. – A. keys() B. items() C. values() D. print() 3

4 Multiple Choice (cont'd) Use the expression ____ to obtain the i-th element through the j-th element of list, sequence, inclusive. – A. sequence[i, j] B. sequence[i, j+1] – C. sequence[i : j] D. sequence[i : j+1] List method _____ returns the number of times a specified element occurs in a list. – A. size() B. length() C. count() D. len() Which of the following statements is false to iterate all elements in the list, list? – A. for item in list – B. for i in range(len(list)) – C. for i in range(0, len(list)) – D. for i in range(0, len(list), 2) The list units's second element is _______. – A. units{1} B. units(2) C. units[0: 2] D. units [1] 4

5 Multiple Choice (cont'd) Which of the following sorts list averageRainfall? – A. list(averageRainfall).Sort() – B. Sort.list(averageRainfall) – C. Sort(averageRainfall) – D. averageRainfall.sort() Which of the following returns the index of a searchKey in a list averageRainfall? – A. averageRainfall[searchKey] – B. averageRainfall.search(searchKey) – C. averageRainfall.index(searchKey) – D. averageRainfall.find(searchKey) 5

6 True/False Statements A sequence begins at subscript 1. Strings and tuples are mutable sequences. Each key-value pair in a dictionary has the form key:value. Using a tuple as a dictionary key is an error. Dictionary values are accessed with the dot operator. 6

7 True/False Statements (cont'd) Method insert adds one element to the end of a list. The += statement appends items into lists. List method sort sorts the elements of a list in place. If list method search finds a list value that matches the search key, it returns the subscript of the list value. The initialization of a list is as follows: – myList = (1, 2, 3) 7

8 Debugging Errors 1.List = (1, 2) 2.For item in List 3.print (list[item]) 4.For i in range(len(List)) 5.List[i] *= 2 6.i=3 7.While i<10 8.List[i] = i 8

9 Debugging Errors (cont'd) Def Function (tuple) Print(tuple) Return x 9

10 Write a Program Write a complete Python program to generate random number between 1 and 99, and output the resulting number to a list, myList. 10

11 Write a Program (cont'd) Initialize a list, numList, to hold 100 random integer numbers within [1, 100] Sort this list Check whether a number 50 exists in this array using the linear search and the for loop Output the multiplication of all even integers in this list; if no even numbers exist, then output 1 11

12 12


Download ppt "CSCI/CMPE 4341 Topic: Programming in Python Chapter 6: Lists, Tuples, and Dictionaries – Exercises Xiang Lian The University of Texas – Pan American Edinburg,"

Similar presentations


Ads by Google