Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imbingz/ea827e6ca60a3f038d92964bfd509440 to your computer and use it in GitHub Desktop.
Save imbingz/ea827e6ca60a3f038d92964bfd509440 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will need the dictionary <code>D</code>:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 0, 'b': 1, 'c': 2}\n"
]
}
],
"source": [
"D={'a':0,'b':1,'c':2}\n",
"print(D)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 align=center>Add an Element to the set</h3> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the value for the key <code> 'a' </code>"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D={'a':0,'b':1,'c':2}\n",
"D['a']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3 align=center>Keys</h3> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the keys of the dictionary <code>D</code>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"D['b']\n",
"\n",
"D.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<small>Copyright &copy; 2018 IBM Cognitive Class. This notebook and its source code are released under the terms of the [MIT License](https://cognitiveclass.ai/mit-license/).</small>"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['a', 'b', 'c'])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D={'a':0,'b':1,'c':2}\n",
"D.keys()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([0, 1, 2])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"D.values()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'a': 0, 'b': 1, 'c': 2, 'd': 'd'}\n"
]
}
],
"source": [
"# Add a new key \"d\" and value 3 to D\n",
"D={'a':0,'b':1,'c':2}\n",
"D[\"d\"]= 'd'\n",
"print(D)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a': 0, 'b': 1, 'c': 2}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remove key \"d\", value d from D\n",
"D = {'a': 0, 'b': 1, 'c': 2, 'd': 'd'}\n",
"del D[\"d\"]\n",
"D"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# check length of D\n",
"len(D)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Test the objects in D\n",
"\n",
"'a' in D"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"s\" in D"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Get the value of key \"a\"\n",
"\n",
"D[\"a\"]\n"
]
},
{
"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