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

load files

parent 742f575e
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Up and running from spreadsheets to Python\n",
"\n",
"## Hello, Jupyter\n",
"\n",
"This is the interface that we will use to execute `.ipynb`, or IPython notebook files. \n",
"\n",
"You may also often see Python files with a `.py` extension. These are *script* files versus *notebook* files.\n",
"\n",
"Notebooks are divided into cells which can be either text or code, among other things.\n",
"\n",
"Go ahead and click into this cell. What happens? "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can add, reorder, cut and paste cells using the menu icons. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(From a new cell) you are seeing *raw Markdown* styling in the above cell. \n",
"\n",
"You can close out of it by **running the cell.** `Ctrl + Enter` is the keyboard shortcut.\n",
"\n",
"Markdown allows us to style text using plain-text format. \n",
"\n",
"There's [a lot you can do with Markdown](https://www.markdownguide.org/cheat-sheet). Some basics:\n",
"\n",
"# Big Header 1\n",
"## Smaller Header 2\n",
"### Even smaller headers\n",
"#### Still more\n",
"\n",
"*Using one asterisk renders italics*\n",
"\n",
"**Using two asterisks renders bold**\n",
"\n",
"It's worth studying up on Markdown to write elegant text in your notebooks. \n",
"\n",
"But in this class we'll focus on the *code* block, because that's where executable code goes!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Python as a fancy calculator\n",
"\n",
"We can use Python as a highfalutin calculator, just as you might do with Excel.\n",
"\n",
"Enter some basic arithmetic below, then **run the cell** (Do you remember how to do that?)."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# This is a code block. \n",
"# You can execute code here.\n",
"\n",
"# Python can be used as a fancy calculator.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some of these arithmetic operators are the same as in Excel, but others are different:\n",
"\n",
"\n",
"| Operator | Description |\n",
"| -------- | -------------- |\n",
"| `+` | Addition |\n",
"| `-` | Subtraction |\n",
"| `/` | Division |\n",
"| `%` | Modulus |\n",
"| `**` | Exponent |\n",
"| `//` | Floor division |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Cell comments\n",
"\n",
"What's the deal with the hashtags and text in the above cell?\n",
"\n",
"Those are cell comments used to give us verbal instructions and reminders about our code. This helps other users -- and ourselves -- remember what we are doing with it.\n",
"\n",
"![Gandalf coding meme](images/gandalf.jpg)\n",
"\n",
"And yes, you can embed images into notebooks 😎.\n",
"\n",
"Try writing comments in the cell below.\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# Hello, world!"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0606601717798214"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Python follows the order of operations, just like spreadsheets. \n",
"\n",
"2+3/4*2**.5"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Running functions\n",
"\n",
"Python includes many functions for working with data.\n",
"\n",
"Functions take arguments inside parentheses, just like in Excel:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# We can also call functions:\n",
"# Let's find the absolute value of -100\n",
"abs(-100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are some important differences:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'ABS' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-15-60970b9d13d5>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# These aren't going to work to find them!\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mABS\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mAbs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'ABS' is not defined"
]
}
],
"source": [
"# These aren't going to work to find them!\n",
"ABS(-100)\n",
"Abs(-100)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Moral of the story: **Python is case-sensitive** and all-around finicky. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comparison operators\n",
"\n",
"We can also test for whether one value is greater than another, much like you would do in Excel:"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Is 3 > 4?\n",
"3 > 4"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like in Excel, Python will return either a `True` or `False`. \n",
"\n",
"Much of these conditional operators will look familiar to you:\n",
"\n",
"| Operator | Meaning |\n",
"| -------- | ------------------------ |\n",
"| `!=` | Not equal to |\n",
"| `>` | Greater than |\n",
"| `<` | Less than |\n",
"| `>=` | Greater than or equal to |\n",
"| `<=` | Less than or equal to |\n",
"| **`==`** | **Equal to** |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Did you catch that last one?\n",
"\n",
"You do not check for whether two values are equal to each other using `=`, but instead using `==`. Why?\n",
"\n",
"Because in Python, we assign data to *variables*. This is a game-changer!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Assigning variables\n",
"\n",
"Calling functions like `abs(100)` can be useful, but where things get *really* interesting in Python is by assigning results of operations to variables.\n",
"\n",
"Let's go ahead and pass the absolute value of -100 to a variable, `my_first_variable`."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"my_first_variable = abs(-100)"
]
},
{
"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",
"\n",
"### Printing variables\n",
"\n",
"To see the result of that variable, we can *print* it using the `print()` function: "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
}
],
"source": [
"print(my_first_variable)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What do you think the result of the below will be?"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'MY_FIRST_VARIABLE' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-19-97b021f5434c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mMY_FIRST_VARIABLE\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mNameError\u001b[0m: name 'MY_FIRST_VARIABLE' is not defined"
]
}
],
"source": [
"print(MY_FIRST_VARIABLE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"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",
"There are some rules in naming Python variables:\n",
"\n",
"- They must start with a letter or underscore.\n",
"- The rest of your variable can only contain letters, numbers or underscores.\n",
"\n",
"In theory, you can name your variables almost anything so long as they fit these rules. But `sales` may be a better name for sales data than `scooby_doo`. \n",
"\n",
"### DRILL\n",
"\n",
"Based on these rules, which of the following is an invalid variable name?\n",
"\n",
"A. `My_string_` \n",
"B. `string_1` \n",
"C. `razzle.dazzle` \n",
"D. `_` "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Try assigning and printing these variables if you're not sure!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Variable types\n",
"\n",
"You can think about a variable as a box that we are putting a piece of information into. \n",
"\n",
"Variables can be of different types, like different categories and dimensions of boxes. \n",
"\n",
"![variables shoebox](images/variables-shoebox.png)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can find a variable's type with the `type()` function."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n",
"<class 'str'>\n",
"<class 'bool'>\n"
]
}
],
"source": [
"# Assigning different variable types\n",
"\n",
"# Integer\n",
"my_int = 2\n",
"\n",
"# Float(ing point decimal)\n",
"my_float = 2.222\n",
"\n",
"# String\n",
"my_string = 'Hello'\n",
"\n",
"# 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))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can call functions directly on these variables.\n",
"\n",
"In fact, that's what we were doing with `print()` and `type()` all along!"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Absolute value of my_int\n",
"abs(my_int)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Length of my_string\n",
"len(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"# Assign the product to a variable\n",
"my_nonsense = abs(my_int) * len(my_string)\n",
"print(my_nonsense)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# DRILLS\n",
"\n",
"1. Assign the sum of -10 and 2 to `a`.\n",
"2. Assign the absolute value of `a` to `b`.\n",
"3. Assign `b` minus 1 as `d`.\n",
"4. Print the result of `d`. What is the value? What type is this variable?\n",
"\n",
"You can insert a code cell below to conduct your work.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# From spreadsheet ranges to Python lists\n",
"\n",
"Generally in spreadsheets we want to operate on multiple cells at a time and the same is true in Python. "
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"<class 'int'>\n"
]
}
],
"source": [
"# You know how to assign the number 1 to a variable... do it now!\n",
"my_variable = 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What about the numbers 1, 2 and 3? Do we have to assign each to its own variable?\n",
"\n",
"Thank heavens not! We can use a *collection* variable to to assign all of them at once. Let's look at a common collection data type, a list.\n",
"\n",
"## Lists\n",
"\n",
"Lists are denoted with brackets `[]`. \n",
"\n",
"Each *element* of the list is separated by commas `,`."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"<class 'list'>\n"
]
}