Skip to content

Commit

Permalink
Updating notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney committed Mar 13, 2024
1 parent 130a4e5 commit 286748c
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 63 deletions.
150 changes: 87 additions & 63 deletions 01_variables.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Welcome\n",
"\n",
"This is the Jupyter notebook for Chapter 1 of [*Elements of Data Science*](https://greenteapress.com/wp/elements-of-data-science), by Allen B. Downey.\n",
"\n",
"\n",
"% TODO: Update these links\n",
"\n",
"If you are not familiar with Jupyter notebooks,\n",
"[click here for a short introduction](https://colab.research.google.com/github/AllenDowney/ElementsOfDataScience/blob/master/jupyter_intro.ipynb).\n",
"\n",
"Then, if you are not already running this notebook on Colab, [click here to run this notebook on Colab](https://colab.research.google.com/github/AllenDowney/ElementsOfDataScience/blob/master/01_variables.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -14,15 +31,14 @@
},
"source": [
"[Run this notebook on Colab](https://colab.research.google.com/github/AllenDowney/ElementsOfDataScience/blob/master/01_variables.ipynb) or \n",
"[Run this notebook on Sagemaker Studio Lab](https://studiolab.sagemaker.aws/import/github/AllenDowney/ElementsOfDataScience/blob/master/01_variables.ipynb) or \n",
"[Download this notebook](https://github.com/AllenDowney/ElementsOfDataScience/raw/master/01_variables.ipynb)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Data science is the use of data to answers questions and guide decision making.\n",
"Data science is the use of data to answer questions and guide decision making.\n",
"For example, a topic of current debate is whether we should raise the minimum wage in the United States.\n",
"Some economists think that raising the minimum wage would raise families out of poverty; others think it would cause more unemployment.\n",
"But economic theory can only take us so far.\n",
Expand All @@ -49,13 +65,11 @@
"source": [
"The goal of this book is to give you the tools you need to execute a data science project from beginning to end, including these steps:\n",
"\n",
"* Choosing questions, data, and methods that go together.\n",
"\n",
"* Finding data or collecting it yourself.\n",
"* Finding questions, data, and methods that go together.\n",
"\n",
"* Cleaning and validating data.\n",
"\n",
"* Exploring datasets, visualizing distributions and relationships between variables.\n",
"* Exploring datasets by visualizing distributions and relationships between variables.\n",
"\n",
"* Modeling data and generating predictions.\n",
"\n",
Expand Down Expand Up @@ -83,13 +97,11 @@
"source": [
"The topics in this chapter are:\n",
"\n",
"* Using Jupyter to write and run Python code.\n",
"\n",
"* Basic programming features in Python: variables and values.\n",
"\n",
"* Translating formulas from math notation to Python.\n",
"\n",
"Along the way, we'll review a couple of math topics I assume you have seen before, logarithms and algebra."
"You don't need a lot of math to do data science, but and the end of this chapter I'll review one topic that comes up a lot: logarithms."
]
},
{
Expand All @@ -111,17 +123,17 @@
"tags": []
},
"source": [
"If you are running this notebook on Colab, you should see buttons in the top left that say \"+ Code\" and \"+ Text\". The first one adds code cell and the second adds a text cell.\n",
"If you are running this notebook on Colab, you should see buttons in the top left that say \"+ Code\" and \"+ Text\". The first one adds a code cell and the second adds a text cell.\n",
"\n",
"If you want to try them out, select this cell by clicking on it, then press the \"+ Text\" button. A new cell should appear below this one.\n",
"\n",
"Add some text to the cell. You can use the buttons to format it, or you can mark up the text using [Markdown](https://www.markdownguide.org/basic-syntax/). When you are done, hold down Shift and press Enter, which will format the text you just typed and then move to the next cell.\n",
"Add some text to the cell. You can use the buttons to format it, or you can mark up the text using [Markdown](https://www.markdownguide.org/basic-syntax/). When you are done, hold down \"Shift\" and press \"Enter\", which will format the text you just typed and then move to the next cell.\n",
"\n",
"If you select a Code cell, you should see a button on the left with a triangle inside a circle, which is the icon for \"Play\". If you press this button, Jupyter runs the code in the cell and displays the results.\n",
"\n",
"When you run code in a notebook for the first time, you might get a message warning you about the things a notebook can do. If you are running a notebook from a source you trust, which I hope includes me, you can press \"Run Anyway\".\n",
"When you run code in a notebook for the first time, you might get a message warning you about the things a notebook can do. If you are running a notebook from a source you trust -- which I hope includes me -- you can press \"Run Anyway\".\n",
"\n",
"Instead of clicking the \"Play\" button, you can also run the code in a cell by holding down Shift and pressing Enter."
"Instead of clicking the \"Play\" button, you can also run the code in a cell by holding down \"Shift\" and pressing \"Enter\"."
]
},
{
Expand All @@ -139,7 +151,7 @@
"* `float`, which represents numbers that have a fraction part, like `3.14159`.\n",
"\n",
"Most often, we use `int` to represent counts and `float` to represent measurements.\n",
"Here's an example of an `int` and a `float`:"
"Here's an example of an `int`:"
]
},
{
Expand All @@ -151,6 +163,13 @@
"3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you run a cell that contains a value like this, Jupyter displays the value. Here's an example of a `float`:"
]
},
{
"cell_type": "code",
"execution_count": 2,
Expand All @@ -167,17 +186,36 @@
" `float` is short for \"floating-point\", which is the name for the way these numbers are stored."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Floating-point numbers can also be written in scientific notation, like this:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"1.2345e3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `e` in `1.2345e3` stands for \"exponent\". This way of writing a number is equivalent to $1.2345 \\times 10^{3}$."
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"**Exercise:** Create a code cell below this one and type in the following number: `1.2345e3`\n",
"\n",
"Then run the cell. The output should be `1234.5`\n",
"\n",
"The `e` in `1.2345e3` stands for \"exponent\". This way of writing numbers is a version of scientific notation that means $1.2345 \\times 10^{3}$. If you are not familiar with scientific notation, you can read about it at <https://en.wikipedia.org/wiki/Scientific_notation>. "
"If you are not familiar with scientific notation, you can read about it at <https://en.wikipedia.org/wiki/Scientific_notation>. "
]
},
{
Expand Down Expand Up @@ -232,20 +270,19 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"2**3"
"2 ** 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unlike math notation, Python does not allow \"implicit multiplication\". For example, in math notation, if you write $3 (2 + 1)$, that's understood to be the same as $3 \\times (2+ 1)$.\n",
"Python does not allow that notation.\n",
"If you want to multiply, you have to use the `*` operator."
"Python does not allow that notation."
]
},
{
Expand All @@ -254,13 +291,28 @@
"tags": []
},
"source": [
"Try running this code to see what error you get.\n",
"NOTE: The following cell uses `%%expect`, which is a Jupyter \"magic command\" that means we expect the code in this cell to produce an error. For more on this topic, see the\n",
"[Jupyter notebook introduction](https://colab.research.google.com/github/AllenDowney/ThinkPython/blob/v3/chapters/jupyter_intro.ipynb)."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"%%expect TypeError\n",
"\n",
"```\n",
"3 (2 + 1)\n",
"```\n",
"3 (2 + 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this example, the error message is not very helpful, which is why I am warning you now. \n",
"\n",
"In this example, the error message is not very helpful, which is why I am warning you now. "
"If you want to multiply, you have to use the `*` operator."
]
},
{
Expand Down Expand Up @@ -375,16 +427,14 @@
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"If you run this code in the following cell, you should get an error:\n",
"%%expect ModuleNotFoundError\n",
"\n",
"```\n",
"import NumPy as np\n",
"```"
"import NumPy as np"
]
},
{
Expand Down Expand Up @@ -947,32 +997,6 @@
"# Solution goes here"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## The Colab mental model\n",
"\n",
"Congratulations on completing the first notebook!\n",
"\n",
"Now that you have worked with Colab, you might find it helpful to watch this video, where I explain a little more about how it works:"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from IPython.display import YouTubeVideo \n",
"\n",
"YouTubeVideo(\"eIY-PsYBrPs\")"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -1011,7 +1035,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
"version": "3.10.13"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 286748c

Please sign in to comment.