Unit D Reading assignment

Examples

comprehension.png

Chapter 7 Tuples and Lists

Tuples

Read all sections on tuples.

Lists

We have already read all sections on lists.

Tuples Versus Lists

Read.

There are No Tuples Comprehensons

Read.

Coming up

Things to Do

skip; we will have our own exercises and assignments.

Chapter 8 Dictionaries and Sets

Dictionaries

Keys in a dictionary and items in a set must be hashable.
Hashable is a little more restrictive than just immutable.
To be hashable the object must meet these two requirements:
1) the object must be immutable
2) the object can not contain a reference to an object that is not hashable.
Some examples:
int float str are immutable and contain no references, so they are hashable.
list set dictionary are mutable because they can add a new item, so they are not hashable.
tuple is sometimes hashable, but if it contains a referece to an object that is not hashable, it is not hashable.
Examples of tuples:
1) A tuple containing three items that refer to an int, a float, and a str, is hashable.
2) A tuple containing a reference to a list is not hashable.
3) tuple1 contains a reference to tuple2, and tuple2 contains a reference to tuple3 et cetera, all the way down to tuple27. If tuple27 contains a reference to a list it is not hashable. Thus tuple26 is not hashable, and then tuple25 is not hashable, all the way back to tuple1 are all not hashable.

Create with {}

Create with dict()

Convert with dict()

Scan. The example of two-character strings is a little suprising. A str object object is usually processed as a whole object unless square brackets are used to specify part of the str object.

Add or Change an Item by [ key ]

Get an Item by [key] or with get()

Get All Keys with keys()

Get All Values with values()

Get All Key-Value Pairs with items()

Get Length with len()

Combine Dictionaries with {**a, **b}

Combine Dictionaries with update()

Delete an Item by Key with del

del is an operator, not a function or a method.

Get an Item by Key and Delete It with pop()

Delete All Items with clear()

Test for a Key with in

Assign with =

Copy with copy()

Copy Everything with deepcopy()

Compare Dictionaries

Iterate with for and in

Dictionary Comprehensions

Sets

Create with set()

Convert with set()

Get Length with len()

Add an Item with add()

Delete an Item with remove()

Iterate with for and in

Test for a Value with in

Combinations and Operators

Set Comprehensions

Create an Immutable Set with Frozenset()

Data Structures So Far

Make Bigger Data Structures

Coming up

Things to Do

omit

Chapter 10 Oh Oh: Objects and Classes

Named Tuples

Read only this section.
Using named tuples gives you some benefits that may not be immediately obvious. They're well suited to working with well defined records from a file or database.