diff --git a/IntroProgrammingInPython.ipynb b/IntroProgrammingInPython.ipynb index 3c2fba836749f8573bb98d34fe09fbcb9a98aea1..be52844bfcfa60abe017fe23a9041f447c6a9a71 100644 --- a/IntroProgrammingInPython.ipynb +++ b/IntroProgrammingInPython.ipynb @@ -23,11 +23,11 @@ "source": [ "## Introduction\n", "\n", - "The rules of computation are simple to explain, but takes years to master. Similar to the game of Chess or Backgammon. We have 2.5 days together to get the basic concepts explained and understood. It will be a first step in you yourney as a programmer. How far you take the journey is up to you.\n", + "The rules of computation are simple to explain, but it takes many years to master. Similar to the game of Chess or Backgammon. We have 2.5 days together to get the basic concepts explained and understood. It will be a first step in you journey as a programmer. How far you take the journey is up to you.\n", "\n", "The limit of 2.5 days will mean that, just with your first lessons in say mathematics, I will have to leave things out. For example I will not be able to cover Object Oriented Programming or Functional Programming.\n", "\n", - "After these days you will know how to program in a basic way that will already be helpfull in automating your life. And just with running you can call yourself a runner if you run, you will be a programmer when you program." + "After finishing these days you will know how to program in a basic way, that will already be helpful in automating your life. And just as with running, where you can call yourself a runner if you run, you can call yourself a programmer when you program. Lets start." ] }, { @@ -44,11 +44,11 @@ "\n", "Programming is solving problems with computations. Its describing a process in an accurate way with discrete steps.\n", "\n", - "So that sound really vague. So let me try to clear it up a bit. A computer exists of a Central Processing Unit (CPU) that can perfom certain task. It also has some form of temporary storage (memory). And to be usefull it has some interfaces to the outside world. The CPU performs tasks on memory, like adding up two numbers and storing the result in memory.\n", + "So that sound really vague. So let me try to clear it up a bit. A computer exists of a Central Processing Unit (CPU) that can perform certain task. It also has some form of temporary storage (memory). And to be useful it also has some interfaces to the outside world, like a monitor or a keyboard interface. The CPU performs tasks on memory, like adding up two numbers and storing the result in memory.\n", "\n", - "These individual computational tasks can be combined to solve all sorts of problems. The task of the programmer is to find a combination of task to solve a problem. If one of taks is missing or performed in a wrong order the program is incorrect. So one has te be very precise.\n", + "These individual computational tasks can be combined to solve all sorts of problems. The task of the programmer is to find a combination of task that will solve a problem. If one of tasks is missing or performed in a wrong order the program is incorrect. So one has to be very precise.\n", "\n", - "The discipline surrounding the actual programming is usually called software engineering. There are many ways to go about it and the discipline is still young so there still no clear cut way of doing things. But it all has to deal with combining small steps that describe a process." + "The discipline surrounding the actual programming is usually called software engineering. There are many ways to go about it and the discipline is still young so there still is no clear cut way of doing things. But it all has to deal with combining small steps that describe a process." ] }, { @@ -71,17 +71,19 @@ "source": [ "The CPU takes in information from a form of memory. This could be a harddrive, USB thumbdrive, SD card, etc. And it tries to decode the information into instructions it can perform. Many instructions sets start with an operational code (op-code) and then continue with decoding its parameters if it has any. These are just numbers represented by 0s and 1s.\n", "\n", - "A computer can be programmed by providing a long list of numbers. In the past some computers were programmed by toggeling switches to represent numbers and a nother switch to tell the computer to process that number. Luckely some programmers decided they could write programs to help translate the sequence of instructions into numbers. And this started the creation of many programming languages.\n", + "A computer can be programmed by providing a long list of numbers. In the past some computers were programmed by toggling switches to represent numbers and another switch to tell the computer to process that number. Luckily some programmers decided they could write programs to help translate the sequence of instructions into numbers. And this started the creation of many programming languages.\n", "\n", - "So a programming language has the task to turn input into numbers that represent instructions the computer understands. The early programming languages had a one to one relationship between words and numbers. With the added benefit that the process can also be reversed. These languages we later extended to help with repeating common patterns in the instructions. For example a sequence of code was repeated many times and loops were introduced. The loops got there own instructions that did not have a one on one relationship any more.\n", + "So a programming language has the task to turn textual input into numbers that represent instructions the computer understands. The early programming languages had a one to one relationship between words and numbers. With the added benefit that the process can also be reversed. These languages were later extended to help with repeating common patterns in the instructions. For example a sequence of code was repeated many times and therefore the concept of loops were introduced. The loops got there own instructions that did not have a one on one relationship any more.\n", "\n", - "Later the one on one relationship was abandond mere and more, where certain instructions in the programming language resulted in multiple computer instructions. These languages provided abstractions for common patterns. Now the reversal becames very difficult and not guaranteed to be the same as the original.\n", + "Later the one on one relationship was abandoned more and more, where certain instructions in the programming language resulted in multiple computer instructions. These languages provided abstractions for common patterns. Now the reversal becomes very difficult and not guaranteed to be the same as the original.\n", "\n", - "In this sense programming language provide higher building blocks than the computer is providing. This makes the life of the programmer easier by giving up on some of the flexibily that you have when you have full control over the computer. With the newer languages we more and more step away from the computer we are working on. This has a benefit that code could run on more than one type of computer but with a possible downside of not taking full use of the capabilities of the computer in use.\n", + "In this sense a programming language provides higher building blocks than the computer is providing. This makes the life of the programmer easier by giving up on some of the flexibility that you have when you have full control over the computer. With the newer languages we more and more step away from the computer we are working on. This has a benefit that code could run on more than one type of computer, but with a possible downside of not taking full use of the capabilities of the computer in use.\n", "\n", - "There a couple of paradigms of solving problems with computation. Each with there own benefits and downsides. There is procedural programming, Object Oriented programming, logical programmin, funtional program, etc. Some programming language work only well with one paradigms where others cater to more.\n", + "There a couple of paradigms of solving problems with computation. Each with there own benefits and downsides. There is procedural programming, Object Oriented programming, logical programming, functional programming, etc. Some programming languages work only well with one paradigms where others cater to more than one.\n", "\n", - "Usually one gets away with using a general-purpose programming language, but at other times one might need to look into a specialised programming language. Its the job of the programmer to pick the right tool for the job. Which one is the best depends on multiple factors you might have guessed already." + "Usually one gets away with using a general-purpose programming language, but at other times one might need to look into a specialized programming language. Its the job of the programmer to pick the right tool for the job. Which one is the best depends on multiple factors as you might have guessed already.\n", + "\n", + "The many options and different ways of solving a problem with a computer makes programming still feel more like an art than a science. But to be honest most of the time it even feels more like magic where we create tools out of invisible stuff." ] }, { @@ -97,7 +99,7 @@ "id": "17af43c5-b1f9-4ac3-9280-0128dcd73921", "metadata": {}, "source": [ - "Python comes preinstalled on many systems, but if its not it can be installed in many ways. You could download a binary or a install packages from the python website: https://www.python.org/downloads/release/python-3100/\n", + "Python comes preinstalled on many systems, but if its not it can be installed in many ways. You could download a binary or an install package from the python website: https://www.python.org/downloads/release/python-3100/\n", "\n", "### Windows\n", "\n", @@ -120,7 +122,7 @@ "\n", "### Jupyter lab\n", "\n", - "This presentation is created in Jupyter lab. It provides an environment that combines code and text. This makes it ideal for science purposes, because it facilitates reproducable code. It can also be used to log your work/experiments.\n", + "This presentation is created in Jupyter lab. It provides an environment that combines code and text. This makes it ideal for science purposes, because it facilitates documented and reproducible code. It can also be used to log your work/experiments. During the course you will see some of the functionalities of Jupyter lab demonstrated.\n", "\n", "https://jupyter.org/install" ] @@ -140,7 +142,19 @@ "source": [ "### Python 2\n", "\n", - "We used to have python2. It had a long life (20 years), but its no longer supported. You will find a lot of code examples and libraries around. Please don't use it anymore. The operating systems will stop supporting it as well soon. Luckely we still have python3 which is a mature successor (12 years old)." + "We used to have Python 2. It had a long life (20 years), but its no longer supported. You will still find a lot of code examples and libraries around for Python 2. Please don't use it anymore. The operating systems will stop supporting it as well soon. Luckily we still have Python 3 which is a mature successor (12 years old).\n", + "\n", + "A clear hint that code is python 2 is the old way of printing strings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4dfcdf73-b032-45f7-ab24-883322040c83", + "metadata": {}, + "outputs": [], + "source": [ + "print \"Hello World\"" ] }, { @@ -193,7 +207,7 @@ "source": [ "### Syntax\n", "\n", - "The syntax of the language determines which combination of symbols form a correct expression or statement. We will dive into different aspects of Pythons syntax during the entire coourse, but two items I want to introduce early. Namely comments and blocks.\n", + "The syntax of the language determines which combination of symbols form a correct expression or statement. We will dive into different aspects of Pythons syntax during the entire course, but two items I want to introduce early. Namely comments and blocks.\n", "\n", "#### Comments\n", "\n", @@ -223,7 +237,7 @@ "\n", "Blocks are a defining feature of Pythons syntax. They are created with indentation. Here we see that text formatting is used to indicate what belongs together. Many other languages don't use indentation for this but use special characters.\n", "\n", - "Blocks will shown in examples when we get to functions and flow control. But beware that you can't just introduce indentation without any effect. It often cause many errors when used incorrectly. " + "Blocks will shown in examples when we get to functions and flow control. But beware that you can't just introduce indentation without any effect. It often cause many errors when used incorrectly." ] }, { @@ -241,13 +255,13 @@ "\n", "Under GNU/Linux and MacOS you can also add the shebang `#!/bin/env python3` as the first line to tell your shell how to run the code following it. You will need to make the file executable with `chmod +x`.\n", "\n", - "Under MacOS you can also assign the Python Laucher 3 to python files.\n", + "Under MacOS you can also assign the Python Launcher 3 to python files.\n", "\n", "Under windows a simple double click would work as well if the python interpreter is assigned to the py extension.\n", "\n", "#### Directly in the REPL\n", "\n", - "You can also experiment with the REPL but starting the python on the command line. The interpreter is waiting for you input and will directly interpret your input when you have entered it. It will report any error or perform your request. Your results are not saved so any results that you want to keep need to be copied to a file. But the direct interaction with python helps a lot with learning and debugging. " + "You can also experiment with the REPL by starting the python on the command line. The interpreter is waiting for you input and will directly interpret your input when you have entered it. It will report any error or perform your request. Your results are not saved so any results that you want to keep need to be copied to a file. But the direct interaction with python helps a lot with learning and debugging. " ] }, { @@ -297,7 +311,7 @@ "id": "0fbd93a6-fa1c-4f38-a6e5-99c2ec222c91", "metadata": {}, "source": [ - "This is a small script / program that outputs the string plus a newline on the standar output.\n", + "This is a small script / program that outputs the string plus a newline on the standard output.\n", "\n", "The first line is the shebang that tells my terminal how to run the program. This way I only have to execute my script and the terminal feeds it into python. An alternative is to run it directly with python like so: python helloworld.py\n", "\n", @@ -372,7 +386,7 @@ "source": [ "## First broken programs\n", "\n", - "Your programs will be broken alot, especially in the beginning, so lets see how to deal with that." + "Your programs will be broken a lot, especially in the beginning, so lets see how to deal with that." ] }, { @@ -632,7 +646,7 @@ "id": "8023c798-fb79-4e2a-ac2d-ee18df1dfa94", "metadata": {}, "source": [ - "In python we also have to deal with the operator precedence. If we want to influence the order of application we will need to use parenthese '(' and ')'." + "In python we also have to deal with the operator precedence. If we want to influence the order of application we will need to use parentheses '(' and ')'." ] }, { @@ -691,6 +705,7 @@ "metadata": {}, "outputs": [], "source": [ + "# These are all variable names that are allowed \n", "_hello = 1\n", "hello = 1\n", "hello2 = 1\n", @@ -705,6 +720,7 @@ "metadata": {}, "outputs": [], "source": [ + "# This one does not start with a letter or an underscore\n", "1_value = 1" ] }, @@ -715,7 +731,8 @@ "metadata": {}, "outputs": [], "source": [ - "@tor = 1" + "# This one includes a non alpha-numeric. (remember _ are allowed)\n", + "t@or = 1" ] }, { @@ -725,7 +742,7 @@ "metadata": {}, "outputs": [], "source": [ - ".der = 1" + "der. = 1" ] }, { @@ -781,7 +798,7 @@ "id": "e9a4844c-e536-45ce-bc87-c572204c2ad2", "metadata": {}, "source": [ - "And variables as case sensitive." + "And variables are case sensitive." ] }, { @@ -866,7 +883,7 @@ "source": [ "### Scope\n", "\n", - "The scope where a variable can be used is limited to the block its defined in. A block can be the global block or a function block or a loop block. The search for the variable is always upword. So first the local block is searched and only if it can't be resolved there it will go up. We will come back to this concept later on." + "The scope where a variable can be used is limited to the block its defined in. A block can be the global block or a function block or a loop block. The search for the variable is always upward. So first the local block is searched and only if it can't be resolved there it will go up. We will come back to this concept later on." ] }, { @@ -878,9 +895,9 @@ "\n", "A function is a grouping of instructions that gets its own name and variables. This is one of the abstraction options most programming languages provide. It allows the concept contained in the function to be reused in multiple places as well. We have seen a few examples of functions already namely print(), input(), help(), and type().\n", "\n", - "We invoke a function by calling its name and adding parenthese. Between the parenthese we can specify parameters if they are required.\n", + "We invoke a function by calling its name and adding parentheses. Between the parentheses we can specify parameters if they are required.\n", "\n", - "We could create long programs without any function created by ourselfs. This is fine for short scripts that perform a small task. But once programs become bigger then that, functions will provide the needed abstraction to keep the code readable and maintainable. " + "We could create long programs without any function created by ourselves. This is fine for short scripts that perform a small task. But once programs become bigger then that, functions will provide the needed abstraction to keep the code readable and maintainable. " ] }, { @@ -903,7 +920,7 @@ "id": "f7524fe0-d24c-4ece-bfc1-d3fa6a90ff3e", "metadata": {}, "source": [ - "This program prints words with rows of stars before and after it. If I have to this for a lot of words it become very repetitive and error prown. I might forgot a star for example. We can make it a bit easier by putting part of the code in a function and call that function a couple of times." + "This program prints words with rows of stars before and after it. If I have to this for a lot of words it become very repetitive and error prone. I might forget a star for example. We can make it a bit easier by putting part of the code in a function and call that function a couple of times." ] }, { @@ -929,7 +946,7 @@ "source": [ "A function definition starts with the reserved word def followed by the name of the function and the parentheses. Between the parentheses we can provide parameters we need for the function. We can have as many parameters as we need but its usually good to limit it to a max of 3. This prevents the function to do too much. A function is most clear to understand, change and reuse if it does only one thing/task. In our case with print a word surrounded by stars.\n", "\n", - "Also pick your names of functions and variable such that an external user understands what it does/means. We could also have choosen oe3 for the function name and l for the parameter name. Python doesn't care about the names but the reader does." + "Also pick your names of functions and variable such that an external user understands what it does/means. We could also have chosen oe3 for the function name and l for the parameter name. Python doesn't care about the names but the reader does." ] }, { @@ -981,7 +998,7 @@ "id": "8e3a3ac0-6d8c-41b4-b881-eee7780d320b", "metadata": {}, "source": [ - "Keyword arguments can be used to be either more explicit or specify one of the default values. This can also be useful for functions with alot of default values. But what are default values? " + "Keyword arguments can be used to be either more explicit or specify one of the default values. This can also be useful for functions with a lot of default values. But what are default values? " ] }, { @@ -1167,7 +1184,7 @@ "id": "2aee0843-6262-4437-9b28-1f7a343353b7", "metadata": {}, "source": [ - "The tripple quoted version allows text to span over multiple lines." + "The triple quoted version allows text to span over multiple lines." ] }, { @@ -1249,7 +1266,7 @@ "id": "87e70f9a-3cf6-4eb8-880e-3486b022ab1a", "metadata": {}, "source": [ - "Other times we want to know the first character of a string. We can do that with an index. And in most programming languages we count from 0 when it comes to indexes. This confuses most beginner programmers but the is a clear hirstorical and technical explanation for it. But I leave that for a coffee break if people are interested." + "Other times we want to know the first character of a string. We can do that with an index. And in most programming languages we count from 0 when it comes to indexes. This confuses most beginner programmers but the is a clear historical and technical explanation for it. But I leave that for a coffee break if people are interested." ] }, { @@ -1360,7 +1377,7 @@ "source": [ "## Objects intermezzo\n", "\n", - "Eventhough we will not be discussing Object Oriented Programming in this course, I do need to discuss the concept of Objects because just about anything in Python is an object.\n", + "Even though we will not be discussing Object Oriented Programming in this course, I do need to discuss the concept of Objects because just about anything in Python is an object.\n", "\n", "So an object is formally an instance of a class. Where the class forms the blueprint from which you can create objects. An object is a shell around data that can be manipulated with methods provided by the object. The data can be directly exposed but most of the time its hidden.\n", "\n", @@ -1385,7 +1402,7 @@ "id": "0ce2f5d1-4e21-4a51-bb47-6a7deed00cb5", "metadata": {}, "source": [ - "And properties are data on an object and are used byt adding a . to the end of the instance and the name of the property.\n", + "And properties are data on an object and are used by adding a . to the end of the instance and the name of the property.\n", "\n", "For example:" ] @@ -1407,7 +1424,7 @@ "id": "cd26d763-f070-4fea-a46a-645cd6c279ab", "metadata": {}, "source": [ - "A nice way to see what methods and properties are available is to use help and dir. Help we have already seen and gives you documentation on a functions, objects and other concepts. Dir provides all the attibutes of an object:" + "A nice way to see what methods and properties are available is to use help and dir. Help we have already seen and gives you documentation on a functions, objects and other concepts. Dir provides all the attributes of an object:" ] }, { @@ -1447,7 +1464,7 @@ "source": [ "## Back to Strings\n", "\n", - "Lets have a look at some methods on Strings. For example the methodes to make all letters uppercase or lowercase letters:" + "Lets have a look at some methods on Strings. For example the methods to make all letters uppercase or lowercase letters:" ] }, { @@ -1475,7 +1492,7 @@ "id": "89070e58-8ade-405f-9b7d-0307a69a7d83", "metadata": {}, "source": [ - "So when would this be useful? One use for it is to compare it some other text where you don't care about case sensetivity. There you would upper or lower both strings before comparing." + "So when would this be useful? One use for it is to compare it some other text where you don't care about case sensitivity. There you would upper or lower both strings before comparing." ] }, { @@ -1502,7 +1519,7 @@ "id": "ef7b8b6e-cf56-4e5f-ac49-528f88e1f183", "metadata": {}, "source": [ - "Another way is to use Formated string literals. This has a mini language on its own so I will only be showing some basic options. We start a formated string with a f\"." + "Another way is to use Formatted string literals. This has a mini language on its own so I will only be showing some basic options. We start a formatted string with a f\"." ] }, { @@ -1548,7 +1565,7 @@ "id": "c1ef4501-0f22-44bf-9cb9-590c56d14028", "metadata": {}, "source": [ - "Yet another way that can be used it the format string syntax. Its similar to a Formated string literals but its a bit more explicit." + "Yet another way that can be used it the format string syntax. Its similar to a Formatted string literals but its a bit more explicit." ] }, { @@ -1616,7 +1633,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 formated 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 given options of creating new strings." ] }, { @@ -1640,7 +1657,7 @@ "source": [ "## Compound data types\n", "\n", - "So we have seen basic data types like numbers. And strings that are a chain of numbers intereted as characters. In the end we only have numbers in memory. But as programmers we can give meaning to these numbers and with it we can build complex data stuctures. We can use numbers to point to memory locations and that is what happens under the hood with the following compound data types." + "So we have seen basic data types like numbers. And strings that are a chain of numbers interpreted as characters. In the end we only have numbers in memory. But as programmers we can give meaning to these numbers and with it we can build complex data structures. We can use numbers to point to memory locations and that is what happens under the hood with the following compound data types." ] }, { @@ -1881,7 +1898,7 @@ "source": [ "### tuples\n", "\n", - "A tuple is an immutable container. One can create a tuple with the parenthese characters '(' and ')'." + "A tuple is an immutable container. One can create a tuple with the parentheses characters '(' and ')'." ] }, { @@ -2162,7 +2179,7 @@ "source": [ "## Flow control\n", "\n", - "In flow control we decide to run or skip certain code based on truth values. Python has the two object True and False reserved as truth values. The meaning of these objects is quite self explanetory.\n", + "In flow control we decide to run or skip certain code based on truth values. Python has the two object True and False reserved as truth values. The meaning of these objects is quite self explanatory.\n", "\n", "Lets get into what things result in truth values." ] @@ -2364,7 +2381,7 @@ "source": [ "### While\n", "\n", - "To repeast certain steps we could call a function multiple time but this is very limited. We could use a looping mechanism in the language. Luckely Python provides a could of those. One of them is the while loop.\n", + "To repeat certain steps we could call a function multiple time but this is very limited. We could use a looping mechanism in the language. Luckily Python provides a could of those. One of them is the while loop.\n", "\n", "It will perform a block until something becomes false." ] @@ -2403,7 +2420,7 @@ "source": [ "### Exercise\n", "\n", - "Create a program to calculate the fibonacci series till a maximum of say 100. https://en.wikipedia.org/wiki/Fibonacci_number" + "Create a program to calculate the Fibonacci series till a maximum of say 100. https://en.wikipedia.org/wiki/Fibonacci_number" ] }, { @@ -2434,7 +2451,7 @@ "source": [ "### Exercise\n", "\n", - "Loop over a list or tuple with numbers and print out the square of it. You could create a function for the quare calculation if you want." + "Loop over a list or tuple with numbers and print out the square of it. You could create a function for the square calculation if you want." ] }, { @@ -2511,7 +2528,7 @@ "\n", "The abstraction can be helpful in clearly writing down how our process works. And we do this by abstracting details away at the right level. We want to combine abstractions of the same detail level to create another layer of building block with a higher detail level.\n", "\n", - "We could give a complex check used in an \"if\" a name to give it a meaning. We want the reader of the code flowing through our code and not have interuptions with lower level details." + "We could give a complex check used in an \"if\" a name to give it a meaning. We want the reader of the code flowing through our code and not have interruptions with lower level details." ] }, {