Skip to content

Commit

Permalink
lecture 0.3 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vigji committed Mar 11, 2024
1 parent 101f98e commit d3f91ad
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 67 deletions.
139 changes: 75 additions & 64 deletions lectures/Lecture0.3_Functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
{
"cell_type": "markdown",
"id": "1a618701",
"id": "02f32aac",
"metadata": {
"slideshow": {
"slide_type": "subslide"
Expand All @@ -36,7 +36,7 @@
},
{
"cell_type": "markdown",
"id": "a0e2798c",
"id": "bafdd45d",
"metadata": {
"slideshow": {
"slide_type": "subslide"
Expand All @@ -48,7 +48,7 @@
},
{
"cell_type": "markdown",
"id": "ac00363e",
"id": "63fb8c22",
"metadata": {},
"source": [
"If a variable is `None` no value is assigned to it!"
Expand All @@ -57,7 +57,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "adb665b4",
"id": "0f22be20",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -67,7 +67,7 @@
},
{
"cell_type": "markdown",
"id": "b8956d3d",
"id": "794af21b",
"metadata": {
"slideshow": {
"slide_type": "subslide"
Expand All @@ -80,7 +80,7 @@
{
"cell_type": "code",
"execution_count": 22,
"id": "3d29fc8b",
"id": "c016a966",
"metadata": {},
"outputs": [
{
Expand All @@ -102,7 +102,7 @@
{
"cell_type": "code",
"execution_count": 23,
"id": "756ceeba",
"id": "dcfeef9d",
"metadata": {},
"outputs": [
{
Expand All @@ -122,7 +122,7 @@
},
{
"cell_type": "markdown",
"id": "5e7da0db",
"id": "b0da8ad3",
"metadata": {
"slideshow": {
"slide_type": "subslide"
Expand All @@ -134,7 +134,7 @@
},
{
"cell_type": "markdown",
"id": "e9139877",
"id": "79a7ce4c",
"metadata": {
"slideshow": {
"slide_type": "-"
Expand All @@ -147,7 +147,7 @@
{
"cell_type": "code",
"execution_count": 6,
"id": "f6cea7d9",
"id": "2ff29b35",
"metadata": {
"slideshow": {
"slide_type": "subslide"
Expand All @@ -161,7 +161,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "f205fd11",
"id": "9320ffa4",
"metadata": {},
"outputs": [
{
Expand All @@ -182,7 +182,7 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "306d5fcc",
"id": "b02611dc",
"metadata": {},
"outputs": [
{
Expand All @@ -203,7 +203,7 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "f3b8ddee",
"id": "695e8344",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -238,11 +238,7 @@
"id": "b358df84",
"metadata": {},
"source": [
"With `while` we can keep repeating code until one condition is met.\n",
"\n",
"Sometimes we can use alternatively `for` or `while`, but in general:\n",
" - we use `for` when we know in advance the number of iterations\n",
" - we use `while` if we do not know number of iterations beforehand\n"
"With `while` we can keep repeating code until one condition is met."
]
},
{
Expand Down Expand Up @@ -311,6 +307,16 @@
" print(\"Flip: \", coin_flip)"
]
},
{
"cell_type": "markdown",
"id": "ef8e473c",
"metadata": {},
"source": [
"Sometimes we can use alternatively `for` or `while`, but in general:\n",
" - we use `for` when we know in advance the number of iterations\n",
" - we use `while` if we do not know number of iterations beforehand"
]
},
{
"cell_type": "markdown",
"id": "e3fbba40",
Expand Down Expand Up @@ -988,7 +994,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 16,
"id": "fee83430",
"metadata": {
"slideshow": {
Expand All @@ -1000,8 +1006,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Input: 2; result: 4\n",
"Input: 2; result: 22\n"
"Input: 2 (<class 'int'>); result: 4\n",
"Input: 2.0 (<class 'float'>); result: 4.0\n",
"Input: 2 (<class 'str'>); result: 22\n"
]
}
],
Expand All @@ -1012,8 +1019,8 @@
" return input_number * 2\n",
"\n",
"# Test different inputs to the function:\n",
"for input_to_test in [2, \"2\"]:\n",
" print(f\"Input: {input_to_test}; result: {multiply_numbers(input_to_test)}\")"
"for input_to_test in [2, 2., \"2\"]:\n",
" print(f\"Input: {input_to_test} ({type(input_to_test)}); result: {multiply_numbers(input_to_test)}\")"
]
},
{
Expand Down Expand Up @@ -1098,44 +1105,6 @@
"In this way, smart IDEs can tell us when we code if there is something strange, and type checkers can be run on code to make sure functions are called with the correct inputs."
]
},
{
"cell_type": "markdown",
"id": "b0d97d17",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "dec5a722",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# Imported functions"
]
},
{
"cell_type": "markdown",
"id": "68c62379",
"metadata": {},
"source": [
"Many times you will import functions from external modules, like `random`. In those cases, you can be sure that the functions docs give you all the information you need about how to use the function:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8c383931",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"?random.randint"
]
},
{
"cell_type": "markdown",
"id": "fbf8fc87",
Expand Down Expand Up @@ -1366,16 +1335,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "e648b1c4",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Object `exponentiate` not found.\n"
]
}
],
"source": [
"# We can read out documentation also for our custom functions:\n",
"# We can read out documentation for our custom functions:\n",
"?exponentiate"
]
},
Expand Down Expand Up @@ -1486,6 +1463,8 @@
}
},
"source": [
"## Side effects\n",
"\n",
"**Avoid sides effects!** Do not modify the passed values or variables existing ouside the function scope. In the cases you do it, do it consciously and make it very explicit in the name of the function or in the docstring!"
]
},
Expand Down Expand Up @@ -1518,6 +1497,38 @@
"print(a_list)"
]
},
{
"cell_type": "markdown",
"id": "e8b536d5",
"metadata": {
"slideshow": {
"slide_type": "subslide"
}
},
"source": [
"# Imported functions"
]
},
{
"cell_type": "markdown",
"id": "bedcbdf1",
"metadata": {},
"source": [
"Many times you will import functions from external modules, like `random`. the functions docs give you all the information you need about how to use the function:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "486b2f25",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"?random.randint"
]
},
{
"cell_type": "markdown",
"id": "24911992",
Expand Down
7 changes: 4 additions & 3 deletions practicals/Practicals_0.3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"source": [
"# Practicals for lecture 0.3\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vigji/python-cimec/blob/main/practicals/Practicals_0.3.ipynb)"
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/vigji/python-cimec-2024/blob/main/practicals/Practicals_0.3.ipynb)"
]
},
{
Expand All @@ -33,8 +33,9 @@
"metadata": {},
"outputs": [],
"source": [
"# Write a while loop over integer numbers that terminates \n",
"# when you encounter a multiple of both 10 and 12:\n",
"# Write a while loop over integer numbers incrementing the i \n",
"# variably by one at every iteration, and stop when i\n",
"# is a multiple of both 10 and 12:\n",
"\n",
"\n",
"\n"
Expand Down

0 comments on commit d3f91ad

Please sign in to comment.