21
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Embed Size (px)

Citation preview

Page 1: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Tuples and Dictionaries

Intro to Computer Science

CS1510, Section 2

Dr. Sarah Diesburg

Page 2: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Homework Assignments

PA07 regular deadline is today - 11/4 New homework PA08 assigned today Elise is available to help with homework in-

person or in email Email: [email protected] 3-4pm M/W in WRT 339

Page 3: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Tuples

Last time we finished up talking about lists We made an anagram program

Today we will briefly go over tuples Like lists, but immutable!

Page 4: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Tuples

Tuples are easy: they are simply immutable lists.

They are designated with a comma: Not the parenthesis!

myTuple = (1,’a’,3.14,True)

newTuple = (,) #Empty tuple

anotherTuple = 2,3 #Tuple without parenthesis

Page 5: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

The Question is, Why?

The real question is, why have an immutable list, a tuple, as a separate type?

An immutable list gives you a data structure with some integrity, some permanency, if you will.

You know you cannot accidentally change one.

Page 6: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Lists and Tuples

Everything that works with a list works with a tuple except methods that modify the list/tuple.

Thus indexing, slicing, len, print all work as expected.

However, none of the mutable methods work: append, extend, remove.

Page 7: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Anagram Example with Tuples Let’s see if we can modify our anagram

program to use tuples

7

Page 8: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Chapter 9Dictionaries

Page 9: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

More Data Structures

We have seen the string and list data structures and their uses.

In particular, the dictionary is an important, very useful part of Python as well as generally useful to solve many problems.

Page 10: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

What is a Dictionary?

In data structure terms, a dictionary is better termed an associative array or associative list or a map.

You can think if it as a list of pairs, where the first element of the pair, the key, is used to retrieve the second element, the value.

Thus we map a key to a value.

Page 11: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Key Value Pairs

The key acts as a “lookup” to find the associated value.

Just like a dictionary, you look up a word by its spelling to find the associated definition.

A dictionary can be searched to locate the value associated with a key.

Page 12: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Python Dictionary Use the { } marker to create a dictionary Use the : marker to indicate key:value

pairs:contacts= {‘bill’: ‘353-1234’, ‘rich’: ‘269-1234’, ‘jane’: ‘352-1234’}print (contacts){‘jane’: ‘352-1234’, ‘bill’: ‘353-1234’, ‘rich’: ‘369-1234’}

Page 13: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Page 14: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Keys and Values

Key must be immutable: strings, integers, tuples are fine lists are NOT

Value can be anything.

Page 15: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Collections but not a Sequence Dictionaries are collections, but they are not

sequences like lists, strings or tuples: there is no order to the elements of a dictionary in fact, the order (for example, when printed)

might change as elements are added or deleted. So how to access dictionary elements?

Page 16: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Access Dictionary ElementsAccess requires [ ], but the key is the index!

myDict={} an empty dictionary

myDict[‘bill’]=25 added the pair ‘bill’:25

print(myDict[‘bill’]) prints 25

Page 17: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Dictionaries are Mutable

Like lists, dictionaries are a mutable data structure: you can change the object via various operations,

such as index assignment

myDict = {‘bill’:3, ‘rich’:10}

print (myDict[‘bill’] ) # prints 3

myDict[‘bill’] = 100

print (myDict[‘bill’]) # prints 100

Page 18: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Again, Common Operators

Like others, dictionaries respond to these: len(myDict)

number of key:value pairs in the dictionary element in myDict

boolean, is element a key in the dictionary for key in myDict:

iterates through the keys of a dictionary

Page 19: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Lots of Methods

myDict.items() – all the key/value pairs myDict.keys() – all the keys myDict.values() – all the values key in myDict

does the key exist in the dictionary myDict.clear() – empty the dictionary myDict.update(yourDict) – for each key in yourDict, updates myDict with that key/value pair

Page 20: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Dictionaries are Iterable

for key in myDict:

print(key) prints all the keys

for key,value in myDict.items():

print(key,value) prints all the key/value pairs

for value in myDict.values():

print(value) prints all the values

Page 21: Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Doing something with this

Write a function called letterCount that: takes in a string as a parameter prints a table of the letters of the alphabet (in

alphabetical order ) together with the number of times each letter occurs.

Case should be ignored.