Commit f6d3aae4 authored by George Mount's avatar George Mount
Browse files

update files

q
parent 806a2d8c
...@@ -83,7 +83,8 @@ ...@@ -83,7 +83,8 @@
"source": [ "source": [
"my_week = (['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])\n", "my_week = (['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])\n",
"my_week.sort()\n", "my_week.sort()\n",
"print(my_week)" "\n",
"my_week"
] ]
}, },
{ {
...@@ -95,7 +96,7 @@ ...@@ -95,7 +96,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"my_week.clear()\n", "my_week.clear()\n",
"print(my_week)" "my_week"
] ]
}, },
{ {
...@@ -127,42 +128,6 @@ ...@@ -127,42 +128,6 @@
"print(my_list[1:5])\n" "print(my_list[1:5])\n"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DRILL\n",
"\n",
"Practice slicing lists below. \n",
"\n",
"These operations will work the same regardless of whether your list contains floats, strings, or other data types."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"this_list = [\"Slicing\",\"works\",\"on\",\"lists\",\"of\",\"strings\",\"identically\"]\n",
"\n",
"# Get the third to final elements\n",
"print(this_list[2:])\n",
"\n",
"\n",
"# Get everything up to the fourth element\n",
"print(this_list[:4])\n",
"\n",
"\n",
"# Get everything starting with the second-last element\n",
"print(this_list[-2:])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
......
...@@ -260,7 +260,7 @@ ...@@ -260,7 +260,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"The result of `abs(-100)` has been stored in a *variable*, which will make it much easier for us to refer to and use it. \n", "The result of `abs(-100)` has been stored in a *variable* or *object*, which will make it much easier for us to refer to and use it. \n",
"\n", "\n",
"### Printing variables\n", "### Printing variables\n",
"\n", "\n",
...@@ -278,6 +278,22 @@ ...@@ -278,6 +278,22 @@
"print(my_first_variable)" "print(my_first_variable)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's not necessary to include the function in Jupyter; simply run the object's name to print:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_first_variable"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
...@@ -291,7 +307,7 @@ ...@@ -291,7 +307,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"print(MY_FIRST_VARIABLE)" "MY_FIRST_VARIABLE"
] ]
}, },
{ {
...@@ -300,8 +316,6 @@ ...@@ -300,8 +316,6 @@
"source": [ "source": [
"## Python variable naming conventions\n", "## Python variable naming conventions\n",
"\n", "\n",
"> There are only two hard things in Computer Science: cache invalidation and naming things. --Phil Karlton\n",
"\n",
"\n", "\n",
"In theory, you can name your variables almost anything so long as they fit these rules:\n", "In theory, you can name your variables almost anything so long as they fit these rules:\n",
"\n", "\n",
...@@ -353,7 +367,7 @@ ...@@ -353,7 +367,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Some of these distinctions between data types are found in Excel, although you will find Python to be much more exacting about them!" "Some of these distinctions between data types are found in Excel, although you will find Python to be much more exacting about them:\n"
] ]
}, },
{ {
...@@ -373,29 +387,45 @@ ...@@ -373,29 +387,45 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"metadata": { "metadata": {},
"tags": []
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Assigning different variable types\n",
"\n",
"# Integer\n", "# Integer\n",
"my_int = 2\n", "my_int = 2\n",
"\n", "type(my_int)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Float(ing point decimal)\n", "# Float(ing point decimal)\n",
"my_float = 2.222\n", "my_float = 2.222\n",
"\n", "type(my_float)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# String\n", "# String\n",
"my_string = 'Hello'\n", "my_string = 'Hello'\n",
"\n", "type(my_string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Boolean\n", "# Boolean\n",
"my_boolean = True\n", "my_boolean = True\n",
"\n", "type(my_boolean)"
"print(type(my_int))\n",
"print(type(my_float))\n",
"print(type(my_string))\n",
"print(type(my_boolean))"
] ]
}, },
{ {
...@@ -437,7 +467,7 @@ ...@@ -437,7 +467,7 @@
"source": [ "source": [
"# Assign the product to a variable\n", "# Assign the product to a variable\n",
"my_nonsense = abs(my_int) * len(my_string)\n", "my_nonsense = abs(my_int) * len(my_string)\n",
"print(my_nonsense)" "my_nonsense"
] ]
}, },
{ {
...@@ -495,9 +525,17 @@ ...@@ -495,9 +525,17 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Make a list\n", "# Make a list\n",
"my_first_list = [1,2,3]\n", "my_first_list = [1, 2, 3]\n",
"print(my_first_list)\n", "my_first_list"
"print(type(my_first_list))" ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(my_first_list)"
] ]
}, },
{ {
...@@ -517,9 +555,8 @@ ...@@ -517,9 +555,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_other_list = [1,2,3,\"Boo!\"]\n", "my_other_list = [1, 2, 3, \"Boo!\"]\n",
"print(my_other_list)\n", "type(my_other_list)"
"print(type(my_other_list))"
] ]
}, },
{ {
...@@ -535,9 +572,8 @@ ...@@ -535,9 +572,8 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list_here = [1,2,3,[1,2,3,\"Boo!\"]]\n", "my_list_here = [1, 2, 3, [1, 2, 3, \"Boo!\"]]\n",
"print(my_list_here)\n", "type(my_list_here)"
"print(type(my_list_here))"
] ]
}, },
{ {
...@@ -591,11 +627,12 @@ ...@@ -591,11 +627,12 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [-1,4,3,2]\n", "my_list = [-1, 4, 3, 2]\n",
"\n", "\n",
"# This is a method\n", "# This is a method\n",
"my_list.sort()\n", "my_list.sort()\n",
"print(my_list)" "\n",
"my_list"
] ]
}, },
{ {
...@@ -612,7 +649,7 @@ ...@@ -612,7 +649,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list.sort(reverse=True)\n", "my_list.sort(reverse=True)\n",
"print(my_list)" "my_list"
] ]
}, },
{ {
...@@ -632,7 +669,7 @@ ...@@ -632,7 +669,7 @@
"source": [ "source": [
"# Add number 0 to the list\n", "# Add number 0 to the list\n",
"my_list.append(0)\n", "my_list.append(0)\n",
"print(my_list)" "my_list"
] ]
}, },
{ {
...@@ -643,7 +680,7 @@ ...@@ -643,7 +680,7 @@
"source": [ "source": [
"# Let's re-sort our list!\n", "# Let's re-sort our list!\n",
"my_list.sort()\n", "my_list.sort()\n",
"print(my_list)" "my_list"
] ]
}, },
{ {
...@@ -700,7 +737,7 @@ ...@@ -700,7 +737,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]" "my_list = [7, 12, 5, 10, 9]"
] ]
}, },
{ {
...@@ -790,7 +827,7 @@ ...@@ -790,7 +827,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"\n", "\n",
"# Find the next-to-last element in the list \n", "# Find the next-to-last element in the list \n",
"# using a negative index \n", "# using a negative index \n",
...@@ -823,7 +860,7 @@ ...@@ -823,7 +860,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"\n", "\n",
"# This gives me the \n", "# This gives me the \n",
"# first through second elements... right?\n", "# first through second elements... right?\n",
...@@ -871,7 +908,7 @@ ...@@ -871,7 +908,7 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"\n", "\n",
"# First through second elements\n", "# First through second elements\n",
"print(my_list[0:2])\n", "print(my_list[0:2])\n",
...@@ -898,7 +935,7 @@ ...@@ -898,7 +935,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"\n", "\n",
"# Get the first through third elements\n", "# Get the first through third elements\n",
"\n", "\n",
...@@ -930,9 +967,9 @@ ...@@ -930,9 +967,9 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"# Print the second through the end element\n", "# Print the second through the end element\n",
"print(my_list[1:])" "my_list[1:]"
] ]
}, },
{ {
...@@ -941,9 +978,9 @@ ...@@ -941,9 +978,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_big_list = [1,3,2,5,3,1,8,3,11,4]\n", "my_big_list = [1, 3, 2, 5, 3, 1, 8, 3, 11, 4]\n",
"# Works the same here\n", "# Works the same here\n",
"print(my_big_list[1:])" "my_big_list[1:]"
] ]
}, },
{ {
...@@ -962,8 +999,8 @@ ...@@ -962,8 +999,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Get everything but the last element\n", "# Get everything but the last element\n",
"my_list = [7,12,5,10,9]\n", "my_list = [7, 12, 5, 10, 9]\n",
"print(my_list[:-1])" "my_list[:-1]"
] ]
}, },
{ {
...@@ -972,9 +1009,9 @@ ...@@ -972,9 +1009,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_big_list = [1,3,2,5,3,1,8,3,11,4]\n", "my_big_list = [1, 3, 2, 5, 3, 1, 8, 3, 11, 4]\n",
"# Get everything up until the fourth element\n", "# Get everything up until the fourth element\n",
"print(my_big_list[:4])" "my_big_list[:4]"
] ]
}, },
{ {
...@@ -984,39 +1021,8 @@ ...@@ -984,39 +1021,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Yes, this would print the whole list 😎\n", "# Yes, this would print the whole list 😎\n",
"my_big_list = [1,3,2,5,3,1,8,3,11,4]\n", "my_big_list = [1, 3, 2, 5, 3, 1, 8, 3, 11, 4]\n",
"print(my_big_list[:])" "my_big_list[:]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## DRILL\n",
"\n",
"Practice slicing lists below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"this_list = [\"Slicing\",\"works\",\"on\",\"lists\",\"of\",\"strings\",\"identically\"]\n",
"\n",
"# Get the third to final elements\n",
"print(this_list[2:])\n",
"\n",
"\n",
"# Get everything up to the fourth element\n",
"print(this_list[:4])\n",
"\n",
"\n",
"# Get everything starting with the second-last element\n",
"print(this_list[-2:])"
] ]
}, },
{ {
...@@ -1056,24 +1062,7 @@ ...@@ -1056,24 +1062,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"This is an example of an [IPython magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html). These commands help with monitoring and managing your code environment.\n", "This is an example of an [IPython magic command](https://ipython.readthedocs.io/en/stable/interactive/magics.html). These commands help with monitoring and managing your code environment.\n"
"\n",
"We should be aware of the variables that we create as they take memory and can bloat our environment.\n",
"\n",
"If we aren't using a variable anymore, it's not a bad idea to delete it. We can do so with `del`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Remove the my_big_list_ variable\n",
"del my_big_list\n",
"\n",
"# my_big_list has left the building!\n",
"print(my_big_list)"
] ]
}, },
{ {
...@@ -1098,7 +1087,7 @@ ...@@ -1098,7 +1087,7 @@
"source": [ "source": [
"## Lists and data analysis\n", "## Lists and data analysis\n",
"\n", "\n",
"Lists are a foundational variable type in Python. It's worth getting comfortable with them as a foundational variable type in Python.\n", "Lists are a foundational variable type in Python. It's worth getting comfortable with them.\n",
"\n", "\n",
"All that said, lists are not easily capable of handing many common data analysis tasks. Let's take doubling what we would call a \"range\" of cells, like we do in spreadsheets all the time:" "All that said, lists are not easily capable of handing many common data analysis tasks. Let's take doubling what we would call a \"range\" of cells, like we do in spreadsheets all the time:"
] ]
...@@ -1123,7 +1112,7 @@ ...@@ -1123,7 +1112,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"my_list = [1,9,5,3,8]\n", "my_list = [1, 9, 5, 3, 8]\n",
"my_list * 2" "my_list * 2"
] ]
}, },
...@@ -1250,21 +1239,9 @@ ...@@ -1250,21 +1239,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"You will use packages all the time, and if you ever have an issue with one, a good place to start (after restarting the kernel!) is checking whether you have it installed, and what version.\n", "_However_, when you are working with an Anaconda distribution of Python, it's better to download packages direcly from Anaconda: `!conda install [package name]`.\n",
"\n", "\n",
"You can see all packages you've installed with `pip`, along with their versions, using `pip freeze`." "To see what packages come installed with Anaconda, and what packages you can install from there, head to [Anaconda's documentation](https://docs.anaconda.com/anaconda/packages/py3.7_win-64/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# This may take awhile\n",
"pip freeze"
] ]
}, },
{ {
...@@ -1273,9 +1250,7 @@ ...@@ -1273,9 +1250,7 @@
"source": [ "source": [
"# Drill \n", "# Drill \n",
"\n", "\n",
"One interesting thing about packages is that they can be based on top of and borrow code from other packages!\n", "Import the `seaborn` package. This is a visualization package that we'll use later in the class."
"\n",
"Import the `seaborn` package. This is a visualization package that we'll use later in the class, that is indeed built on top of another visualization package."
] ]
}, },