Objects, Data Structures, & Data Types Flashcards

1
Q

Dictionaries

(def., & 6 properties)

A
  • unordered mappings for storing objects
  • stores with key: value pair, just call the key to find the value dict [‘key_name’]
  • notation {‘key’, ‘value’}
  • place list or dictionaries within dictionaries
    • d = { ‘key1’ : [a, b, c] }
    • d [‘key1’] [2] .upper( ) retuns C
  • dictionaries are mutable
  • you can retrieve items in a dictionary using the index notation ex. dict [0] will retrieve the first item
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

list (def. & 3 properties)

A
  • ordered sequence holding a variety of object types
  • notation: [], nested [[]]
  • supports indexing and slicing, and are mutable
  • allows for duplicate values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

sets

(def., how created, how to add objects)

A
  • unorder collections of unique elements
  • there can only be unique values (‘three’ can only appear once)
  • created with: set( )
  • add object to set: myset.add( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

tuple

(def./how to create, prop., when to use)

A

definition

  • order objects that are immutable
  • created with ( )

properties

  • can have different data types within it
  • indexed and count the same as strings and list

when to use

  • when to use: when passing objects between functions and you don’t want them to acadentially change
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

when to use a dictionary ( 1 )

when to use a list ( 2 )?

A
  1. use dictionary when you want to store information, but do not need to know its exact index location and you do not need to sort.
  2. use list when you want to idex, slice, sort
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

index a string, slice a string, index nested string

A

index: mystring[0] , mystring[7], mystring[-1]

slice: mystring[2:], mystring[3:7], mystring[:3]

nested string: mystring[0][1]

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

How to format floating point numbers within a string with the .format( ) method

A

result = 1000/777

print(“The result was { r : 1.3f}”.format(r = result))

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

String Concatenation & Repitition

(addition & multiplication)

A
first\_last = 'first name' + ' ' + 'last name' 
letter = 'z', z \* 10 returns 10 z's
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Setting Data Types in Python

(strings, integers, floats, complex)

A

string: quotation marks (“ “), or str( )

integer: x = 20 or x = int(20)

float: x = 20.5 or x = float(20.5)

complex: x = 20j or x = complex(20j)

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

unpacking for string, list, & dictionaries

(how to perform)

unpacking with star notation

A

def: extract each element from string, tuple, dictionary

  • string:
    • a, b, c = ‘123’
  • list:
    • a, b, c = [1, 2, 3]
  • dict:
    • dict = { ‘one’ : 1, ‘two’ : 2, ‘three’ : 3 }
    • keys:
      • a, b, c = dict
    • values:
      • a, b, c = dict.values( )
    • items:
      • a, b, c = dict.items( )

Unpacking with star notation:

the left side assignemnt must be in a tuple or list

*a, = 1, 2 returns: a = [1, 2]

a, *b = 1, 2, 3 returns: a = 1 , b = [2, 3]

*a, b = 1, 2, 3 returns: a = [1, 2] , b = 3

*a, b, c, d = 1, 2, 3 returns:

a = [], b = 1, c = 2, d = 3

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

Slice Notation

(syntax and what datasets they are used for)

A

used for list, strings, & tuples

list [start : stop : step]

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

What it a deque in Python?

A

a deque is a special data strucure in python for implementing stacks and queues

the deque is pronounced “deck” and is short for “double-ended queue”

produces O(1) performance in either direction, more efficient than list with O(n) performance

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