Objects, Data Structures, & Data Types Flashcards
Dictionaries
(def., & 6 properties)
- 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
list (def. & 3 properties)
- ordered sequence holding a variety of object types
- notation: [], nested [[]]
- supports indexing and slicing, and are mutable
- allows for duplicate values
sets
(def., how created, how to add objects)
- 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( )
tuple
(def./how to create, prop., when to use)
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
when to use a dictionary ( 1 )
when to use a list ( 2 )?
- 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.
- use list when you want to idex, slice, sort
index a string, slice a string, index nested string
index: mystring[0] , mystring[7], mystring[-1]
slice: mystring[2:], mystring[3:7], mystring[:3]
nested string: mystring[0][1]
How to format floating point numbers within a string with the .format( ) method
result = 1000/777
print(“The result was { r : 1.3f}”.format(r = result))
String Concatenation & Repitition
(addition & multiplication)
first\_last = 'first name' + ' ' + 'last name' letter = 'z', z \* 10 returns 10 z's
Setting Data Types in Python
(strings, integers, floats, complex)
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)
unpacking for string, list, & dictionaries
(how to perform)
unpacking with star notation
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
Slice Notation
(syntax and what datasets they are used for)
used for list, strings, & tuples
list [start : stop : step]
What it a deque in Python?
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