641 lines
88 KiB
Plaintext
641 lines
88 KiB
Plaintext
|
{
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"jp-MarkdownHeadingCollapsed": true
|
||
|
},
|
||
|
"source": [
|
||
|
"# Reminder: Do not rename file (or allow your computer to do that)\n",
|
||
|
"\n",
|
||
|
"Please overwrite the original exercise release file(s) when uploading your assignment. I still had to fix some of your submissions.\n",
|
||
|
"\n",
|
||
|
"# Update: Trouble with previous assignment due to `_` breaking.\n",
|
||
|
"\n",
|
||
|
"In the previous assignment, some of you had trouble because the assignment expected `_` to be the output of the previous cell, but the Jupyter output was increasing by \"2\" as described [here](https://stackoverflow.com/q/78765478). The solution to this problem [is to disable the Anaconda Assistant](https://stackoverflow.com/a/78766025/1633026), namely:\n",
|
||
|
"\n",
|
||
|
"Steps to fix:\n",
|
||
|
"\n",
|
||
|
"1. Disable Anaconda Assistant\n",
|
||
|
"1. Run `jupyter labextension disable @anaconda/assistant`\n",
|
||
|
"1. Restart the Jupyter notebook\n"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# `()` and `[]`\n",
|
||
|
"\n",
|
||
|
"Remember that Python is very, very precise about how it understands your code.\n",
|
||
|
"\n",
|
||
|
"`()` have multiple meanings in python:\n",
|
||
|
"\n",
|
||
|
"- function call: `my_function(arg1)`\n",
|
||
|
"- defining a function: `def my_function(arg1):`\n",
|
||
|
"- specifying the order of operations: `4*(3+2)`\n",
|
||
|
"- creating a tuple: `(1,2,3,4)` or `(1,)` or `()` (Note that `(1)` will not create a tuple. Also: you can create a tuple without parentheses...)\n",
|
||
|
"\n",
|
||
|
"`[]` similarly has a few meanings, based on context\n",
|
||
|
"\n",
|
||
|
"- creating a list: `[1,2,3,4]` or `[1,]` or `[1]` or `[]`\n",
|
||
|
"- getting an item: `x = my_list[index]`\n",
|
||
|
" - getting a slice: `x = my_list[start_index:end_index:increment]`\n",
|
||
|
"- setting an item: `my_list[index] = x`"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Basic plotting, revisited"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 1,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"# Below, we will use matplotlib, so we need to import it here.\n",
|
||
|
"import matplotlib.pyplot as plt"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 2,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"x=[1,2,3,4,5,6,7,8,9,1]\n",
|
||
|
"y=[0,4,0,3,3,0,3,4,5,2]"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x10b899cd0>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 3,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABphUlEQVR4nO3dd3iT57k/8O8ryZKXLO89sLHNMjZgprEhhEBLEpo0e5ME0vaU5CTlpL80TVea5NA2bU5H2rSB7N00exKSMGz2stm2wXtvyUv7/f0hSwGCwUPSq/H9XBdXa0e2bmMh3Xre5/negiiKIoiIiIicQCZ1AUREROQ72FgQERGR07CxICIiIqdhY0F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gNOOdAw2YnxHluKyxujDd8b/TEjVSljdm7xxowD+2ngYA/P666Zg9IVLiityPjQUREblNq06Pl3bW4LU9ddAOmnDD7GT84bo8AMC0RA2eumGGtAWOw+GGHjz87hEAwNolE/H9mckSVyQNNhZERORyx5t02FhShY/KmmCy2PZPpEUFY0ZKhMSVOU92nBrLpsXBahXxP8smSV2OZNhYEBGRS93/5iF8UNrk+HjOhAisLszAsqlxkHvZ/okLCQyQ4+mbZ8JosXrdvhBnYmNBREROpTdZoJTLHC+uGdGhkMsErMiJx5qiDMxICZe2QCeyWEV8UNqIq2ckQSYTIAiCx6Z/ugsbCyIicoqOPgNe3V2LV3bVYv0107F8WjwA4M6CCbg2PwnJEb53OuKJT07g+R3V2HGqE3+
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"plt.plot(x,y,'--')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"[<matplotlib.lines.Line2D at 0x10ba02490>]"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 4,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
},
|
||
|
{
|
||
|
"data": {
|
||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABysklEQVR4nO3dd1hc55k3/u+ZThmG3gQCVEFCQtWSuxP32I573137TXJt8ntlx46STeIkm7aJlbbeFCfOOm82m91E7r3XWHKJuigSoAoCB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zehbcCBUpsFv7g1NfIqptPrhOBxCOVZnI0CC0IIIUnx1N5OvNzQDb1OwG/uWovsdJPcS4pZqJ8FNcqajgILQgghCdfaa8f3Xj4MAPjaFcuxviJX5hXFh0863ds2BJ9flHk1ykKBBSGEkIRyuLzY8tcDcHn9uGR5Ab540SK5lxS3FaVZsJoNGHN50Uz9LKagwIIQQkhC/etLh3Ci34GiLDP+/dY66HSpl1cxnZ76WcyKAgtCCCEJ88y+Tjx/4DR0AvDrO9YiL9Ms95Ikw9t7726jwCIcBRaEEEIS4tiZMXz3JZZXsfXyZdgUSHhUC57AuZvyLKagwIIQQojkJt0+bNl+AJMeHy5Yko//75Ilci9JcitKspBpNmDM6UUL9bMIosCCEEKI5L7/8mEcPTOOAqsZ/3H7GuhVkFcxnUGvw8ZK1jWU8ixCKLAghBAiqRcPnsZT+zohCMCvbl+DAqt68iqmo34WZ6PAghBCiGRO9I/jWy80AQC+/OmlOG9JvswrSqxNwbkhQ/BTngU
|
||
|
"text/plain": [
|
||
|
"<Figure size 640x480 with 1 Axes>"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {},
|
||
|
"output_type": "display_data"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"x=[1,2,3,4,5,6,7,8,9,10]\n",
|
||
|
"y1=[0,4,0,3,3,0,3,4,5,2]\n",
|
||
|
"y2=[3,2,4,4,2,4,4,2,4,2]\n",
|
||
|
"plt.plot(x,y1)\n",
|
||
|
"plt.plot(x,y2)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# String formatting"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Old-style string formatting with `%`\n",
|
||
|
"\n",
|
||
|
"When the operator `%` is used on a string, the string is used as a *format string* for old-style formatting.\n",
|
||
|
"\n",
|
||
|
"In these old-style format strings, `%d` means to print an integer, `%s` means to print a string."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'The numbers are 5, 10, 20'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 5,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"\"The numbers are %d, %d, %d\" % (5,10,20)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"'The numbers are 5, 10, 20'"
|
||
|
]
|
||
|
},
|
||
|
"execution_count": 6,
|
||
|
"metadata": {},
|
||
|
"output_type": "execute_result"
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"tmp = (5,10,20)\n",
|
||
|
"\"The numbers are %d, %d, %d\" % tmp"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": 7,
|
||
|
"metadata": {},
|
||
|
"outputs": [
|
||
|
{
|
||
|
"ename": "TypeError",
|
||
|
"evalue": "not all arguments converted during string formatting",
|
||
|
"output_type": "error",
|
||
|
"traceback": [
|
||
|
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||
|
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||
|
"Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m tmp \u001b[38;5;241m=\u001b[39m (\u001b[38;5;241m5\u001b[39m,\u001b[38;5;241m10\u001b[39m,\u001b[38;5;241m20\u001b[39m,\u001b[38;5;241m100\u001b[39m)\n\u001b[0;32m----> 2\u001b[0m \u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mThe numbers are \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m, \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m, \u001b[39;49m\u001b[38;5;132;43;01m%d\u001b[39;49;00m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m%\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mtmp\u001b[49m\n",
|
||
|
"\u001b[0;31mTypeError\u001b[0m: not all arguments converted during string formatting"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"source": [
|
||
|
"tmp = (5,10,20,100)\n",
|
||
|
"\"The numbers are %d, %d, %d\" % tmp"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"tmp = (5,10)\n",
|
||
|
"\"The numbers are %d, %d, %d\" % tmp"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_string = \"The numbers are %d, %d, %d\"\n",
|
||
|
"my_string"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_string%(7, 14, 21)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"tuple1 = (100, 200, 300)\n",
|
||
|
"my_string % tuple1"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"The numbers are %d, %d, %d\"%(5,10)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"The numbers are %d, %d, %d\"%(5,10,20,40)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %s\"%(\"world\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %s\"%1"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %d, %d\"%(1, 2)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %d, %d\"%(1, 2)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %d\"%1"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %d\"%'hello'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello %s\"%'hello'"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## New-style (since Python 2.7) formatting with `.format()`"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"The numbers are {}, {}, {}\".format(5,10,20)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello {}\".format(\"world\")"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Even newer (since Python 3.6) style formatting with f-strings."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"name=\"Andrew\"\n",
|
||
|
"my_string = f\"Hello, my name is {name}.\"\n",
|
||
|
"print(my_string)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"f\"Hello, my name is {name}.\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"f\"Hello, my name is {name}. {akjfhasd}\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"f\"Hello, my name is {name}. {{akjfhasd}}\""
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"\"Hello, my name is {}.\".format(name)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_template = \"hello {}\"\n",
|
||
|
"print('my template is:', my_template)\n",
|
||
|
"my_template.format(name)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"my_template = \"hello {}\"\n",
|
||
|
"my_template.format(name)\n",
|
||
|
"print('my template is', my_template)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## Control flow with `while`"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"x = True\n",
|
||
|
"y = 0\n",
|
||
|
"while x:\n",
|
||
|
" print(y)\n",
|
||
|
" y = y + 100\n",
|
||
|
" if y >= 1000:\n",
|
||
|
" x = False\n",
|
||
|
"print('done')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = 0\n",
|
||
|
"while True:\n",
|
||
|
" print(y)\n",
|
||
|
" y = y + 100\n",
|
||
|
" if y >= 1000:\n",
|
||
|
" break\n",
|
||
|
"print('done')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = 0\n",
|
||
|
"while y < 1000:\n",
|
||
|
" print(y)\n",
|
||
|
" y = y + 100\n",
|
||
|
"print('done')"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## `break` and `continue`"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = 0\n",
|
||
|
"while True:\n",
|
||
|
" print(y)\n",
|
||
|
" y = y + 100\n",
|
||
|
" if y >= 1000:\n",
|
||
|
" break"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = 0\n",
|
||
|
"while True:\n",
|
||
|
" print(y) \n",
|
||
|
" y = y + 100\n",
|
||
|
" if y >= 1000:\n",
|
||
|
" break\n",
|
||
|
" if y < 400:\n",
|
||
|
" continue \n",
|
||
|
" y = y + 10"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"## The `+=` (and `-=`, `*=`, .. ) operator."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y += 20"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y = y +20"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y -= 20"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"execution_count": null,
|
||
|
"metadata": {},
|
||
|
"outputs": [],
|
||
|
"source": [
|
||
|
"y"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {},
|
||
|
"source": [
|
||
|
"# Running Python from the terminal\n",
|
||
|
"\n",
|
||
|
"We want to be able to run Python from the terminal. This is a low-level approach but will let us Python on all kinds of computers in all kinds of ways, not just interactively inside a Jupyter notebook.\n",
|
||
|
"\n",
|
||
|
"First, we need to be able to edit Python scripts. There are lots of programs that let us do that. I recommend [Visual Studio Code](https://code.visualstudio.com). To keep things simple, you can also use Jupyter Lab. In that case, you can click on the \"+\" in the tab list to open a Launcher tab, scroll down and create a \"Python File\". This will open an empty `untitled.py` file in the Jupyter lab editor. These `.py` files are *plain text* files which the `.py` extension tells the computer that this file should be Python source code. Let's create a file called `hello.py` which has the following contents.\n",
|
||
|
"\n",
|
||
|
"```python\n",
|
||
|
"print(\"hello world\")\n",
|
||
|
"```\n",
|
||
|
"\n",
|
||
|
"Now, let's run it in the terminal. Again, this can be done in multiple ways. One way is to use Jupyter Lab by clicking on the \"+\" in the tab list again. This type create a \"Terminal\". Another way is to use Anaconda Navigator as shown in this movie:\n",
|
||
|
"\n",
|
||
|
"<video controls src=\"CLI-python.mp4\" />"
|
||
|
]
|
||
|
}
|
||
|
],
|
||
|
"metadata": {
|
||
|
"kernelspec": {
|
||
|
"display_name": "Python 3 (ipykernel)",
|
||
|
"language": "python",
|
||
|
"name": "python3"
|
||
|
},
|
||
|
"language_info": {
|
||
|
"codemirror_mode": {
|
||
|
"name": "ipython",
|
||
|
"version": 3
|
||
|
},
|
||
|
"file_extension": ".py",
|
||
|
"mimetype": "text/x-python",
|
||
|
"name": "python",
|
||
|
"nbconvert_exporter": "python",
|
||
|
"pygments_lexer": "ipython3",
|
||
|
"version": "3.11.7"
|
||
|
}
|
||
|
},
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 4
|
||
|
}
|