"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",
"\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:"
"\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",
"\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",