Structured Types Flashcards

Tuples, Lists and Dictionaries

You may prefer our related Brainscape-certified flashcards:
1
Q

Tubles

A
  • an ordered sequence of elements
  • immutable
  • represented with parentheses
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Tubles - more

A
  • Swap variables (x, y) = (y, x)

- Return more than one value from a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Lists

A
  • Ordered sequence of information
  • Denoted by square brackets
  • Contains a list of elements
  • Is mutable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Generalization of HOPs

A
  • Python provides a general purpose HOP -> map
  • simple form is a unary
  • general from is an n-ary
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dictionary

A

Store pairs of data:

  • key
  • value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Dictionary - values

A
  • immutable and mutable
  • can be duplicates
  • can be lists, tuples, dictionaries
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Dictionary - keys

A
  • Unique
  • immutable
  • careful with using floats!
  • no order to keys or values!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly