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

3300 lines
160 KiB
Plaintext
Raw Normal View History

2024-10-25 02:05:31 -04:00
{
"cells": [
2024-10-25 05:40:27 -04:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Important reminder\n",
"\n",
"No lecture on 1 Nov. 2024 due to holiday.\n",
"\n",
"Lecture will be at start of tutorial on 4 Nov. 2024 (at the normal tutorial time and loaction, namely 14:15 in the Computer Pool in Bio 2/3 building.)\n",
"\n",
"# File naming in assignments\n",
"\n",
"Please use original name and overwrite original file."
]
},
2024-10-25 02:05:31 -04:00
{
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 28,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = 0\n",
"\n",
"def foo(a):\n",
" return a\n",
"\n",
"z = foo(x)"
]
},
{
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 29,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 29,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 30,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 30,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(10)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 31,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 31,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10.0"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 32,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 32,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(10.0)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 33,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 33,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 10\n",
"type(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 34,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 34,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(\"10\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 35,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 35,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type('10')"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 36,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"my_string = \"Hello, My name is Angela Merkel\""
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 37,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
2024-10-25 05:40:27 -04:00
"my_string = \"übersetzen😀\""
2024-10-25 02:05:31 -04:00
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 38,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"Übersetzung = \"translation\""
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 39,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-10-25 05:40:27 -04:00
"übersetzen😀\n"
2024-10-25 02:05:31 -04:00
]
}
],
"source": [
"print(my_string)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 40,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 41,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (1042171736.py, line 1)",
"output_type": "error",
"traceback": [
2024-10-25 05:40:27 -04:00
"\u001b[0;36m Cell \u001b[0;32mIn[41], 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"
2024-10-25 02:05:31 -04:00
]
}
],
"source": [
"my_string = \"the data is like this: \"gcatcccggg\"\"\n",
"print(my_string)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 42,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 43,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 44,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 45,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (57043761.py, line 1)",
"output_type": "error",
"traceback": [
2024-10-25 05:40:27 -04:00
"\u001b[0;36m Cell \u001b[0;32mIn[45], 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"
2024-10-25 02:05:31 -04:00
]
}
],
"source": [
"my_string = 'the data is like this: 'gcatcccggg''\n",
"print(my_string)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 46,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 47,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 48,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x=10"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 49,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 50,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1234"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 50,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1234"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 51,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"def myprint(x):\n",
" print(x)\n",
" return 32"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 52,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
},
{
"data": {
"text/plain": [
"32"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 52,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"myprint(\"hello\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 53,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"myprint(\"hello\");"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"z=myprint(\"hello\")"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z"
]
},
{
"cell_type": "code",
"execution_count": 56,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 56,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 57,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"# In Jupyter, `_` is a special variable, which means the output value of the previously run cell.\n",
"y=_"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 58,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 58,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 59,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x=x+1"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 60,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 61,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"z=x=x+1"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 62,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 62,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"z"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 63,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 63,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 64,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"x=None\n",
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 66,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"NoneType"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 66,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=None\n",
"type(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 67,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"None"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 68,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 68,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 69,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"None\n"
]
}
],
"source": [
"x=print(10)\n",
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 70,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 71,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x=10"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 72,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 72,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 73,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 73,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3+4"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 74,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 74,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"int.__add__(3,4)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 75,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'abcdef'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 75,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"abc\" + \"def\""
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 76,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [
{
"data": {
"text/plain": [
"'abcdef'"
]
},
"execution_count": 76,
"metadata": {},
"output_type": "execute_result"
}
],
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 77,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 77,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"4 * 3 + 2"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 78,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 78,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 + 4 * 3"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 79,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = 4 * 3 + 2"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 80,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 80,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 81,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = x * 2"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 82,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"28"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 82,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 83,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 83,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y=4*3+2\n",
"y"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 84,
2024-10-25 02:05:31 -04:00
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 84,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp = 4*3\n",
"y = tmp+2\n",
"y"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 99,
2024-10-25 02:05:31 -04:00
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"tmp = 'my super import data'\n",
"\n",
"y=4*(3+2)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 100,
2024-10-25 02:05:31 -04:00
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 100,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 101,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'my super import data'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 101,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tmp"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# list type\n",
"\n",
"## list construction"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 102,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 103,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"x=list()\n",
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 104,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 107,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 108,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'green', 'blue']\n"
]
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 109,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 110,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 111,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'red'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 111,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[0]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 112,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'green'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 112,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[1]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 113,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 113,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[2]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 114,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 114,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"green\",\"blue\"]\n",
"x[-1]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 115,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'violet'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 115,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[-1]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 116,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 116,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[-3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 117,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 117,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 118,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'yellow'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 118,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"y=2\n",
"x[y]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 119,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[119], 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",
2024-10-25 02:05:31 -04:00
"\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not str"
]
}
],
"source": [
"x['hello']"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 120,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[120], 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",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": 121,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 121,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x.index(\"blue\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 122,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[122], 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",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": 123,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 123,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 124,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x[3]=3"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 125,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 125,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## list slicing"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 126,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 126,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[0:3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 127,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'green'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 127,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 128,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 128,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 129,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 129,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[None:3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 130,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['red', 'orange', 'yellow']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 130,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[0:3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 131,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo', 'violet']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 131,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[3:]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 132,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo', 'violet']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 132,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=[\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\"]\n",
"x[3:None]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 133,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['green', 'blue', 'indigo']"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 133,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 136,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 137,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"()\n"
]
}
],
"source": [
"x = ()\n",
"print(type(x))\n",
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 138,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 139,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 140,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 141,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 142,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 143,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 144,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 145,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 146,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[146], 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",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": 147,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 148,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'yellow'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 148,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[2]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 149,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'violet'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 149,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[-1]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 150,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'blue'"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 150,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[-3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 151,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('red', 'orange', 'yellow')"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 151,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x=(\"red\",\"orange\",\"yellow\",\"green\",\"blue\",\"indigo\",\"violet\")\n",
"x[0:3]"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 152,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('red', 'orange', 'yellow')"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 152,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## lists are *mutable*, tuples are not"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 153,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 154,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[154], 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",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": 155,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 159,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-10-25 05:40:27 -04:00
"10"
2024-10-25 02:05:31 -04:00
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 159,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"None\n"
]
}
],
"source": [
"def modify_arg(a,b):\n",
"\t a = a + b\n",
"\t # Notice that there is no return here!\n",
"\t # So, this function has an important \"side effect\",\n",
"\t # but the output is None.\n",
"\t \n",
"x = 10\n",
"y = modify_arg(x, 4)\n",
"x\n",
"print(y)"
]
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 161,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 163,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 164,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['red', 'orange', 'yellow', 3, 'blue', 'indigo', 'violet']\n"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 165,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 166,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 167,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 168,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 174,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
2024-10-25 05:40:27 -04:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABZwklEQVR4nO3deVjUVdsH8O+wu+KOmLibay5hKiaWYZj2+mhptqrtkZoLWQZlmxXtmpkLZT6ZmVZIWVphKbhrKKa5kCkKKoiksqmsv/eP+4FhFHBmmJkzy/dzXXPpHM7M3OMPmZuz3EenaZoGIiIiIkXcVAdAREREro3JCBERESnFZISIiIiUYjJCRERESjEZISIiIqWYjBAREZFSTEaIiIhIKSYjREREpJSH6gCMUVpaitOnT6NevXrQ6XSqwyEiIiIjaJqG3NxctGjRAm5uVY9/OEQycvr0aQQEBKgOg4iIiMyQlpaGli1bVvl1h0hG6tWrB0DeTP369RVHQ0RERMbIyclBQEBA+ed4VRwiGSmbmqlfvz6TESIiIgdzrSUWXMBKRERESjEZISIiIqWYjBAREZFSTEaIiIhIKSYjREREpBSTESIiIlKKyQgREREpxWSEiIiIlHKIomdE5KRKSoDNm4H0dMDfHwgOBtzdVUdFRDZWo5GRqKgo6HQ6TJs2rdp+CQkJCAwMhI+PD9q1a4dFixbV5GWJyBmsXg20aQMMHgw88ID82aaNtBORSzE7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qlZaqjoCIjMRkhIiMc/asLBB9/nmgpER1NFU7cAC4805g6FDVkRCRkThNQ0TGiY0F/vkH+P1
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"plt.plot(x,y,\"ro:\");"
2024-10-25 02:05:31 -04:00
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 175,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAAB1t0lEQVR4nO3deVxU9f7H8dcs7KuCLCoq7guiuKPQZmmCpu27tlhZKpa3e2/W3bv9vPf+uv0STc3S9tIKMxU0rVzAHUXFDXdBZRGVHWaYmfP7A4a0UAGBMzN8no/HPB4xnsN8zuPQzGfO+3y/X42iKApCCCGEECrRql2AEEIIIVo2aUaEEEIIoSppRoQQQgihKmlGhBBCCKEqaUaEEEIIoSppRoQQQgihKmlGhBBCCKEqaUaEEEIIoSq92gXUhcVi4fz583h5eaHRaNQuRwghhBB1oCgKxcXFtG3bFq322tc/7KIZOX/+PCEhIWqXIYQQQogGyMrKon379tf8d7toRry8vICqg/H29la5GiGEEELURVFRESEhITWf49diF82INZrx9vaWZkQIIYSwMze6xUJuYBVCCCGEqqQZEUIIIYSqpBkRQgghhKqkGRFCCCGEqqQZEUIIIYSqpBkRQgghhKqkGRFCCCGEqqQZEUIIIYSqpBkRQgghhKpuqhmZPXs2Go2Gl19++brbbdq0iYEDB+Lq6krnzp1ZuHDhzbysEEIIIRxIg5uRXbt2sWjRI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1jVUNcbYIQQzSth91l+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",
2024-10-25 05:40:27 -04:00
"execution_count": 176,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAiMAAAGdCAYAAADAAnMpAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjguMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy81sbWrAAAACXBIWXMAAA9hAAAPYQGoP6dpAABXDElEQVR4nO3deXCU97kv+O/bu5bu1r4hAVpArAIhDAgkGwcbAhiD45nJvZOxc0/iTFEx8XEoxndwqnJz46RIzXFliMvHJr6x43MmldiTwQgcMDE5NvtiFglkNktCICG1dqlba6/v/NF6GwSSUEvd/Xa//f1UdSVqv61+JKHW07/f83seQRRFEUREREQyUckdABEREUU3JiNEREQkKyYjREREJCsmI0RERCQrJiNEREQkKyYjREREJCsmI0RERCQrJiNEREQkK43cAUyEx+NBc3MzjEYjBEGQOxwiIiKaAFEU0dvbi6ysLKhUY69/REQy0tzcjJycHLnDICIioklobGxEdnb2mP89IpIRo9EIwPvFmEwmmaMhIiKiibDZbMjJyfH9HR9LRCQj0taMyWRiMkJERBRhHlViwQJWIiIikhWTESIiIpIVkxEiIiKSFZMRIiIikhWTESIiIpIVkxEiIiKSFZMRIiIikhWTESIiIpIVkxEiIiKS1ZSSkV27dkEQBLz66qvjXnfs2DGUlJTAYDAgLy8Pe/bsmcrTEhERkYJMOhk5f/483nvvPRQVFY17XX19PTZs2IDy8nJUVlbi9ddfxyuvvIK9e/dO9qmJiIhIQSaVjPT19eF73/se/sf/+B9ITEwc99o9e/Zg+vTp2L17N+bOnYuXXnoJP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"
]
},
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 177,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 177,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"y = x > 0\n",
"y"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 178,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 178,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(y)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 180,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
2024-10-25 05:40:27 -04:00
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is positive\n"
]
},
2024-10-25 02:05:31 -04:00
{
"data": {
"text/plain": [
2024-10-25 05:40:27 -04:00
"10"
2024-10-25 02:05:31 -04:00
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 180,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-10-25 05:40:27 -04:00
"x = 1\n",
2024-10-25 02:05:31 -04:00
"\n",
"if x > 0:\n",
" print(\"x is positive\")\n",
" x = 10\n",
"\n",
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 181,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-10-25 05:40:27 -04:00
"True"
2024-10-25 02:05:31 -04:00
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 181,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x>0"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 182,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"bool"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 182,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x>0)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 183,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 184,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 184,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 > 4"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 185,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 185,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3 == 4"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 186,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 186,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+2 == 4"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 188,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-10-25 05:40:27 -04:00
"3"
2024-10-25 02:05:31 -04:00
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 188,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-10-25 05:40:27 -04:00
"2+(2 == 2)"
2024-10-25 02:05:31 -04:00
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 189,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 189,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+(False)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 190,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 190,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2+False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coercion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## explicit coersion"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = \"10\""
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"type(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"x+32"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = int(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"x"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"type(x)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"x+32"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 192,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
2024-10-25 05:40:27 -04:00
"True"
2024-10-25 02:05:31 -04:00
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 192,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
2024-10-25 05:40:27 -04:00
"bool(10)"
2024-10-25 02:05:31 -04:00
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(1)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(\"\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(\" \")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(None)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(\"False\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(False)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"str(False)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"bool(str(False))"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"int(False)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"int('False')"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"int(bool('False'))"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"int(False)"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"source": [
"int(True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## implicit coersion"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 193,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 194,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"if 0:\n",
" print(\"why doesn't this print?\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 195,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [
"x = \"False\"\n",
"if x:\n",
" print(\"hello\")"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 196,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"x = \"\"\n",
"if x:\n",
" print(\"hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python's `assert`"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 197,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"assert True"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 198,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[198], 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",
2024-10-25 02:05:31 -04:00
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert False"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 199,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
2024-10-25 05:40:27 -04:00
"execution_count": 199,
2024-10-25 02:05:31 -04:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bool(1)==True"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 200,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [],
"source": [
"assert bool(1)==True"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 201,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[201], 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",
2024-10-25 02:05:31 -04:00
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"assert bool(0)==True"
]
},
{
"cell_type": "code",
2024-10-25 05:40:27 -04:00
"execution_count": 202,
2024-10-25 02:05:31 -04:00
"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)",
2024-10-25 05:40:27 -04:00
"Cell \u001b[0;32mIn[202], 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",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": 203,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 206,
2024-10-25 02:05:31 -04:00
"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",
2024-10-25 05:40:27 -04:00
"execution_count": 208,
2024-10-25 02:05:31 -04:00
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2024-10-25 05:40:27 -04:00
"a is one and b is negative\n"
2024-10-25 02:05:31 -04:00
]
}
],
"source": [
"a = 1\n",
2024-10-25 05:40:27 -04:00
"b = -1.0\n",
2024-10-25 02:05:31 -04:00
"\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",
2024-10-25 05:40:27 -04:00
"execution_count": null,
2024-10-25 02:05:31 -04:00
"metadata": {},
2024-10-25 05:40:27 -04:00
"outputs": [],
2024-10-25 02:05:31 -04:00
"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
}