diff --git a/.pytest_cache/README.md b/.pytest_cache/README.md new file mode 100644 index 000000000..bb78ba07e --- /dev/null +++ b/.pytest_cache/README.md @@ -0,0 +1,8 @@ +# pytest cache directory # + +This directory contains data from the pytest's cache plugin, +which provides the `--lf` and `--ff` options, as well as the `cache` fixture. + +**Do not** commit this to version control. + +See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information. diff --git a/.pytest_cache/v/cache/nodeids b/.pytest_cache/v/cache/nodeids new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/.pytest_cache/v/cache/nodeids @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/index.ipynb b/index.ipynb index 86372606a..0fcb4d148 100644 --- a/index.ipynb +++ b/index.ipynb @@ -1 +1,610 @@ -{"cells": [{"cell_type": "markdown", "metadata": {"collapsed": true}, "source": ["# Going further with dictionaries "]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Introduction"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now that we know a little bit about lists and dictionaries, we can take data in a different digital format, and move it into code. In this lesson, we'll see that in just a few lines of code, we can use Python to work with data in other formats. Then we'll learn a couple of other methods for working with dictionaries: `keys()`, `values()`, and the `dict()` constructor."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Objectives"]}, {"cell_type": "markdown", "metadata": {}, "source": ["* Understand how the list data structure aligns with data in non-programming contexts\n", "* Understand how the dictionary data structure aligns with data in non-programming contexts\n", "* See some of the steps involved in getting data from a different format and into code"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### From Google Sheet to Local File"]}, {"cell_type": "markdown", "metadata": {}, "source": ["For example, here is [our list of travel cities and countries](https://docs.google.com/spreadsheets/d/1kv8z2lZ3NLWbJcdE6ysd40BZLresdl5W6mWrtIunMn4/edit?usp=sharing) in the form of a google sheet. If you click on the link, you will see our spreadsheet."]}, {"cell_type": "markdown", "metadata": {}, "source": [""]}, {"cell_type": "markdown", "metadata": {}, "source": ["You'll notice two additional columns: Population and Area. The Population column contains the population of the city in units of 1000 people. The Area column contains the area of the city in units of $km^2$. Now if we download this spreadsheet in the form of an .xlsx file we can start to work with it."]}, {"cell_type": "markdown", "metadata": {}, "source": [""]}, {"cell_type": "markdown", "metadata": {}, "source": ["We've already placed that file into this lesson, and you can see it [here](https://github.com/learn-co-curriculum/excel-to-python), where the contents of this lesson are also located."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### From Local File to Python "]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now that we have this file in the folder we are working with, we can get this data into Python code in a few lines."]}, {"cell_type": "markdown", "metadata": {}, "source": ["> **Deep breath, soft eyes**: In the gray box below are four lines of code. They go over some topics we did not cover yet. So don't worry, if you don't follow everything right now. By the end of this unit, you will understand all of the code. For right now, it's fine to just have a slight sense of what's going on. "]}, {"cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'City': 'Buenos Aires',\n", " 'Country': 'Argentina',\n", " 'Population': 2891,\n", " 'Area': 203}"]}, "execution_count": 1, "metadata": {}, "output_type": "execute_result"}], "source": ["import pandas\n", "travel_df = pandas.read_excel('./cities.xlsx')\n", "cities = travel_df.to_dict('records')\n", "cities[0]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["> Press shift+enter to run the code."]}, {"cell_type": "markdown", "metadata": {}, "source": ["The code above relies on using an outside library called `pandas`, as it's good at reading excel files. A library is just a set of reusable functions. The `pandas` library is available for free online. We tell our current Jupyter notebook that we are about to use it with the line `import pandas`. \n", "\n", "And that gives us an object, like a dictionary, which has a method in it called `read_excel`. Similar to how we can call `{'foo': 'bar'}.keys()`. That's the benefit of a library, we can get methods that do not come out of the box with Python. So we use the `read_excel` data to read our excel file, by providing the name of the file, `cities.xlsx`, and the preceding `./` just indicates that the file is found in the current folder. Finally with the line `travel_df.to_dict('records')` we return a list of our dictionaries representing our data. You can see that when we access the first element of this list, it returns our first dictionary. "]}, {"cell_type": "markdown", "metadata": {}, "source": ["Here is the code again, with some comments, if you are interested."]}, {"cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": ["# Here we use a library, which is some code not part of standard Python, to make this process easier \n", "import pandas\n", "# If we use the `import pandas` we have access to the pandas library \n", "travel_df = pandas.read_excel('./cities.xlsx')\n", "# We call the pandas.read_excel method and pass through the string './cities.xlsx' as the file is called cities.xlsx. By saying './' we are saying \n", "# go to the current folder, excel-to-python, and find the 'cities.xlsx' file there\n", "cities = travel_df.to_dict('records')"]}, {"cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [{"data": {"text/plain": ["[{'City': 'Buenos Aires',\n", " 'Country': 'Argentina',\n", " 'Population': 2891,\n", " 'Area': 203},\n", " {'City': 'Toronto', 'Country': 'Canada', 'Population': 2732, 'Area': 630},\n", " {'City': 'Marakesh', 'Country': 'Morocco', 'Population': 929, 'Area': 230},\n", " {'City': 'Albuquerque', 'Country': 'USA', 'Population': 559, 'Area': 491},\n", " {'City': 'Los Cabos', 'Country': 'Mexico', 'Population': 288, 'Area': 3751},\n", " {'City': 'Greenville', 'Country': 'USA', 'Population': 93, 'Area': 68},\n", " {'City': 'Archipelago Sea',\n", " 'Country': 'Finland',\n", " 'Population': 60,\n", " 'Area': 2000},\n", " {'City': 'Pyeongchang',\n", " 'Country': 'South Korea',\n", " 'Population': 44,\n", " 'Area': 1464},\n", " {'City': 'Walla Walla Valley',\n", " 'Country': 'USA',\n", " 'Population': 33,\n", " 'Area': 35},\n", " {'City': 'Salina Island', 'Country': 'Italy', 'Population': 3, 'Area': 26},\n", " {'City': 'Solta', 'Country': 'Croatia', 'Population': 2, 'Area': 59},\n", " {'City': 'Iguazu Falls',\n", " 'Country': 'Argentina',\n", " 'Population': 0,\n", " 'Area': 2396}]"]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["cities"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Look at that. Our variable `cities` is full of cities from our spreadsheet."]}, {"cell_type": "markdown", "metadata": {}, "source": [""]}, {"cell_type": "markdown", "metadata": {}, "source": ["And we got there in four lines of code."]}, {"cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": ["import pandas\n", "file_name = './cities.xlsx'\n", "travel_df = pandas.read_excel(file_name)\n", "cities = travel_df.to_dict('records')"]}, {"cell_type": "markdown", "metadata": {}, "source": ["And now that we have data, we can operate on it just like a normal list of dictionaries."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Working with the keys and values functions"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Now that we have the data in Python, we can use a couple of other of functions to quickly explore our data. For example, let's say that we want to remind ourselves of all of the attributes associated with a single city. If we just look at the first element we see both the keys and values."]}, {"cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'City': 'Buenos Aires',\n", " 'Country': 'Argentina',\n", " 'Population': 2891,\n", " 'Area': 203}"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["cities[0]"]}, {"cell_type": "markdown", "metadata": {}, "source": ["But, we really only need to see the keys."]}, {"cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [{"data": {"text/plain": ["dict_keys(['City', 'Country', 'Population', 'Area'])"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["cities[0].keys()"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Note that the `keys()` function returns a `dict_keys` object. It's a little tricky to work with that type of object, so let's coerce it into a list."]}, {"cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [{"data": {"text/plain": ["['City', 'Country', 'Population', 'Area']"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["list(cities[0].keys())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Much better."]}, {"cell_type": "markdown", "metadata": {}, "source": ["If we would like to also see our values of a dictionary in a list, we can do something similar with the `.values()` function for a dictionary."]}, {"cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [{"data": {"text/plain": ["dict_values(['Buenos Aires', 'Argentina', 2891, 203])"]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["cities[0].values()"]}, {"cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [{"data": {"text/plain": ["['Buenos Aires', 'Argentina', 2891, 203]"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["list(cities[0].values())"]}, {"cell_type": "markdown", "metadata": {}, "source": ["Once again, we call the method, and then coerce it into a list, by using the `list` function."]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Creating Dictionaries"]}, {"cell_type": "markdown", "metadata": {}, "source": ["So far, we have seen one way of creating dictionaries: "]}, {"cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": ["philadelphia = {'City': 'Philadelphia'}"]}, {"cell_type": "markdown", "metadata": {}, "source": ["But there is another way!"]}, {"cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'city': 'Pittsburgh'}"]}, "execution_count": 11, "metadata": {}, "output_type": "execute_result"}], "source": ["pittsburgh = dict(city='Pittsburgh')\n", "pittsburgh"]}, {"cell_type": "markdown", "metadata": {}, "source": ["As you can see, by using the keyword `dict`, we can pass through the name of the key followed by the equal sign. Notice that the key name is provided as a string, but Python still knows how to handle it. \n", "\n", "Let's do one more."]}, {"cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [{"data": {"text/plain": ["{'city': 'Las Vegas', 'state': 'Nevada'}"]}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["dict(city=\"Las Vegas\", state=\"Nevada\")"]}, {"cell_type": "markdown", "metadata": {}, "source": ["### Summary"]}, {"cell_type": "markdown", "metadata": {"collapsed": true}, "source": ["In this section we saw how to get our data from the outside world and into Python. As we become better at Python, the usefulness of taking data and operating on it in code rather than a spreadsheet will become more apparent. We can even begin to try out our new skills with Python and our own data files outside of this lesson.\n", "\n", "Next, we saw a few more methods for operating on dictionaries in Python. Namely, we saw how to use the `keys()` function to retrieve the keys of a dictionary, and the `values()` function to retrieve the dictionary's values. Finally, we saw how to use the `dict()` constructor to create a new dictionary."]}], "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.6"}}, "nbformat": 4, "nbformat_minor": 2} \ No newline at end of file +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "# Going further with dictionaries " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Introduction" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we know a little bit about lists and dictionaries, we can take data in a different digital format, and move it into code. In this lesson, we'll see that in just a few lines of code, we can use Python to work with data in other formats. Then we'll learn a couple of other methods for working with dictionaries: `keys()`, `values()`, and the `dict()` constructor." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Objectives" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "* Understand how the list data structure aligns with data in non-programming contexts\n", + "* Understand how the dictionary data structure aligns with data in non-programming contexts\n", + "* See some of the steps involved in getting data from a different format and into code" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### From Google Sheet to Local File" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "For example, here is [our list of travel cities and countries](https://docs.google.com/spreadsheets/d/1kv8z2lZ3NLWbJcdE6ysd40BZLresdl5W6mWrtIunMn4/edit?usp=sharing) in the form of a google sheet. If you click on the link, you will see our spreadsheet." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "You'll notice two additional columns: Population and Area. The Population column contains the population of the city in units of 1000 people. The Area column contains the area of the city in units of $km^2$. Now if we download this spreadsheet in the form of an .xlsx file we can start to work with it." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We've already placed that file into this lesson, and you can see it [here](https://github.com/learn-co-curriculum/excel-to-python), where the contents of this lesson are also located." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### From Local File to Python " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we have this file in the folder we are working with, we can get this data into Python code in a few lines." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "> **Deep breath, soft eyes**: In the gray box below are four lines of code. They go over some topics we did not cover yet. So don't worry, if you don't follow everything right now. By the end of this unit, you will understand all of the code. For right now, it's fine to just have a slight sense of what's going on. " + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'City': 'Buenos Aires',\n", + " 'Country': 'Argentina',\n", + " 'Population': 2891,\n", + " 'Area': 203}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas\n", + "travel_df = pandas.read_excel('./cities.xlsx')\n", + "cities = travel_df.to_dict('records')\n", + "cities[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
CityCountryPopulationArea
0Buenos AiresArgentina2891203
1TorontoCanada2732630
2MarakeshMorocco929230
3AlbuquerqueUSA559491
4Los CabosMexico2883751
\n", + "
" + ], + "text/plain": [ + " City Country Population Area\n", + "0 Buenos Aires Argentina 2891 203\n", + "1 Toronto Canada 2732 630\n", + "2 Marakesh Morocco 929 230\n", + "3 Albuquerque USA 559 491\n", + "4 Los Cabos Mexico 288 3751" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "travel_df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The code above relies on using an outside library called `pandas`, as it's good at reading excel files. A library is just a set of reusable functions. The `pandas` library is available for free online. We tell our current Jupyter notebook that we are about to use it with the line `import pandas`. \n", + "\n", + "And that gives us an object, like a dictionary, which has a method in it called `read_excel`. Similar to how we can call `{'foo': 'bar'}.keys()`. That's the benefit of a library, we can get methods that do not come out of the box with Python. So we use the `read_excel` data to read our excel file, by providing the name of the file, `cities.xlsx`, and the preceding `./` just indicates that the file is found in the current folder. Finally with the line `travel_df.to_dict('records')` we return a list of our dictionaries representing our data. You can see that when we access the first element of this list, it returns our first dictionary. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here is the code again, with some comments, if you are interested." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "# Here we use a library, which is some code not part of standard Python, to make this process easier \n", + "import pandas\n", + "# If we use the `import pandas` we have access to the pandas library \n", + "travel_df = pandas.read_excel('./cities.xlsx')\n", + "# We call the pandas.read_excel method and pass through the string './cities.xlsx' as the file is called cities.xlsx. By saying './' we are saying \n", + "# go to the current folder, excel-to-python, and find the 'cities.xlsx' file there\n", + "cities = travel_df.to_dict('records')" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'City': 'Buenos Aires',\n", + " 'Country': 'Argentina',\n", + " 'Population': 2891,\n", + " 'Area': 203},\n", + " {'City': 'Toronto', 'Country': 'Canada', 'Population': 2732, 'Area': 630},\n", + " {'City': 'Marakesh', 'Country': 'Morocco', 'Population': 929, 'Area': 230},\n", + " {'City': 'Albuquerque', 'Country': 'USA', 'Population': 559, 'Area': 491},\n", + " {'City': 'Los Cabos', 'Country': 'Mexico', 'Population': 288, 'Area': 3751},\n", + " {'City': 'Greenville', 'Country': 'USA', 'Population': 93, 'Area': 68},\n", + " {'City': 'Archipelago Sea',\n", + " 'Country': 'Finland',\n", + " 'Population': 60,\n", + " 'Area': 2000},\n", + " {'City': 'Pyeongchang',\n", + " 'Country': 'South Korea',\n", + " 'Population': 44,\n", + " 'Area': 1464},\n", + " {'City': 'Walla Walla Valley',\n", + " 'Country': 'USA',\n", + " 'Population': 33,\n", + " 'Area': 35},\n", + " {'City': 'Salina Island', 'Country': 'Italy', 'Population': 3, 'Area': 26},\n", + " {'City': 'Solta', 'Country': 'Croatia', 'Population': 2, 'Area': 59},\n", + " {'City': 'Iguazu Falls',\n", + " 'Country': 'Argentina',\n", + " 'Population': 0,\n", + " 'Area': 2396}]" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Look at that. Our variable `cities` is full of cities from our spreadsheet." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And we got there in four lines of code." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas\n", + "file_name = './cities.xlsx'\n", + "travel_df = pandas.read_excel(file_name)\n", + "cities = travel_df.to_dict('records')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And now that we have data, we can operate on it just like a normal list of dictionaries." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Working with the keys and values functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now that we have the data in Python, we can use a couple of other of functions to quickly explore our data. For example, let's say that we want to remind ourselves of all of the attributes associated with a single city. If we just look at the first element we see both the keys and values." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'City': 'Buenos Aires',\n", + " 'Country': 'Argentina',\n", + " 'Population': 2891,\n", + " 'Area': 203}" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities[0]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "But, we really only need to see the keys." + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_keys(['City', 'Country', 'Population', 'Area'])" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities[0].keys()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that the `keys()` function returns a `dict_keys` object. It's a little tricky to work with that type of object, so let's coerce it into a list." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['City', 'Country', 'Population', 'Area']" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(cities[0].keys())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Much better." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If we would like to also see our values of a dictionary in a list, we can do something similar with the `.values()` function for a dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict_values(['Buenos Aires', 'Argentina', 2891, 203])" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cities[0].values()" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['Buenos Aires', 'Argentina', 2891, 203]" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(cities[0].values())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once again, we call the method, and then coerce it into a list, by using the `list` function." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating Dictionaries" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "So far, we have seen one way of creating dictionaries: " + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [], + "source": [ + "philadelphia = {'City': 'Philadelphia'}" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "But there is another way!" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'city': 'Pittsburgh'}" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "pittsburgh = dict(city='Pittsburgh')\n", + "pittsburgh" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As you can see, by using the keyword `dict`, we can pass through the name of the key followed by the equal sign. Notice that the key name is provided as a string, but Python still knows how to handle it. \n", + "\n", + "Let's do one more." + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'city': 'Las Vegas', 'state': 'Nevada'}" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dict(city=\"Las Vegas\", state=\"Nevada\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Summary" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "In this section we saw how to get our data from the outside world and into Python. As we become better at Python, the usefulness of taking data and operating on it in code rather than a spreadsheet will become more apparent. We can even begin to try out our new skills with Python and our own data files outside of this lesson.\n", + "\n", + "Next, we saw a few more methods for operating on dictionaries in Python. Namely, we saw how to use the `keys()` function to retrieve the keys of a dictionary, and the `values()` function to retrieve the dictionary's values. Finally, we saw how to use the `dict()` constructor to create a new dictionary." + ] + } + ], + "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.7.0" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}