Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course A201: Introduction to Programming 10/28/2010.

Similar presentations


Presentation on theme: "Course A201: Introduction to Programming 10/28/2010."— Presentation transcript:

1 Course A201: Introduction to Programming 10/28/2010

2 Outline for this week List – Lists are mutable!!; List indexing and slicing; List functions Access Items in nested sequences String function split() and join() Shared references and list COPY Dictionary – Structure; How to use get(); other dictionary functions

3 Lists are Mutable Change Item value using index in list Strings and Tuples are NOT mutable! >>> b = [Python, C++, Java, HTML] >>> b[0] = PHP >>> print(b) >>> b[1:3] = [PHP] >>> print(b) >>> b[1:2] = PHP What will happen if you forgot to put PHP in []?

4 Lists are Mutable Change Item value using index in list Strings and Tuples are NOT mutable! >>> b = [Python, C++, Java, HTML] >>> b[0] = PHP >>> print(b) >>> b[1:3] = [PHP] >>> print(b) >>> b[1:3] = PHP b will become: [PHP, P, H, P] So, DO NOT forget [].

5 Lists are Mutable Delete items by using keyword del >>> b = [Python, C++, Java, HTML] >>> del b[1] >>> print(b) >>> del b[:] >>> print(b)

6 List Indexing and Slicing Just like String/Tuple indexing and slicing >>> b = [Python, C++, Java, HTML] >>> b[0:2] >>> b[-2] >>> b[0:1000] >>> b[4]

7 List Methods Refer to Lecture Slides: List Methods

8 Recall Function&Method [functions] I am a function. I will perform a certain job. input1, input2, input3, … output1, output2, output3, … You give me some inputs I give you some outputs back, explicitly or implicitly

9 List Methods What are the jobs of these list methods perform and what are the return values? So, how about append(), remove(), sort(), index(), insert()? List Method Its JobReturn ValueChange original variable? count()Count how many times one item appears in the list One integer. if this item is not in the list, then return value is 0. No pop()Remove the item from list at the specified index The item that at the position of index. Yes

10 List Methods The return values of append(), remove(), sort() and insert() are None! Try this: >>> b = [Python, C++, Java, HTML] >>> b = b.append(Perl) >>> print(b) None So, NEVER NEVER write code in this manner! List Method Its JobReturn ValueChange original variable? append()Add one item into the list, you can specify the position NoneYes

11 Access Items in nested sequences Refer to Lecture Slides: Access Items in nested sequences

12 Access Items in nested sequences Try this: for i in range(len(scores)): print(i, :, scores[i][0], scored, scores[i][1]) vs. for i in range(len(scores)): name, score = scores[i] print(i, :, name, scored, score) A special way of assignment two variables at the same time using tuple: name, score = (Joe, 2000)

13 An example with Lists Refer to Lecture Slides: An example with Lists

14 More math functions Refer to Lecture Slides: More functions (related to your assignment this week)

15 String method: split() Split one string into a list, you can specify the split- symbol >>> string = 1 2 3 4 >>> string.split() [1, 2, 3, 4] >>> string = 1,2,3,4 >>> string.split(,) [1, 2, 3, 4] The value in variable string stays the same!

16 String method: join() Join all the elements in one list into one string, you can also specify the join-symbol >>> string = # >>> list1 = [1, 2, 3, 4] >>> string.join(list1) 1#2#3#4 >>> string = % >>> string.join(list1) 1%2%3%4 The value in variable string, and list1 stays the same!

17 Shared reference and COPY Refer to Lecture Slides: Shared reference >>> list1 = [1, 2, 3, 4] >>> list2 = list1 >>> list3 = list1[:] >>> list1[0] = hello >>> print(list1) [hello, 2, 3, 4] >>> print(list2) [hello, 2, 3, 4] >>> print(list3) [1, 2, 3, 4] This is shared reference This is making a COPY

18 Dictionaries Refer to Lecture Slides: Dictionaries

19 Have a nice evening! See you tomorrow~


Download ppt "Course A201: Introduction to Programming 10/28/2010."

Similar presentations


Ads by Google