Data Structures 810:052 More with Python collections - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Data Structures 810:052 More with Python collections

Description:

... Python list is an ordered, mutable, collection of comma-separated values of any ... Complete the 'Working with Lists as Collections' and Lists are Mutable pt. ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 26
Provided by: systema178
Category:

less

Transcript and Presenter's Notes

Title: Data Structures 810:052 More with Python collections


1
Data Structures810052More with Python
collections
2
Review - Lists
  • A Python list is an ordered, mutable, collection
    of comma-separated values of any data type
    enclosed in square brackets
  • faculty Schafer , Fienup, East ,
    Drake
  • rhyme 1, 2, buckle, your , shoe
  • empty this is an empty list

3
Your turn
  • Complete the Working with Lists as Collections
    and Lists are Mutable pt. 1 sections of your
    handout

4
Additional methods just for lists
5
Your Turn
  • Complete the Lists are mutable pt. 2 activity
    on your handout

6
The range function
  • As you get started, one of the most commonly used
    lists is created by the range function.
  • The range() function is used to generate a list
    of equally spaced integers
  • range(start, stop ,step)

7
The range function
  • gtgtgt range(10)
  • 0,1,2,3,4,5,6,7,8,9
  • gtgtgt range(2,10)
  • 2,3,4,5,6,7,8,9
  • gtgtgt range(2,10,2)
  • 2,4,6,8

8
Your turn
  • Complete the Range activity on your handout

9
One place we use lists
  • Iteration Python provides two forms of
    iteration
  • for
  • while

10
The for statement
  • The for statement is designed to iterate over a
    list or other sequence of data.
  • Its syntax is
  • for variable reference_name in list_name
  • block
  • of
  • code

11
The for statement
  • gtgtgt wordlist linux, windows, mac
  • gtgtgt for word in wordlist
  • print word str(len(word))
  • linux 5
  • windows 7
  • mac 3

12
The for statement
  • gtgtgt for num in range(1,11)
  • print str(num)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

13
The while statement
  • The while statement is designed to continue to
    loop as long as ome condition is true
  • Its syntax is
  • while boolean_condition
  • block
  • of
  • code

14
The while statement
  • gtgtgt number 1
  • gtgtgt while numberlt10
  • print str(number)
  • number number 1
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

15
A String is a sequence/collection of data
16
Strings are immutable
  • gtgtgt rhyme 1,2,"buckle","my","shoe"
  • gtgtgt rhyme4"flu
  • gtgtgt rhyme
  • 1, 2, 'buckle', 'my', 'flu'
  • gtgtgt lastname Schafer
  • gtgtgt lastname3o
  • Traceback (most recent call last)
  • File "ltpyshell21gt", line 1, in ltmodulegt
  • lastname3o
  • TypeError 'str' object does not support item
    assignment

17
But Strings respond to additional methods
(The string module can be imported to provide
more string operations.)
18
Your Turn
  • Complete the section named Working with Strings
    on your handout

19
Tuples
  • A tuple is an immutable sequence of data defined
    by a sequence of commea-separated items enclosed
    in parentheses
  • student1 (Bob, 123456, Jr, 3.12)
  • student2 (Sally, 654321, Fr, 0.0)
  • Fields of a tuple can be unpacked to its
    components using a single assignment statement
    as
  • name, idnum, rank, gpa student1

20
Your Turn
  • Complete the Tuples section on your handout

21
Dictionary
  • A dictionary is an unordered set of key-value
    pairs (written as keyvalue).
  • Keys must be unique and immutable (e.g.,
    numerics, strings, tuples of immutable objects).
  • Dictionaries are typically used to lookup the
    value corresponding to a specified key.

22
Dictionary
  • Dictionaries can be written as comma-separated
    keyvalue pairs enclosed in curly braces. For
    example
  • phoneNumbers fienup35918,
  • gray35917,
  • east32939,
  • drake35811,
  • schafer32187

23
Dictionary
  • Access to individual keyvalue pairs looks
    syntactically like a sequence lookup using a key
    instead of an index.
  • For example
  • gtgtgt phoneNumberseast
  • 32939
  • gtgtgt phoneNumberswallingford35919

24
Dictionary Methods
25
Your Turn
  • Complete the Dictionary section of your handout
Write a Comment
User Comments (0)
About PowerShow.com