Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "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": 1,
- "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": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "1"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "Dict[\"key1\"]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "6"
- ]
- },
- "execution_count": 4,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "Dict[(0,1)]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "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": 5,
- "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": 6,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1982'"
- ]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Get value by keys\n",
- "\n",
- "release_year_dict['Thriller'] "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "'1992'"
- ]
- },
- "execution_count": 7,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Get value by key\n",
- "\n",
- "release_year_dict['The Bodyguard'] "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "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": 8,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Get all the keys in dictionary\n",
- "\n",
- "release_year_dict.keys() "
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])"
- ]
- },
- "execution_count": 9,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "release_year_dict.values()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "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": 10,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#Add an entry to the dictionary\n",
- "release_year_dict['Graduation']='2007'\n",
- "release_year_dict"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "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": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "# Delete entries by key\n",
- "del(release_year_dict['Graduation'])\n",
- "release_year_dict"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "True"
- ]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "#Verify if key is in disctionary\n",
- "'Rumours' in release_year_dict"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}"
- ]
- },
- "execution_count": 13,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "soundtrack_dic = {\"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\n",
- "soundtrack_dic"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "dict_keys(['The Bodyguard', 'Saturday Night Fever'])"
- ]
- },
- "execution_count": 14,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "soundtrack_dic.keys()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "dict_values(['1992', '1977'])"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "soundtrack_dic.values()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "{'Back in Black': 50, 'The Bodyguard': 50, 'Thriller': 65}"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "album_sales_dict={\"Back in Black\":50,\"The Bodyguard\":50,\"Thriller\":65}\n",
- "album_sales_dict"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python",
- "language": "python",
- "name": "conda-env-python-py"
- },
- "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.7"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement