pm21-dragon/lectures/lecture-02/1 - Scopes, Types, etc.ipynb

3511 lines
174 KiB
Plaintext
Raw Normal View History

2024-10-25 02:05:31 -04:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Review\n",
"\n",
"One key idea in programming is a **variable**. A variable is a name that refers to a value. It lets us reference the value, even if we do not know what it is. We can **assign** to variables in Python using the `=` symbol.\n",
"\n",
"Even though we are still working with simple code, we have already learned two key ideas of programming that let us build more complex systems through modularity.\n",
"\n",
"The first is **functions**. We learned that code is organized in **blocks**.\n",
"\n",
"The second idea is **scope**.\n",
"\n",
"We also learned about **exceptions**."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"x = 0\n",
"\n",
"def foo(a):\n",
" return a\n",
"\n",
"z = foo(x)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"32\n",
"3.125\n",
"0\n",
"32\n"
]
}
],
"source": [
"x = 0\n",
"\n",
"def foo(x):\n",
" y = 100/x\n",
" print(x)\n",
" print(y)\n",
" return y\n",
"\n",
"y = 32\n",
"z = foo(y)\n",
"print(x)\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Coding style\n",
"\n",
"It is great if your code runs successfully! However, you are likely to want to run it again. (And even if not, it is good to practice \"Doin' It Right\".) Therefore, it is good to optimize it for readability and reuse. There is certainly a sense of style in software coding. It's good to develop your own sense of good style.\n",
"\n",
"There is more one way to write correct code. (In fact, there are infinitely many ways.)\n",
"\n",
"Depending on the programming language, and often depending on the project or environment, there are standard, \"idiomatic\" ways of doing things. You should prefer those - they make your code easier to read for others - and also yourself in the future.\n",
"\n",
"It is useful to spend time refining code so that it looks simple and, ideally, *is* simple. Of course this is not always possible.\n",
"\n",
"One stylistic rule that holds pretty much in every programming language is the principle \"Don't Repeat Yourself\" (DRY). If you need to change something, ideally you will only need to change it in one place in your code. Another way of saying this is that there should only be a \"single source of truth\".\n",
"\n",
"The python package \"black\" is commonly used linter for making code style changes to an idiomatic format.\n",
"\n",
"### Style suggestions\n",
"\n",
"- In general: simplify\n",
"- Remove irrelevant code (e.g. `plt.show;`)\n",
"- Remove irrelevant comments (e.g. `# YOUR CODE HERE`)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Types\n",
"\n",
"Every value in Python has a **type**. You don't usually *see* the types written, but it is critically important to understand what types are used in code you are working on. Python checks *exactly* how something is typed and, according to strict rules, decides what type the value will have. For example, these will all be different types:\n",
"\n",
"```python\n",
"10\n",
"10.0\n",
"'10'\n",
"```\n",
"\n",
"\n",
"## Types of types\n",
"\n",
"So far, we have mostly worked with whole numbers called **integers** - in python, an `int`. We briefly saw `None` and some strings. You may have also seen **floating point numbers** (`float` in python).\n",
"\n",
"Let's practice with some of these types.\n",
"\n",
"## Playing with types"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10.0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(10.0)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 10\n",
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(\"10\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type('10')"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"my_string = \"Hello, My name is Angela Merkel\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"my_string = \"übersetzen\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"Übersetzung = \"translation\""
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"übersetzen\n"
]
}
],
"source": [
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: \"gcatcccggg\"\n"
]
}
],
"source": [
"my_string = 'the data is like this: \"gcatcccggg\"'\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (1042171736.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[15], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m my_string = \"the data is like this: \"gcatcccggg\"\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"my_string = \"the data is like this: \"gcatcccggg\"\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: \"gcatcccggg\"\n"
]
}
],
"source": [
"my_string = \"the data is like this: \\\"gcatcccggg\\\"\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: \\gcatcccggg\"\n"
]
}
],
"source": [
"my_string = \"the data is like this: \\\\gcatcccggg\\\"\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: 'gcatcccggg'\n"
]
}
],
"source": [
"my_string = 'the data is like this: \\'gcatcccggg\\''\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (57043761.py, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m Cell \u001b[0;32mIn[19], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m my_string = 'the data is like this: 'gcatcccggg''\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"my_string = 'the data is like this: 'gcatcccggg''\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: 'gcatcccggg'\n"
]
}
],
"source": [
"my_string = \"the data is like this: 'gcatcccggg'\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the data is like this: \\ 'gcatcccggg'\n"
]
}
],
"source": [
"my_string = \"the data is like this: \\\\ 'gcatcccggg'\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"x=10"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1234"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1234"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"def myprint(x):\n",
" print(x)\n",
" return 32"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
},
{
"data": {
"text/plain": [
"32"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myprint(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"myprint(\"hello\");"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"# In Jupyter, `_` is a special variable, which means the output value of the previously run cell.\n",
"y=_"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"x=x+1"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"z=x=x+1"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# None type\n",
"\n",
"The `None` type is used when there is no value. It is actually very common in Python. For example a function which does not return anything actually returns the value `None`."
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"x=None\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NoneType"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=None\n",
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"None"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"None\n"
]
}
],
"source": [
"x=print(10)\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"x=10"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operators\n",
"\n",
"Many of the most commonly used operations (like addition) could be written as function calls, but instead have special symbols (like `+`)."
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3+4"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int.__add__(3,4)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcdef'"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"abc\" + \"def\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"str.__add__(\"abc\", \"def\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Complex expressions\n",
"\n",
"Often we write expressions in which multiple operations happen in one line of code"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 * 3 + 2"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 + 4 * 3"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"x = 4 * 3 + 2"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"x = x * 2"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y=4*3+2\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp = 4*3\n",
"y = tmp+2\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"tmp = 'my super import data'\n",
"\n",
"y=4*(3+2)"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'my super import data'"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"tmp2 = 'my super import data'\n",
"\n",
"tmp=3+2\n",
"tmp2 = 4*tmp\n",
"y = tmp2"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp2"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"14\n"
]
}
],
"source": [
"print(4*3+2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# list type\n",
"\n",
"## list construction"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 2, 2, 2, 2, 'three', 4.0]\n"
]
}
],
"source": [
"x = [1,2,2,2,2,2,\"three\", 4.0]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"x=list()\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4]\n"
]
}
],
"source": [
"x=[1,2,3,4]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"[1]\n",
"[1, 2]\n",
"[1, 2, 3]\n",
"[1, 2, 3, 4]\n",
"None\n"
]
}
],
"source": [
"x=[]\n",
"print(x)\n",
"\n",
"x.append(1)\n",
"print(x)\n",
"\n",
"x.append(2)\n",
"print(x)\n",
"\n",
"x.append(3)\n",
"print(x)\n",
"\n",
"z = x.append(4)\n",
"print(x)\n",
"\n",
"print(z)"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'green', 'blue']\n"
]
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 101, 'green', 202, 'blue', 303]\n"
]
}
],
"source": [
"x=[\"red\", 101, \"green\", 202, \"blue\", 303]\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 101, 'green', 202, 'blue', 303, [1, 2, 3], 'lkasjdf\"laskdjfj']\n"
]
}
],
"source": [
"x=[\"red\", 101, \"green\", 202, \"blue\", 303, [1,2,3], 'lkasjdf\"laskdjfj']\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list indexing"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'red'"
]
},
"execution_count": 75,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[0]"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'green'"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[1]"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[2]"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 78,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[-1]"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'violet'"
]
},
"execution_count": 79,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[-1]"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[-3]"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']"
]
},
"execution_count": 81,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'yellow'"
]
},
"execution_count": 82,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y=2\n",
"x[y]"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "list indices must be integers or slices, not str",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[83], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mhello\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m]\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
]
}
],
"source": [
"x['hello']"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "list indices must be integers or slices, not float",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[84], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1.23\u001b[39;49m\u001b[43m]\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not float"
]
}
],
"source": [
"x[1.23]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## getting index of item in list"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x.index(\"blue\")"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "321 is not in list",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[86], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m321\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mValueError\u001b[0m: 321 is not in list"
]
}
],
"source": [
"x.index(321)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## setting an item in a list"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [],
"source": [
"x[3]=3"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list slicing"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[0:3]"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'green'"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[3]"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:3]"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[None:3]"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0:3]"
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo', 'violet']"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[3:]"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo', 'violet']"
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[3:None]"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo']"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[3:-1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# tuples\n",
"## tuple construction"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1, 2, 3, 4)\n"
]
}
],
"source": [
"x = (1,2,3,4)\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"()\n"
]
}
],
"source": [
"x = ()\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1,)\n"
]
}
],
"source": [
"x = (1,)\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"1\n"
]
}
],
"source": [
"x = (1)\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1,)\n"
]
}
],
"source": [
"x = 1,\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1, 2, 3)\n"
]
}
],
"source": [
"x = 1,2,3\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"1\n"
]
}
],
"source": [
"x = 1\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"()\n"
]
}
],
"source": [
"x = tuple()\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1,)\n"
]
}
],
"source": [
"x = tuple([1])\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"(1, 2, 3, 4)\n"
]
}
],
"source": [
"x = tuple([1,2,3,4])\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "tuple expected at most 1 argument, got 4",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[115], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m x \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mtuple\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;28mtype\u001b[39m(x))\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(x)\n",
"\u001b[0;31mTypeError\u001b[0m: tuple expected at most 1 argument, got 4"
]
}
],
"source": [
"x = tuple(1,2,3,4)\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'list'>\n",
"<class 'tuple'>\n",
"(1,)\n"
]
}
],
"source": [
"tmp = [1]\n",
"print(type(tmp))\n",
"x = tuple(tmp)\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## tuple indexing and slicing"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'yellow'"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[2]"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'violet'"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[-1]"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[-3]"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('red', 'orange', 'yellow')"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[0:3]"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('red', 'orange', 'yellow')"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## lists are *mutable*, tuples are not"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']\n",
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"print(x)\n",
"x[3]=3\n",
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet')\n"
]
},
{
"ename": "TypeError",
"evalue": "'tuple' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[123], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m x\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124morange\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124myellow\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mgreen\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mblue\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mindigo\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mviolet\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28mprint\u001b[39m(x)\n\u001b[0;32m----> 3\u001b[0m \u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m3\u001b[39m\n\u001b[1;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(x)\n",
"\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
]
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"print(x)\n",
"x[3]=3\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### passing mutable lists to functions"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"def modify_arg(a,b):\n",
" a.append(b)\n",
" # Notice that there is no return here!\n",
" # So, this function has an important \"side effect\",\n",
" # but the output is None.\n",
" \n",
"x = [1,2,3]\n",
"y = modify_arg(x, 4)\n",
"x\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## variables can be names pointing to an object in memory"
]
},
{
"cell_type": "code",
"execution_count": 127,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']\n",
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"y=x\n",
"print(x)\n",
"y[3]=3\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 128,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 129,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"0\n",
"3\n"
]
}
],
"source": [
"x=0\n",
"y=x\n",
"print(x)\n",
"y=3\n",
"print(x)\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 130,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']\n",
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"# View the previous example and then this one in pythontutor.com\n",
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"y=x.copy()\n",
"print(x)\n",
"y[3]=3\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 131,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Plotting"
]
},
{
"cell_type": "code",
"execution_count": 132,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 138,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sb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pKSHHkII05WcmcyW8w9atsZs0ejpjyEToAlROhS6GDl58iT29vbY2NgQGBjImjVraNq0ab7burq6Mm/ePIKDgwkJCaFRo0Z069aNPXv2PPEYs2bNwsnJKe/h7u5e2JhCiBK04dwGMnWZNKzckBbOLYx+vGFNpVUjRGmiURRFKcwOWVlZxMTEcP/+fYKDg1mwYAHh4eGPLUj+qX///mg0Gtavf/xI+MzMTDIzM/N+TkpKwt3dncTERBwdHQsTVwhRAgYtH8S6s+t41+9dPur6UYkcs8WcFpxMOMmiAYsY6zm2RI4phCicpKQknJycnvr+XehvRqytralfvz4+Pj7MmjWLli1b8u233xZ4/7Zt23L+/PknbmNjY5N3x47+IYQwTUmZSWy5UHItGj39sfRjVYQQ5qvY84woivLQtxhPExkZiaura3EPK4QwEevPridLl0XjKo1pXq15iR1X36rZcWkHd9LulNhxhRCGZ1mYjWfOnEnv3r1xd3cnOTmZ5cuXExYWxtatWwGYMWMG169f55dffgFg9uzZ1K5dm2bNmpGVlcXSpUsJDg4mODjY8GcihFCFfr6P4U2NM9HZ4zSq0oiWzi05cfMEa8+sZbzX+BI7thDCsApVjNy8eZORI0cSFxeHk5MTLVq0YOvWrfT
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x=[1,2,3,0,4,1]\n",
"y=[0,4,0,3,3,0]\n",
"plt.plot(x,y,\"go-\");"
]
},
{
"cell_type": "code",
"execution_count": 141,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB1t0lEQVR4nO3deVxU9f7H8dcs7KuCLCoq7guiuKPQZmmCpu27tlhZKpa3e2/W3bv9vPf+uv0STc3S9tIKMxU0rVz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fymX6HV353agezfKad7+7mSM5xfzngXAeGiRfWISwRUVFRfj4+Nzw87veV0acnZ3p2rUrgwYNYvbs2fTr1485c+bUef9hw4Zx7Nix627j4uJSM2LH+hBC2Kbiiko2ZVRHNE04iubXrDeyygRoQti/m55nRFGUq65i3EhaWhrBwc33hiWEaFo/Hs7FaLbQpY0HPQKbPqKxiqlufLYcz+dyqbHZXlcI0fj09dn49ddfZ8yYMYSEhFBcXMzSpUvZuHEja9euBWDWrFmcO3eOTz/9FIB3332XTp060adPH4xGI59//jkJCQkkJCQ0/pEIIVRRM9FZeOOuRXMjXdp40ivYm8PZRaw7lMPDgzs022sLIRpXvZqR3NxcnnzySbKzs/Hx8SE8PJy1a9dy1113AZCdnU1mZmbN9kajkVdffZVz587h5uZGnz59SExMJCYmpnGPQgihisLySjYfzQcafy2auhgbHszh7CJW78+WZkQIO1bvG1jVUNcbYIQQzSth91l+980+ugV4sn7mrc3++qfyS7n97Y3otBp
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x=[1,2,3,0,4,1]\n",
"y=[0,4,0,3,3,0]\n",
"plt.plot(x,y);"
]
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABXDElEQVR4nO3deXCU97kv+O/bu5bu1r4hAVpArAIhDAgkGwcbAhiD45nJvZOxc0/iTFEx8XEoxndwqnJz46RIzXFliMvHJr6x43MmldiTwQgcMDE5NvtiFglkNktCICG1dqlba6/v/NF6GwSSUEvd/Xa//f1UdSVqv61+JKHW07/f83seQRRFEUREREQyUckdABEREUU3JiNEREQkKyYjREREJCsmI0RERCQrJiNE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YA7Ny5E01NTfj3f/93AMDu3bsxc+ZMzJ8/Hw6HA3/605+wd+9e7N27N/BfCYWMbx5NkJIRqQ7lwu1uDDrciNHx9BURkZL5lYy0trbihRdegMVigdlsRlFREQ4fPoynn34aAGCxWNDQ0OC73uFwYMeOHWhqakJMTAzmz5+PgwcPYsOGDYH9KihkGrsGcKdzABqVgBX5yUF5jvzUOGSaDbBYh3D+dhcen82+NERESuZ3AascJloAQ8H3l68asPOTajw2MxF/3boyaM/zf/z1Mv568S7+98fz8PqGuUF7HiIiCp6gFbBSdJPqOAJ9pPdBUr+RE6wbISJSPCYjNGFuj4hTdYGdRzMWKdm5brGho2/iBdJERBR5mIzQhF1rtqFnwIl4vQaLshOC+lwp8XrMzfQu6Z1
"text/plain": [
"<Figure size 640x480 with 1 Axes>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y=[0,4,0,3,3,0]\n",
"plt.plot(y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Cheatsheet for much more matplotlib: https://twitter.com/dr_shlee/status/1282772480046891010"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Boolean (`bool`) type\n",
"\n",
"Python's `bool` type can take one of two values: `True` or `False`. It is used to test a condition, such as in an *if statement*."
]
},
{
"cell_type": "code",
"execution_count": 144,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 144,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"y = x > 0\n",
"y"
]
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 145,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(y)"
]
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 148,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = -1\n",
"\n",
"if x > 0:\n",
" print(\"x is positive\")\n",
" x = 10\n",
"\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 149,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x>0"
]
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x>0)"
]
},
{
"cell_type": "code",
"execution_count": 153,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is positive\n",
"x is still positive\n",
"out of the blocks\n",
"20\n"
]
}
],
"source": [
"x = 1\n",
"\n",
"if x > 0:\n",
" y = 20\n",
" print(\"x is positive\")\n",
" print(\"x is still positive\")\n",
"else:\n",
" y = 40\n",
" print(\"x is negative\")\n",
" print(\"x is still negative\")\n",
" \n",
"print(\"out of the blocks\")\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Equality testing"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"# Equality and comparisons\n",
"\n",
"Comparison operators: `==`, `>`, `>=`, `<`, `<=`, `!=`\n",
"\n",
"These operators take two arguments (left and right hand side) and return a boolean."
]
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 154,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 > 4"
]
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 155,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 == 4"
]
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 156,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+2 == 4"
]
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 157,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+(2 == 4)"
]
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 158,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+(False)"
]
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coercion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## explicit coersion"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"x = \"10\""
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'10'"
]
},
"execution_count": 161,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 162,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "can only concatenate str (not \"int\") to str",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[163], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[38;5;241;43m32\u001b[39;49m\n",
"\u001b[0;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str"
]
}
],
"source": [
"x+32"
]
},
{
"cell_type": "code",
"execution_count": 164,
"metadata": {},
"outputs": [],
"source": [
"x = int(x)"
]
},
{
"cell_type": "code",
"execution_count": 165,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 165,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x)"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"42"
]
},
"execution_count": 167,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x+32"
]
},
{
"cell_type": "code",
"execution_count": 168,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 168,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(0)"
]
},
{
"cell_type": "code",
"execution_count": 169,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 169,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(1)"
]
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 170,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(\"\")"
]
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 171,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(\" \")"
]
},
{
"cell_type": "code",
"execution_count": 172,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 172,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(None)"
]
},
{
"cell_type": "code",
"execution_count": 173,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(\"False\")"
]
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 174,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(False)"
]
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'False'"
]
},
"execution_count": 175,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"str(False)"
]
},
{
"cell_type": "code",
"execution_count": 176,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 176,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(str(False))"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(False)"
]
},
{
"cell_type": "code",
"execution_count": 177,
"metadata": {},
"outputs": [
{
"ename": "ValueError",
"evalue": "invalid literal for int() with base 10: 'False'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[177], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mFalse\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: 'False'"
]
}
],
"source": [
"int('False')"
]
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 179,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(bool('False'))"
]
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 180,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(False)"
]
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 181,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int(True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## implicit coersion"
]
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10 is an integer, not a boolean. Why is this not an error?\n"
]
}
],
"source": [
"if 10:\n",
" print(\"10 is an integer, not a boolean. Why is this not an error?\")"
]
},
{
"cell_type": "code",
"execution_count": 183,
"metadata": {},
"outputs": [],
"source": [
"if 0:\n",
" print(\"why doesn't this print?\")"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"x = \"False\"\n",
"if x:\n",
" print(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [],
"source": [
"x = \"\"\n",
"if x:\n",
" print(\"hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python's `assert`"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [],
"source": [
"assert True"
]
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[187], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert False"
]
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(1)==True"
]
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [],
"source": [
"assert bool(1)==True"
]
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[190], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mbool\u001b[39m(\u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m==\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert bool(0)==True"
]
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "When I wrote this function, I assumed this would be otherwise.",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[191], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28mbool\u001b[39m(\u001b[38;5;241m0\u001b[39m)\u001b[38;5;241m==\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWhen I wrote this function, I assumed this would be otherwise.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
"\u001b[0;31mAssertionError\u001b[0m: When I wrote this function, I assumed this would be otherwise."
]
}
],
"source": [
"assert bool(0)==True, \"When I wrote this function, I assumed this would be otherwise.\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Blocks and control flow"
]
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"statement 1\n",
"statement 2\n",
"statement 3\n"
]
}
],
"source": [
"if True:\n",
" print(\"statement 1\")\n",
" print(\"statement 2\")\n",
" print(\"statement 3\")"
]
},
{
"cell_type": "code",
"execution_count": 194,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"here\n",
"a is one\n"
]
}
],
"source": [
"a = 1\n",
"b = -2\n",
"\n",
"if a==1:\n",
" if b>0:\n",
" print(\"a is one and b is positive\")\n",
" else:\n",
" print(\"here\")\n",
" print(\"a is one\")\n",
"else:\n",
" print(\"a is not one\")"
]
},
{
"cell_type": "code",
"execution_count": 195,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a is one\n",
"b is zero\n"
]
}
],
"source": [
"a = 1\n",
"b = -0.0\n",
"\n",
"if a==1:\n",
" if b>0:\n",
" print(\"a is one and b is positive\")\n",
" elif b<0:\n",
" print(\"a is one and b is negative\")\n",
" else:\n",
" print(\"a is one\")\n",
" print(\"b is zero\")\n",
"else:\n",
" print(\"a is not one\")"
]
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"-0.0"
]
},
"execution_count": 196,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
}
],
"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
}