dictionaries, sets, and comprehension intro2cs – week 4 part ii 1

16
Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

Upload: thomas-edwards

Post on 06-Jan-2018

221 views

Category:

Documents


1 download

DESCRIPTION

Other Complex Data Types in Python Dictionary Set 3 ages = {“dov”:12, “shir”:13} rooms = {212, 215, 367}

TRANSCRIPT

Page 1: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

1

Dictionaries, Sets, and Comprehension

Intro2CS – week 4 part II

Page 2: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

2

Complex Data Types you have seen

StringList

RangeTuple

Page 3: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

3

Other Complex Data Types in Python

• Dictionary

• Set

ages = {“dov”:12, “shir”:13}

rooms = {212, 215, 367}

Page 4: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

4

Dictionaries

Page 5: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

5

Phone book application

Accepts a sequence of commands of the following possible forms:– store name number: add this contact to the phone

book– phone name: print the number for this name– exit: exit

Page 6: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

6

Phone Book

Page 7: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

7

Sets

Page 8: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

8

Unique Elements

Page 9: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

9

MutabilityType Mutable?

String No

Tuple No

List Yes

Dictionary Yes

Set Yes

Frozenset No

Bool, Int, Float No

Rule: Elements in sets and keys in dictionaries must be immutable

Page 10: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

10

List Comprehension

Mathematical notation: S = { x2 | x = 1…N}

Python Notation:

Page 11: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

11

List comprehension implementation

l = [exp(i) for i in seq if cond(i)]

l = []for i in seq: if cond(i): l.append(exp(i))

Page 12: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

12

Factors

Perfect Numbers: [6, 28, 496, 8128]Prime numbers: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Page 13: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

13

Dictionary and Set comprehension

Page 14: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

14

Substitution CodesSubstitution codes:

abcdefghijklmnopqrstuvwxyzqwertyuiopasdfghjklzxcvbnm

hello itssg

Ceaser’s original code: shift by 3abcdefghijklmnopqrstuvwxyzxyzabcdefghijklmnopqrstuvw

Rot13: shift by 13

Page 15: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

15

Our implementation

• A code is a dictionary: letter letter

• ceaser_code(shift) code

• Encrypt(plaintext, code) ciphertext

• Decrypt(ciphertext, code) plaintext

Page 16: Dictionaries, Sets, and Comprehension Intro2CS – week 4 part II 1

16

Ceaser Code

jul qvq gur puvpxra pebff gur ebnq? ==> why did the chicken cross the road?