Structured Types Flashcards
Tuples, Lists and Dictionaries
1
Q
Tubles
A
- an ordered sequence of elements
- immutable
- represented with parentheses
2
Q
Tubles - more
A
- Swap variables (x, y) = (y, x)
- Return more than one value from a function
3
Q
Lists
A
- Ordered sequence of information
- Denoted by square brackets
- Contains a list of elements
- Is mutable
4
Q
Functions as Objects
A
first class objects:
- have types
- can be elements of data structures like lists
- can appear in expressions
- as part of an assignment statement
- as an argument to a function!
- useful to use functions as arguments when coupled with lists -> higher order programming (hop)
5
Q
Generalization of HOPs
A
- Python provides a general purpose HOP -> map
- simple form is a unary
- general from is an n-ary
6
Q
Dictionary
A
Store pairs of data:
- key
- value
7
Q
Dictionary lookup
A
- similar to indexing into a list
- looks up the key
- returns the value associated with the key
- if key not found -> error
8
Q
Dictionary - values
A
- immutable and mutable
- can be duplicates
- can be lists, tuples, dictionaries
9
Q
Dictionary - keys
A
- Unique
- immutable
- careful with using floats!
- no order to keys or values!
10
Q
list
vs
dictionary
A
lists: - ordered, look-up by int. index, indices have an order, index is an integer dict: - matches 'keys' to 'values' - look-up one item by another item - no order is guaranteed - key can be any immutable type
11
Q
global variables
A
can be dangerous
- breaks the scope
- allows for side effects
BUT can be convenient when want to keep track of information inside a function