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

1 / 17
About This Presentation
Title:

Data Structures 810:052 More with Python collections

Description:

[1, 2, 'buckle', 'my', 'flu'] lastname = 'Schafer' lastname[3]='o' ... TypeError: 'str' object does not support item assignment ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 18
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
A String is a sequence/collection of data
3
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

4
But Strings respond to additional methods
(The string module can be imported to provide
more string operations.)
5
Tuples
  • A tuple is an immutable sequence of data defined
    by a sequence of comma-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

6
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.

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

8
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

9
Dictionary Methods
10
Defining your own classesChapter 8Intro to
Lab02
11
Data types so far
  • Numbers
  • Contain no methods and one value
  • Tuples
  • Contain no methods and several values
  • Strings
  • Contain methods and one value
  • Lists and Dictionaries
  • Contain methods and several values

12
These are examples of Objects
  • Objects normally consist of
  • Values (data) often referred to as State
  • Methods often referred to as Behavior

13
So what do you do if you want to create a new
type of Object
  • Earlier I made reference to data about a student
  • First Name string
  • Middle Initial character
  • Last Name - string
  • Age - integer
  • GPA floating point number
  • Hometown - string
  • Do they have an advisor hold boolean

14
How could I store data about a Student as a
single object?
  • Could use Tuples
  • student1 (James , B , Jones , 19 , 3.24 ,
    Ames , True)
  • What are some of the problems with this?

15
Instead we might decide to make our own Student
class
  • !/usr/local/bin/python
  • class Student
  • def __init__(self,first,middle,last,age,gpa,ho
    metown,hold)
  • self.firstfirst
  • self.middlemiddle
  • self.lastlast
  • self.ageage
  • self.gpagpa
  • self.hometownhometown
  • self.holdhold

16
Instead we might decide to make our own Student
class
  • class Student
  • def getName(self)
  • return self.first" "self.middle"
    "self.last
  • def getAge(self)
  • return self.age
  • def getGPA(self)
  • return self.gpa
  • def modifyGPA(self,newGPA)
  • self.gpanewGPA
  • def hasHold(self)
  • return self.hold

17
Demoing some features
  • def studentDemo()
  • student1Student("John","A","Doe",23,4.0,"Ames
    ",True)
  • print student1.getName()
  • print student1.getGPA()
  • student1.modifyGPA(3.66)
  • print student1.getGPA()
  • print student1.hasHold()
Write a Comment
User Comments (0)
About PowerShow.com