1 / 19

Course A201: Introduction to Programming

Course A201: Introduction to Programming. 10/28/2010. 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

zinnia
Download Presentation

Course A201: Introduction to 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. 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] • input1, input2, input3, … • You give me some inputs I am a function. I will perform a certain job. • I give you some outputs back, explicitly or implicitly • output1, output2, output3, …

  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()?

  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!

  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~

More Related