Created
April 6, 2019 18:48
-
-
Save mde-2590/43d18db239632cd355374620b3ee122a to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'key1': 1,\n", | |
" 'key2': '2',\n", | |
" 'key3': [3, 3, 3],\n", | |
" 'key4': (4, 4, 4),\n", | |
" 'key5': 5,\n", | |
" (0, 1): 6}" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Create the dictionary\n", | |
"\n", | |
"Dict = {\"key1\": 1, \"key2\": \"2\", \"key3\": [3, 3, 3], \"key4\": (4, 4, 4), ('key5'): 5, (0, 1): 6}\n", | |
"Dict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Access to the value by the key\n", | |
"\n", | |
"Dict[\"key1\"]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"6" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Access to the value by the key\n", | |
"\n", | |
"Dict[(0, 1)]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Thriller': '1982',\n", | |
" 'Back in Black': '1980',\n", | |
" 'The Dark Side of the Moon': '1973',\n", | |
" 'The Bodyguard': '1992',\n", | |
" 'Bat Out of Hell': '1977',\n", | |
" 'Their Greatest Hits (1971-1975)': '1976',\n", | |
" 'Saturday Night Fever': '1977',\n", | |
" 'Rumours': '1977'}" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Create a sample dictionary\n", | |
"\n", | |
"release_year_dict = {\"Thriller\": \"1982\", \"Back in Black\": \"1980\", \\\n", | |
" \"The Dark Side of the Moon\": \"1973\", \"The Bodyguard\": \"1992\", \\\n", | |
" \"Bat Out of Hell\": \"1977\", \"Their Greatest Hits (1971-1975)\": \"1976\", \\\n", | |
" \"Saturday Night Fever\": \"1977\", \"Rumours\": \"1977\"}\n", | |
"release_year_dict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'1982'" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Get value by keys\n", | |
"\n", | |
"release_year_dict['Thriller']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['Thriller', 'Back in Black', 'The Dark Side of the Moon', 'The Bodyguard', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'Rumours'])" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Get all the keys in dictionary\n", | |
"\n", | |
"release_year_dict.keys() " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Get all the values in dictionary\n", | |
"\n", | |
"release_year_dict.values() " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Thriller': '1982',\n", | |
" 'Back in Black': '1980',\n", | |
" 'The Dark Side of the Moon': '1973',\n", | |
" 'The Bodyguard': '1992',\n", | |
" 'Bat Out of Hell': '1977',\n", | |
" 'Their Greatest Hits (1971-1975)': '1976',\n", | |
" 'Saturday Night Fever': '1977',\n", | |
" 'Rumours': '1977',\n", | |
" 'Graduation': '2007'}" | |
] | |
}, | |
"execution_count": 19, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Append value with key into dictionary\n", | |
"\n", | |
"release_year_dict['Graduation'] = '2007'\n", | |
"release_year_dict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 20, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Verify the key is in the dictionary\n", | |
"\n", | |
"'The Bodyguard' in release_year_dict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}" | |
] | |
}, | |
"execution_count": 22, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Question sample dictionary\n", | |
"\n", | |
"soundtrack_dic = {\"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\n", | |
"soundtrack_dic \n", | |
"#{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}\n", | |
"#a) In the dictionary soundtrack_dict what are the keys ?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['The Bodyguard', 'Saturday Night Fever'])" | |
] | |
}, | |
"execution_count": 23, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"soundtrack_dic.keys()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_values(['1992', '1977'])" | |
] | |
}, | |
"execution_count": 24, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#b) In the dictionary soundtrack_dict what are the values ?\n", | |
"\n", | |
"soundtrack_dic.values()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'Back in Black': '50', 'The Bodyguard': '50', 'Thriller': '65'}" | |
] | |
}, | |
"execution_count": 25, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#The Albums Back in Black, The Bodyguard and Thriller have the following music recording sales in millions 50, 50 and 65 respectively:\n", | |
"\n", | |
"#a) Create a dictionary album_sales_dict where the keys are the album name and the sales in millions are the values.\n", | |
"\n", | |
"album_sales_dict = {\"Back in Black\": \"50\", \"The Bodyguard\":\"50\", \"Thriller\":\"65\"}\n", | |
"album_sales_dict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'65'" | |
] | |
}, | |
"execution_count": 35, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#b) Use the dictionary to find the total sales of Thriller:\n", | |
"\n", | |
"album_sales_dict = {\"Back in Black\": \"50\", \"The Bodyguard\":\"50\", \"Thriller\":\"65\"}\n", | |
"album_sales_dict[\"Thriller\"]\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_keys(['Back in Black', 'The Bodyguard', 'Thriller'])" | |
] | |
}, | |
"execution_count": 36, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#c) Find the names of the albums from the dictionary using the method keys:\n", | |
"\n", | |
"album_sales_dict.keys()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dict_values(['50', '50', '65'])" | |
] | |
}, | |
"execution_count": 37, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"#d) Find the names of the recording sales from the dictionary using the method values:\n", | |
"\n", | |
"album_sales_dict.values()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.8" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment