Chapter 6 Lists - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Chapter 6 Lists

Description:

Title: PowerPoint Presentation Last modified by: Bernard Chen Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 18
Provided by: gsu93
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 6 Lists


1
Chapter 6 Lists
  • Dr. Bernard Chen Ph.D.
  • University of Central Arkansas
  • Spring 2012

2
Lists
  • Lists are Pythons most flexible ordered
    collection object type
  • Lists can contain any sort of object numbers,
    strings and even other lists

3
Lists
  • Ordered collections of arbitrary objects
  • --- from the functional view, lists are just a
    place to collect other objects
  • --- Lists also define a left to right positional
    ordering of the items in the list

4
Lists
  • Accessed by offset
  • --- Just as strings, you can fetch a component
    object out of a list by indexing the list on the
    objects offset
  • --- you can also for such tasks as slicing and
    concatenation

5
Lists
  • Variable length, heterogeneous, arbitrary
    nestable
  • --- Unlike strings, list can grow and shrink in
    place
  • --- lists may contain any sort of object, not
    just one-character strings (they are
    heterogeneous)

6
Lists
  • gtgtgt aa An empty list
  • gtgtgt aa1,2,3,4 4 items, index 0-3

7
List in action
  • List respond to the and operations much like
    strings
  • gtgtgt aa1,2,3
  • gtgtgt bb4,5,6
  • gtgtgt aabb
  • gtgtgt aa3

8
List in action
  • Lists also have the function len() to tell the
    size of lists and in function
  • gtgtgt aa1,2,3
  • gtgtgt len(aa)

9
Indexing, Slicing, and Matrixes
  • Besides lists are sequences, index and slicing
    work the same way
  • gtgtgt aa1,2,3
  • gtgtgt aa2
  • gtgtgt aa-2
  • gtgtgt aa1
  • (also try some strings in aa)

10
Indexing, Slicing, and Matrixes
  • We can also assign lists in a list
  • gtgtgt matrix1,2,3,4,5,6,7,8,9
  • gtgtgt matrix0
  • gtgtgt matrix10
  • gtgtgt matrix02

11
Changing Lists in-place
  • When using a list, you can change its contains b
    assigning to a particular item (offset), or an
    entire section (slice)
  • gtgtgt aa1,2,3
  • gtgtgt aa04
  • gtgtgt aa15,6

12
List method calls
  • The list append method simply tacks a single item
    onto the end of the list
  • gtgtgt aa
  • gtgtgt aa.append(1)
  • gtgtgt aa.append(2)
  • gtgtgt aa.append(3)
  • gtgtgt aa.append(4)
  • gtgtgt aa.append(5.0)

13
List method calls
  • The sort function orders a list in place (in
    ascending fashion)
  • gtgtgt aa4,2,6,8,1,3,4,10
  • gtgtgt aa.sort()
  • (also try strings in lists)

14
List method calls
  • reverse reverse the list in-place
  • gtgtgt aa1,2,3,4
  • gtgtgt aa.reverse()

15
List method calls
  • del can used to delete an item or section
  • gtgtgt aa1,2,3,4,5,6
  • gtgtgt del aa0
  • gtgtgt del aa2

16
In Class Practice
  • gtgtgt a1,2,3
  • gtgtgt b4,5,6
  • gtgtgt bba
  • gtgtgt b
  • gtgtgt b.sort()
  • gtgtgt b
  • gtgtgt b.append(7)
  • gtgtgt b

17
In Class Practice
  • gtgtgt a1,2,3
  • gtgtgt b4,5,6
  • gtgtgt c7,8,9,10
  • gtgtgt array
  • gtgtgt array.append(a)
  • gtgtgt array.append(b)
  • gtgtgt array.append(c)
  • gtgtgt array2
  • gtgtgt array00
  • gtgtgt array23
Write a Comment
User Comments (0)
About PowerShow.com