From 9212434074c7f537aca0319b2de3901965ee5d1f Mon Sep 17 00:00:00 2001
From: Auke Klazema <klazema@astron.nl>
Date: Wed, 24 Nov 2021 15:26:25 +0100
Subject: [PATCH] Small changes after giving the course.

---
 IntroProgrammingInPython.ipynb | 160 ++++++++++++++++++++++-----------
 1 file changed, 110 insertions(+), 50 deletions(-)

diff --git a/IntroProgrammingInPython.ipynb b/IntroProgrammingInPython.ipynb
index af32bc4..6f3c6ed 100644
--- a/IntroProgrammingInPython.ipynb
+++ b/IntroProgrammingInPython.ipynb
@@ -302,8 +302,6 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "#!/bin/env python3\n",
-    "\n",
     "print(\"Hello World\")"
    ]
   },
@@ -615,7 +613,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "1 // 2"
+    "5.2 // 2"
    ]
   },
   {
@@ -989,9 +987,10 @@
    "source": [
     "# multiple positional parameters\n",
     "def important_function(param_one, param_two):\n",
-    "    pass\n",
+    "    print(param_one)\n",
+    "    print(param_two)\n",
     "\n",
-    "important_function(1, 2)"
+    "important_function(4, 3)"
    ]
   },
   {
@@ -1003,9 +1002,10 @@
    "source": [
     "# using keyword arguments, order can be different than with original parameter list.\n",
     "def important_function(param_one, param_two):\n",
-    "    pass\n",
+    "    print(param_one)\n",
+    "    print(param_two)\n",
     "\n",
-    "important_function(param_two=2, param_one=1)"
+    "important_function(param_two=4, param_one=3)"
    ]
   },
   {
@@ -1024,13 +1024,13 @@
    "outputs": [],
    "source": [
     "# default values can be used to make parameters optional\n",
-    "def function(name, greeting=\"Hello\"):\n",
-    "    print(greeting, end=\" \")\n",
+    "def greeting(name, param=1, greeting=\"Hello\"):\n",
+    "    print(greeting * param, end=\" \")\n",
     "    print(name)\n",
     "\n",
-    "function(\"World\", \"Hi\")\n",
-    "function(\"World\")\n",
-    "function(\"World\", greeting=\"Yo\")"
+    "greeting(\"World\", 1, \"Hi\")\n",
+    "greeting(\"World\")\n",
+    "greeting(\"World\", greeting=\"Yo\")"
    ]
   },
   {
@@ -1049,9 +1049,10 @@
    "outputs": [],
    "source": [
     "def square(number):\n",
-    "    return number * number\n",
+    "    result = number * number\n",
+    "    return result\n",
     "\n",
-    "square(3)"
+    "print(square(4))"
    ]
   },
   {
@@ -1090,7 +1091,8 @@
     "    \n",
     "    # prints the local space name\n",
     "    print(name)\n",
-    "    \n",
+    "\n",
+    "print(name)\n",
     "print_something()\n",
     "# prints the global space name\n",
     "print(name)"
@@ -1131,7 +1133,8 @@
     "    name = \"Lofar\"\n",
     "    \n",
     "    print(name)\n",
-    "    \n",
+    "\n",
+    "print(name)\n",
     "print_something()\n",
     "print(name)"
    ]
@@ -1190,7 +1193,7 @@
     "# Tripple double or single quoted\n",
     "print(\"\"\"Hello\n",
     "World\"\"\")\n",
-    "print('''Hello\n",
+    "print('''Hello \n",
     "World''')"
    ]
   },
@@ -1273,7 +1276,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "len(\"Hello\")"
+    "value = \"hello\"\n",
+    "len(value)"
    ]
   },
   {
@@ -1292,7 +1296,7 @@
    "outputs": [],
    "source": [
     "# This gives the first character\n",
-    "\"Hello World\"[0]"
+    "\"Hello World\"[1]"
    ]
   },
   {
@@ -1321,7 +1325,8 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "\"Hello World\"[1:3]"
+    "val = \"Hello World\"\n",
+    "print(val[0:1] + val[3:])"
    ]
   },
   {
@@ -1375,6 +1380,17 @@
     "\"Hello World\"[:]"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "d84c76f0-b45b-4ea5-9037-50cd5e906590",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "val = \"Hello\"\n",
+    "val[3] = \"T\""
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "da426233-05e1-4b6c-a74d-d8e8221862d9",
@@ -1470,7 +1486,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "help(int)"
+    "help(\"\")"
    ]
   },
   {
@@ -1526,7 +1542,11 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "hello = \"Hello\" + \" \" + \"World\"\n",
+    "var_1 = \"Hello\"\n",
+    "var_2 = \" \"\n",
+    "var_3 = \"World\"\n",
+    "hello = var_1 + var_2 + var_3\n",
+    "\n",
     "print(hello)"
    ]
   },
@@ -1578,9 +1598,9 @@
    "source": [
     "# Here we want to use the variable directly in the formatted string.\n",
     "# Python will put the string representative of in the string.\n",
-    "name = \"World\"\n",
+    "name = \"NOVA\"\n",
     "age = 4.6E+9\n",
-    "f\"Hello {name} you don't look a day older than {age} years\""
+    "print(f\"Hello {name} you don't look a day older than {age} years\")"
    ]
   },
   {
@@ -1593,7 +1613,7 @@
     "# We can also provide hints on how to format things.\n",
     "name = \"World\"\n",
     "age = 4600000000\n",
-    "f\"Hello {name} you don't look a day older than {age:.1E} years\""
+    "print(f\"Hello {name} you don't look a day older than {age:.1E} years\")"
    ]
   },
   {
@@ -1654,6 +1674,21 @@
     "\"Hello {0}, my name is {1}. Have a nice day {0}\".format(\"World\", \"Python\")"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "ea89ae8c-f916-4548-bd7b-0d76aa561712",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# You can also store a string that still needs to be formated in a variable\n",
+    "preformated_string = \"Hello {name}. What would you like to do today?\"\n",
+    "\n",
+    "result = preformated_string.format(name=\"Coco\")\n",
+    "\n",
+    "print(result)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "2293f47e-eacb-4132-9a5f-a056d62ff93b",
@@ -1669,7 +1704,7 @@
    "source": [
     "### Exercise\n",
     "\n",
-    "Write a program where you ask for some input from the user with the help of input. And then display the user input into a formatted string using the three given options of creating new strings."
+    "Write a program where you ask for some input from the user with the help of input. And then display the user input into a formatted string using the three ways of creating new strings. Formatted strings, .format() and string concatenation \"+\"."
    ]
   },
   {
@@ -1714,7 +1749,7 @@
    "outputs": [],
    "source": [
     "# This is an empty list\n",
-    "[]"
+    "print([])"
    ]
   },
   {
@@ -1828,7 +1863,10 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "[1, 2, 3] + [4, 5]"
+    "bla = [1, 2, 3]\n",
+    "bli = [4, 5]\n",
+    "ble = bla + bli\n",
+    "print(ble)"
    ]
   },
   {
@@ -1836,7 +1874,7 @@
    "id": "df929be9-697d-4360-8f5b-70a6ca400518",
    "metadata": {},
    "source": [
-    "What we could not so with strings we can do with lists. We can alter the contents of the list."
+    "What we could not so with strings we can do with lists. We can alter the contents of a list."
    ]
   },
   {
@@ -1849,7 +1887,10 @@
     "items = [1, 2, 3]\n",
     "print(items)\n",
     "items[2] = 88\n",
-    "print(items)"
+    "print(items)\n",
+    "\n",
+    "items = [\"a\", \"b\", \"c\"]\n",
+    "items[1] = \"Z\""
    ]
   },
   {
@@ -1911,7 +1952,9 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "items = [[1, 2, 3], [4, 5]]\n",
+    "items = [[1, 2, 3],\n",
+    "         [4, 5],\n",
+    "         [3, 5]]\n",
     "print(items)\n",
     "print(items[0])\n",
     "print(items[0][1])"
@@ -2165,7 +2208,8 @@
    "outputs": [],
    "source": [
     "net_code = {'Dwingeloo': '0521', 'Assen': '0592'}\n",
-    "print(net_code)"
+    "print(net_code)\n",
+    "print(net_code[\"Assen\"])"
    ]
   },
   {
@@ -2198,6 +2242,17 @@
     "print(net_code.values())"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "id": "157297d1-36a4-4601-bd5e-a69aacb22476",
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "dictionary = {1: \"one\", 2: \"two\", 4: \"three\"}\n",
+    "print(dictionary[4])"
+   ]
+  },
   {
    "cell_type": "markdown",
    "id": "d5ec52d1-7e41-4439-b4a8-a46caf0de825",
@@ -2271,8 +2326,10 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "if True:\n",
-    "    print(\"Hello\")"
+    "user_name = input(\"What is your name?\")\n",
+    "\n",
+    "if len(user_name) > 4:\n",
+    "    print(\"Greeting,\", user_name)"
    ]
   },
   {
@@ -2301,7 +2358,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "if False:\n",
+    "if True:\n",
     "    print(\"Hello\")\n",
     "else:\n",
     "    print(\"World\")"
@@ -2322,9 +2379,11 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "if False:\n",
+    "value = 10\n",
+    "\n",
+    "if not value == 1:\n",
     "    print(\"Hello\")\n",
-    "elif False:\n",
+    "elif value == 2:\n",
     "    print(\"World\")\n",
     "else:\n",
     "    print(\"Bye\")"
@@ -2419,7 +2478,7 @@
     "    print('Negative number')\n",
     "elif x == 0:\n",
     "    print('Zero')\n",
-    "else:\n",
+    "elif:\n",
     "    print('Positive number')"
    ]
   },
@@ -2466,10 +2525,10 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "value = 0\n",
-    "while value < 100:\n",
+    "value = 5\n",
+    "while value < 49:\n",
     "    print(value, end=\", \")\n",
-    "    value = value + value + 1"
+    "    value = value + 1"
    ]
   },
   {
@@ -2576,7 +2635,7 @@
    "source": [
     "greeting = \"Hello World\"\n",
     "\n",
-    "for i in range(0, len(greeting)):\n",
+    "for i in range(3, len(greeting)):\n",
     "    print(greeting[i])"
    ]
   },
@@ -2649,9 +2708,9 @@
     "for item in (\"AMOLF\", \"ARCNL\", \"ASTRON\", \"CWI\", \"DIFFER\", \"Nikhef\", \"NIOZ\", \"NSCR\", \"SRON\"):\n",
     "    if item.endswith('R'):\n",
     "        print(\"Ah here it is. It is an\", item)\n",
-    "        break;\n",
-    "\n",
-    "    print(\"Hmm its not\", item)"
+    "        break\n",
+    "    else:\n",
+    "        print(\"Hmm its not\", item)"
    ]
   },
   {
@@ -2671,7 +2730,7 @@
    "source": [
     "print(\"I am looking for something\")\n",
     "\n",
-    "for item in (\"AMOLF\", \"ARCNL\", \"ASTRON\", \"CWI\", \"DIFFER\", \"Nikhef\", \"NIOZ\", \"NSCR\", \"SRON\"):\n",
+    "for item in (\"AMOLF\", \"ARCNL\", \"ASTRON\", \"CWI\", \"DIFFER\", \"Nikhef\", \"NIOZ\", \"NSCR\", \"SRON\"):    \n",
     "    if 'A' in item:\n",
     "        continue\n",
     "\n",
@@ -2757,13 +2816,13 @@
    "source": [
     "## How to solve problems with programming\n",
     "\n",
-    "In one small sentence its about breaking up the problem into pieces that can be solved and then composing the small solutions to bigger solutions until the main problem is solved. Easy right? Not really.\n",
+    "In one small sentence: its about breaking up the problem into pieces that can be solved and then composing the small solutions to bigger solutions until the main problem is solved. Easy right? Not really.\n",
     "\n",
-    "Some problems can be solved by connecting existing solutions in libraries together. So for example I need to collect all the log files from all the servers running in Dwingeloo. There are libraries that can copy files from machines using SSH. So now we only need to know which servers are running in Dwingeloo. This could be a simple list we hard code. In that case we are almost done with a small loop over a list. We might need to think about when things go wrong. Server could be down. Network could have a glitch. The file might be locked. We could decide to ignore it because we run the script often enough and we don't mind a older file. But maybe we do care and we need to record any issues. Is it part of an automated system or can we report to a human that then can take action?\n",
+    "Some problems can be solved by connecting existing solutions in libraries together. So for example I need to collect all the log files from all the servers running in Dwingeloo. There are libraries that can copy files from machines using SSH. So now we only need to know which servers are running in Dwingeloo. This could be a simple list we hard code. In that case we are almost done with a small loop over a list. We might need to think about when things go wrong. Server could be down. Network could have a glitch. The file might be locked. We could decide to ignore all these issues because we run the script often enough and we don't mind a older file. But maybe we do care and we need to record any of these issues. Is it part of an automated system or can we report to a human that then can take action?\n",
     "\n",
     "Some problems are more of the type that does not have a library for it. Then we might need to implement our own algorithm or set of more detailed functions.\n",
     "\n",
-    "Lets have a look at a relative simple problem of converting a string representing a binary number into a string that represents a hexadecimal number. We allow negative numbers. We raise an exception on empty strings.\n",
+    "Lets have a look at a relative simple problem of converting a string representing a binary number into a string that represents a hexadecimal number. We allow negative numbers. But we raise an exception on empty strings.\n",
     "\n",
     "https://github.com/TheAlgorithms/Python/blob/master/conversions/binary_to_hexadecimal.py"
    ]
@@ -2858,7 +2917,8 @@
     "* Reddit /r/python\n",
     "* Reddit /r/AskProgramming\n",
     "* Reddit /r/learnpython\n",
-    "* Reddit /r/dailyprogrammer/"
+    "* Reddit /r/dailyprogrammer/\n",
+    "* Ebooks https://www.humblebundle.com"
    ]
   },
   {
-- 
GitLab