12
Lesson objectives 1. Describe the characteristics of the dictionary data structure in Python 2. Perform basic operations with dictionaries including creation, copying, updating, and traversing 3. Use dictionaries in functions 1

Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Lesson objectives

1. Describe the characteristics of the dictionary data structure in Python

2. Perform basic operations with dictionaries including creation, copying, updating, and traversing

3. Use dictionaries in functions

1

Page 2: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

The dictionary data structure

• In Python, a dictionary is mapping between a set of indices (keys) and a set of values• The items in a dictionary are key-value pairs

2

Page 3: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

The dictionary data structure

• Keys can be any Python data type• Because keys are used for indexing, they should be immutable

• Values can be any Python data type• Values can be mutable or immutable

3

Page 4: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Creating a dictionary

eng2sp = dict()

print eng2sp

eng2sp['one'] = 'uno'

print eng2sp

eng2sp['two'] = 'dos'

print eng2sp

4

Page 5: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Creating a dictionary

eng2sp = {'one': 'uno', 'two': 'dos',

'three': 'tres'}

print eng2sp

• In general, the order of items in a dictionary is unpredictable

•Dictionaries are indexed by keys, not integers

5

Page 6: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Dictionary indexing

print eng2sp['three']

print eng2sp['five']

* If the index is not a key in the dictionary, Python raises an error.

6

Page 7: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Dictionary indexing

if 'five' in eng2sp:

print eng2sp['five']

print eng2sp.get('five')

7

Page 8: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

The in operator

•Note that the in operator works differently for dictionaries than for other sequences• For offset indexed sequences (strings, lists, tuples), x in y checks to see

whether x is an item in the sequence

• For dictionaries, x in y checks to see whether x is a key in the dictionary

8

Page 9: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Keys and values

• The keys method returns a list of the keys in a dictionary

print eng2sp.keys()

• The values method returns a list of the values

print eng2sp.values()

9

Page 10: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Keys and values

• The items method returns a list of tuple pairs of the key-value pairs in a dictionary

print eng2sp.items()

10

Page 11: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Tuple data structure

• tuples are constructed by the comma operator (not within square brackets), with or without enclosing parentheses.

t = 4,5,6

print t

• A single element tuple must have a trailing comma, such as (d,).

Page 12: Lesson objectives - Kent State Universitymallouzi/Algorithms and Programming I fall 2014... · Lesson objectives 1. Describe the characteristics of the dictionary data structure in

Tuple data structure

• tuples are very similar to lists, but they are immutable: items in a tuple cannot be changed.

• tuple elements are accessed by index, or by simultaneous assignment:

print t[0]

a,b,c = t # unpacking a tuple