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

update files

q
parent 806a2d8c
......@@ -83,7 +83,8 @@
"source": [
"my_week = (['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'])\n",
"my_week.sort()\n",
"print(my_week)"
"\n",
"my_week"
]
},
{
......@@ -95,7 +96,7 @@
"outputs": [],
"source": [
"my_week.clear()\n",
"print(my_week)"
"my_week"
]
},
{
......@@ -127,42 +128,6 @@
"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",
"metadata": {},
......
......@@ -260,7 +260,7 @@
"cell_type": "markdown",
"metadata": {},
"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",
"### Printing variables\n",
"\n",
......@@ -278,6 +278,22 @@
"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",
"metadata": {},
......@@ -291,7 +307,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(MY_FIRST_VARIABLE)"
"MY_FIRST_VARIABLE"
]
},
{
......@@ -300,8 +316,6 @@
"source": [
"## Python variable naming conventions\n",
"\n",
"> There are only two hard things in Computer Science: cache invalidation and naming things. --Phil Karlton\n",
"\n",
"\n",
"In theory, you can name your variables almost anything so long as they fit these rules:\n",
"\n",
......@@ -353,7 +367,7 @@
"cell_type": "markdown",
"metadata": {},
"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 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"metadata": {},
"outputs": [],
"source": [
"# Assigning different variable types\n",
"\n",
"# Integer\n",
"my_int = 2\n",
"\n",
"type(my_int)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Float(ing point decimal)\n",
"my_float = 2.222\n",
"\n",
"type(my_float)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# String\n",
"my_string = 'Hello'\n",
"\n",
"type(my_string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Boolean\n",
"my_boolean = True\n",
"\n",
"print(type(my_int))\n",
"print(type(my_float))\n",
"print(type(my_string))\n",
"print(type(my_boolean))"
"type(my_boolean)"
]
},
{
......@@ -437,7 +467,7 @@
"source": [
"# Assign the product to a variable\n",
"my_nonsense = abs(my_int) * len(my_string)\n",
"print(my_nonsense)"
"my_nonsense"
]
},
{
......@@ -495,9 +525,17 @@
"outputs": [],
"source": [
"# Make a list\n",
"my_first_list = [1,2,3]\n",
"print(my_first_list)\n",
"print(type(my_first_list))"
"my_first_list = [1, 2, 3]\n",
"my_first_list"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(my_first_list)"
]
},
{
......@@ -517,9 +555,8 @@
"metadata": {},
"outputs": [],
"source": [
"my_other_list = [1,2,3,\"Boo!\"]\n",
"print(my_other_list)\n",
"print(type(my_other_list))"
"my_other_list = [1, 2, 3, \"Boo!\"]\n",
"type(my_other_list)"
]
},
{
......@@ -535,9 +572,8 @@
"metadata": {},
"outputs": [],
"source": [
"my_list_here = [1,2,3,[1,2,3,\"Boo!\"]]\n",
"print(my_list_here)\n",
"print(type(my_list_here))"
"my_list_here = [1, 2, 3, [1, 2, 3, \"Boo!\"]]\n",
"type(my_list_here)"
]
},
{
......@@ -591,11 +627,12 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [-1,4,3,2]\n",
"my_list = [-1, 4, 3, 2]\n",
"\n",
"# This is a method\n",
"my_list.sort()\n",
"print(my_list)"
"\n",
"my_list"
]
},
{
......@@ -612,7 +649,7 @@
"outputs": [],
"source": [
"my_list.sort(reverse=True)\n",
"print(my_list)"
"my_list"
]
},
{
......@@ -632,7 +669,7 @@
"source": [
"# Add number 0 to the list\n",
"my_list.append(0)\n",
"print(my_list)"
"my_list"
]
},
{
......@@ -643,7 +680,7 @@
"source": [
"# Let's re-sort our list!\n",
"my_list.sort()\n",
"print(my_list)"
"my_list"
]
},
{
......@@ -700,7 +737,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [7,12,5,10,9]"
"my_list = [7, 12, 5, 10, 9]"
]
},
{
......@@ -790,7 +827,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [7,12,5,10,9]\n",
"my_list = [7, 12, 5, 10, 9]\n",
"\n",
"# Find the next-to-last element in the list \n",
"# using a negative index \n",
......@@ -823,7 +860,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [7,12,5,10,9]\n",
"my_list = [7, 12, 5, 10, 9]\n",
"\n",
"# This gives me the \n",
"# first through second elements... right?\n",
......@@ -871,7 +908,7 @@
},
"outputs": [],
"source": [
"my_list = [7,12,5,10,9]\n",
"my_list = [7, 12, 5, 10, 9]\n",
"\n",
"# First through second elements\n",
"print(my_list[0:2])\n",
......@@ -898,7 +935,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [7,12,5,10,9]\n",
"my_list = [7, 12, 5, 10, 9]\n",
"\n",
"# Get the first through third elements\n",
"\n",
......@@ -930,9 +967,9 @@
},
"outputs": [],
"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(my_list[1:])"
"my_list[1:]"
]
},
{
......@@ -941,9 +978,9 @@
"metadata": {},
"outputs": [],
"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",
"print(my_big_list[1:])"
"my_big_list[1:]"
]
},
{
......@@ -962,8 +999,8 @@
"outputs": [],
"source": [
"# Get everything but the last element\n",
"my_list = [7,12,5,10,9]\n",
"print(my_list[:-1])"
"my_list = [7, 12, 5, 10, 9]\n",
"my_list[:-1]"
]
},
{
......@@ -972,9 +1009,9 @@
"metadata": {},
"outputs": [],
"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",
"print(my_big_list[:4])"
"my_big_list[:4]"
]
},
{
......@@ -984,39 +1021,8 @@
"outputs": [],
"source": [
"# Yes, this would print the whole list 😎\n",
"my_big_list = [1,3,2,5,3,1,8,3,11,4]\n",
"print(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:])"
"my_big_list = [1, 3, 2, 5, 3, 1, 8, 3, 11, 4]\n",
"my_big_list[:]"
]
},
{
......@@ -1056,24 +1062,7 @@
"cell_type": "markdown",
"metadata": {},
"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",
"\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)"
"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"
]
},
{
......@@ -1098,7 +1087,7 @@
"source": [
"## Lists and data analysis\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",
"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 @@
"metadata": {},
"outputs": [],
"source": [
"my_list = [1,9,5,3,8]\n",
"my_list = [1, 9, 5, 3, 8]\n",
"my_list * 2"
]
},
......@@ -1250,21 +1239,9 @@
"cell_type": "markdown",
"metadata": {},
"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",
"You can see all packages you've installed with `pip`, along with their versions, using `pip freeze`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# This may take awhile\n",
"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/)."
]
},
{
......@@ -1273,9 +1250,7 @@
"source": [
"# Drill \n",
"\n",
"One interesting thing about packages is that they can be based on top of and borrow code from other packages!\n",
"\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."
"Import the `seaborn` package. This is a visualization package that we'll use later in the class."
]
},
{
......@@ -1293,13 +1268,6 @@
"source": [
"# Questions about modules and packages?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
......
......@@ -34,26 +34,14 @@
"\n",
"`numpy` is short for \"numerical Python.\"\n",
"\n",
"Generally when we are using a package for the first time, we need to do one of these:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# Install numpy\n",
"#!pip install numpy"
"Generally when we are using a package for the first time, we need to install it: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, `numpy` was installed already when we installed `pandas`.\n",
"However, `numpy` was installed already when we installed `pandas`. (These packages are also pre-installed with Anaconda).\n",
"\n",
"We *do* still need to import `numpy` before using it: "
]
......@@ -86,9 +74,18 @@
"outputs": [],
"source": [
"# Create an array\n",
"my_array = numpy.array([4,1,5,2])\n",
"print(my_array)\n",
"print(type(my_array))"
"my_array = numpy.array([4, 1, 5, 2])\n",
"my_array\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(my_array)"
]
},
{
......@@ -98,16 +95,14 @@
"outputs": [],
"source": [
"# This is an example of type coercion\n",
"my_coerced_array = numpy.array([1,2,3,'Boo!'])\n",
"print(my_coerced_array)"
"my_coerced_array = numpy.array([1, 2, 3, 'Boo!'])\n",
"my_coerced_array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also convert our list to an array.\n",
"\n",
"Lists and arrays may *look* the same to you, but they are of different types."
]
},
......@@ -117,12 +112,18 @@
"metadata": {},
"outputs": [],
"source": [
"print(my_list)\n",
"my_list\n",
"type(my_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_list_to_array = numpy.array(my_list)\n",
"print(my_list_to_array)\n",
"\n",
"print(type(my_list))\n",
"print(type(my_list_to_array))"
"type(my_list_to_array)"
]
},
{
......@@ -132,6 +133,15 @@
"`numpy` arrays work in many ways like ranges of a spreadsheet..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_array"
]
},
{
"cell_type": "code",
"execution_count": null,
......@@ -141,8 +151,7 @@
"outputs": [],
"source": [
"# Isn't this what you were expecting earlier?\n",
"print(my_array)\n",
"print(my_array*2)"
"my_array * 2"
]
},
{
......@@ -255,13 +264,27 @@
},
"outputs": [],
"source": [
"my_array = np.array([4,1,5,2])\n",
"\n",
"my_array = np.array([4,1,5,2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Access first element of the array\n",
"print(my_array[1])\n",
"\n",
"my_array[1]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Oh sorry... NOW I'm accessing the first element! 🤦‍♂️\n",
"print(my_array[0])"
"my_array[0]"
]
},
{
......@@ -287,7 +310,7 @@
"Source: Nunez-Iglesias, Juan, Stéfan Van Der Walt, and Harriet Dashnow. *Elegant SciPy: The Art of Scientif