W8_dictionaries

# W8_dictionaries - Two new data types this week tuples like...

This preview shows pages 1–6. Sign up to view the full content.

Two new data types this week! tuples : like lists , but safer what's unsafe about lists? - anybody can change them, as we saw earlier tuples are immutable (just like strings) 1 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Tuples t = (1, 2) r = (1,) s = ('a', 'b', 'c') s[0]? s[3]? len(s)? s[0] = 'A'? 2 2
Tuples can be used for multiple return values def max_min(L): '''Return the maximum and the minimum value of list L as a tuple, in that order.''' return max(L), min(L) OR: return (max(L), min(L)) # calling the function and naming the return values: (biggest, smallest) = max_min(L) OR: biggest, smallest = max_min(L) 3 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Dictionaries CO2 Emissions by year in North America (thousands of tons of carbon) 1799 1 1800 70 1801 74 1802 79 1902 21563 2002 1733297 How can we represent this in a program? 4 4
Dictionaries Carbon emissions - cont'd One obvious choice: lists year = [1799, 1800, 1801, 1802, 1902, 2002] CO2 = [1, 70, 74, 79, 215630, 1733297] The CO2 list at position i represents the emission for the year at position i in the year list 5 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/26/2011 for the course CS a48 taught by Professor Michael during the Fall '10 term at University of Toronto.

### Page1 / 10

W8_dictionaries - Two new data types this week tuples like...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online