names = ['bill', 'sarah', 'cal', 'nina']
names[2]
'cal'
dog = {'name': 'Junior',
'age': 11,
4: ['kibble', 'treat']}
dog['name']
'Junior'
dog['age']
11
dog[4]
['kibble', 'treat']
dog[0]
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /tmp/ipykernel_23/2067700495.py in <module> ----> 1 dog[0] KeyError: 0
bears = {
'polar': {
'color': 'white',
'weight_range': [175, 700],
'hungry': True
},
'grizzly': {
'color': 'brown',
'weight_range': [130, 360],
'endangered': False
},
None: ['koala', 'panda']
}
bears['polar']['hungry']
True
bears[None][1]
'panda'
bears['weight_range']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) /tmp/ipykernel_23/995406303.py in <module> ----> 1 bears['weight_range'] KeyError: 'weight_range'
# {} creates a new empty dictionary
slang = {}
slang
{}
slang['btw'] = 'by the way'
slang
{'btw': 'by the way'}
slang['nw'] = 'no worries'
slang
{'btw': 'by the way', 'nw': 'no worries'}
slang
{'btw': 'by the way', 'nw': 'no worries'}
slang['btw'] = 'bring the windex'
slang
{'btw': 'bring the windex', 'nw': 'no worries'}
slang['nw'] += ", it's cool"
slang
{'btw': 'bring the windex', 'nw': "no worries, it's cool"}
two = 1
numbers = {'1': 2}
numbers['five'] = 5
numbers[two] = numbers['1']
numbers[2] = numbers[1] + numbers['five']
numbers['1']
2
numbers['five']
5
numbers[1]
2
numbers[2]
7
more_slang = {
'haha': 'that was not funny',
'smh': 'shake my head',
'lol': 'laugh out loud',
'GOAT': 'greatest of all time'
}
# Number of key-value pairs
len(more_slang)
4
# We will do this often!
list(more_slang.keys())
['haha', 'smh', 'lol', 'GOAT']
# We will not do this often
list(more_slang.values())
['that was not funny', 'shake my head', 'laugh out loud', 'greatest of all time']
# Checks if 'smh' is a key
'smh' in more_slang
True
# Checks if 'shake my head' is a key
# It is not – it is a value
'shake my head' in more_slang
False
more_slang
{'haha': 'that was not funny', 'smh': 'shake my head', 'lol': 'laugh out loud', 'GOAT': 'greatest of all time'}
for abb in more_slang.keys():
print(more_slang[abb])
that was not funny shake my head laugh out loud greatest of all time
for abb in more_slang.keys():
print(abb, more_slang[abb])
haha that was not funny smh shake my head lol laugh out loud GOAT greatest of all time
# Replaces all abbreviations in text
# that are defined in more_slang
# with their full forms
def replace_slang(text):
for abb in more_slang.keys():
if abb in text:
text = text.replace(abb, more_slang[abb])
return text
replace_slang('smh, I did not lol')
'shake my head, I did not laugh out loud'
replace_slang('serena is the GOAT')
'serena is the greatest of all time'
grandma_tree = {
"name": "Grandma",
"children": [
{"name": "Dad",
"children": [
{"name": "Me"},
{"name": "Brother"}
]
},
{"name": "my aunt",
"children": [
{"name": "Cousin 1"},
{"name": "Cousin 2",
"children": [
{"name": "Cousin 2 Jr."}
]
}
]
}
]
}
np.pi
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_23/4136878702.py in <module> ----> 1 np.pi NameError: name 'np' is not defined
import numpy as np
np.pi
3.141592653589793
pi
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_23/3440806613.py in <module> ----> 1 pi NameError: name 'pi' is not defined
np.array([1, 2, 3]) + np.array([4, 5, -3])
array([5, 7, 0])
Table.read_table('data/pups.csv')
--------------------------------------------------------------------------- NameError Traceback (most recent call last) /tmp/ipykernel_23/1703717253.py in <module> ----> 1 Table.read_table('data/pups.csv') NameError: name 'Table' is not defined
from datascience import *
pups = Table.read_table('data/pups.csv')
pups
name | age | breed |
---|---|---|
Junior Smith | 11 | cockapoo |
Rex Rogers | 7 | labradoodle |
Flash Heat | 3 | labrador |
Reese Bo | 4 | boston terrier |
Polo Cash | 2 | shih tzu |
Table.read_table('covid.csv')
state | cases |
---|---|
california | 100 |
texas | 120 |
import json
def read_json(path):
return json.load(open(path, 'r'))
read_json('data/family.json')
{'name': 'Grandma', 'children': [{'name': 'Dad', 'children': [{'name': 'Me'}, {'name': 'Brother'}]}, {'name': 'my aunt', 'children': [{'name': 'Cousin 1'}, {'name': 'Cousin 2', 'children': [{'name': 'Cousin 2 Jr.'}]}]}]}