"Your programs will be broken a lot, 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. If your program is broken most of the time you will be confronted with an exception. Exceptions are a way of informing the user or programmer that something is wrong."
]
},
{
...
...
@@ -406,7 +406,7 @@
"id": "95082acd-7c8b-4138-9189-84af128a0a93",
"metadata": {},
"source": [
"This program will give a NameError."
"This program will give a NameError exception."
]
},
{
...
...
@@ -426,7 +426,7 @@
"id": "93564fff-c8c9-4059-89f2-82d29453f28e",
"metadata": {},
"source": [
"This program will give an IndentationError. Indentation is very important in a Python program."
"This program will give an IndentationError exception. Indentation is very important in a Python program."
]
},
{
...
...
@@ -446,7 +446,21 @@
"id": "7b84478e-b8c8-48ad-b6b5-b29d4b21ad3d",
"metadata": {},
"source": [
"This program will give a SyntaxError"
"This program will give a SyntaxError exception."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d77d3d4e-f0ca-4df2-82b6-198bc61a3995",
"metadata": {},
"outputs": [],
"source": [
"#!/bin/env python3\n",
"\n",
"# lets raise some exception ourselves to tell that there is something wrong.\n",
"\n",
"raise Exception(\"Something is wrong\")"
]
},
{
...
...
@@ -1514,6 +1528,26 @@
"print(hello)"
]
},
{
"cell_type": "markdown",
"id": "6c95d9cf-4370-47b4-b6da-1933f0333a5e",
"metadata": {},
"source": [
"We can also generate strings by multiplying strings or characters. Very useful if you need a specific amount."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "606c5a7a-767e-43a7-81e0-04a2d17dae54",
"metadata": {},
"outputs": [],
"source": [
"print('+' * 79)\n",
"print('|', \" Hello \" * 11, '|', sep=\"\")\n",
"print('+' * 79)"
]
},
{
"cell_type": "markdown",
"id": "ef7b8b6e-cf56-4e5f-ac49-528f88e1f183",
...
...
@@ -2167,7 +2201,7 @@
"id": "d5ec52d1-7e41-4439-b4a8-a46caf0de825",
"metadata": {},
"source": [
"### Exercise\n",
"#### Exercise\n",
"\n",
"Create a program where you ask the user for a multiple items where you ask for some properties for each item. The properties can be stored in a dictionary and be added to a list. Print the list in the end. Try to use functions as well."
]
...
...
@@ -2199,7 +2233,9 @@
"print(1 <= 2)\n",
"print(1 >= 2)\n",
"print(False is True)\n",
"print(False is not True)"
"print(False is not True)\n",
"print('a' in \"abc\")\n",
"print('A' in \"abc\")"
]
},
{
...
...
@@ -2339,6 +2375,27 @@
" print(\"Bye\")"
]
},
{
"cell_type": "markdown",
"id": "30376e08-b2ce-4b07-8d69-c152d9ad28d1",
"metadata": {},
"source": [
"We can also negate a truth statement by using \"not\":"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a3e06ca4-510f-4a35-ba88-876a46209159",
"metadata": {},
"outputs": [],
"source": [
"if not False:\n",
" print(\"True\")\n",
"else:\n",
" print(\"False\")"
]
},
{
"cell_type": "markdown",
"id": "77804c8d-4a6b-4322-82a9-60696df3be31",
...
...
@@ -2369,7 +2426,7 @@
"id": "97a513bc-26ba-471e-ae3a-67b674bde3a9",
"metadata": {},
"source": [
"### Exercise\n",
"#### Exercise\n",
"\n",
"Create a text menu with options. Let the user enter a selection and print something out specific for that option. Use the else to catch an option that you didn't provide. Think about uppercase and lowercase if you don't use numbers as options."
]
...
...
@@ -2418,7 +2475,7 @@
"id": "102f9376-db7e-4beb-855c-bbbf36fde65e",
"metadata": {},
"source": [
"### Exercise\n",
"#### Exercise\n",
"\n",
"Create a program to calculate the Fibonacci series till a maximum of say 100. https://en.wikipedia.org/wiki/Fibonacci_number"
]
...
...
@@ -2449,11 +2506,125 @@
"id": "f56444df-a1d6-4413-b1e7-ac0b259ad5be",
"metadata": {},
"source": [
"### Exercise\n",
"#### 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 square calculation if you want."
]
},
{
"cell_type": "markdown",
"id": "0d2014c1-fc60-4af6-862c-a08cef58cbaf",
"metadata": {},
"source": [
"#### Range\n",
"\n",
"We sometime want to loop based up on a sequence of numbers. We can do this with a range."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c166c501-25ed-4cea-9e6b-f1849df8d090",
"metadata": {},
"outputs": [],
"source": [
"print(range(5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eae49e01-f8b9-4571-a769-75badfd83765",
"metadata": {},
"outputs": [],
"source": [
"print(list(range(5)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "870ec3b2-4adb-4be1-83b2-cd5e873e6df8",
"metadata": {},
"outputs": [],
"source": [
"for i in range(5):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f77a3494-dcc3-41dd-827f-7145a550c7b7",
"metadata": {},
"outputs": [],
"source": [
"greeting = \"Hello World\"\n",
"\n",
"for i in range(len(greeting)):\n",
" print(i, greeting[i])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "beb40a6a-2468-4fe5-8b9d-8dbd693365d9",
"metadata": {},
"outputs": [],
"source": [
"greeting = \"Hello World\"\n",
"\n",
"for i in range(0, len(greeting)):\n",
" print(greeting[i])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7ebf73d-a58d-4ca5-8a00-b5519c847793",
"metadata": {},
"outputs": [],
"source": [
"greeting = \"Hello World\"\n",
"\n",
"for i in range(0, len(greeting), 2):\n",
" print(greeting[i])"
]
},
{
"cell_type": "markdown",
"id": "ba7f4e9f-51c9-4516-975b-a6365aee1b86",
"metadata": {},
"source": [
"#### Enumerate\n",
"\n",
"We see that we had to do the indexing of the letter in the range examples. This is a repeating patter that used so often it got its own abstraction. Its the enumerate. It returns a tuple with the index and item."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6f049e8a-3915-4dca-a37f-ceeda4261f89",
"metadata": {},
"outputs": [],
"source": [
"print(enumerate(\"Hello\"))\n",
"print(list(enumerate(\"Hello\")))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68e2e358-dbcd-4a24-a525-9ed5115a2b99",
"metadata": {},
"outputs": [],
"source": [
"greeting = \"Hello World\"\n",
"\n",
"for i, char in enumerate(greeting):\n",
" print(i, char)"
]
},
{
"cell_type": "markdown",
"id": "0d025fb4-d651-431b-96fc-2605d2c18385",
...
...
@@ -2543,7 +2714,7 @@
"\n",
"angle = 44\n",
"\n",
"if angle > 30 and angle < 180:\n",
"if angle >= 30 and angle <= 180:\n",
" turn_to(angle)\n",
"else:\n",
" print(\"Unsafe angle:\", angle)"
...
...
@@ -2556,16 +2727,8 @@
"metadata": {},
"outputs": [],
"source": [
"def turn_to(angle):\n",
" print(\"Turning to angle:\", angle)\n",
"\n",
"def is_safe_angle(angle):\n",
" return angle > 30 and angle < 180\n",
"\n",
"def report_unsafe_angle(angle):\n",
" print(\"Unsafe angle:\", angle)\n",
" \n",
"# ------------------------------------\n",
"MIN_ALLOWED_ANGLE = 30\n",
"MAX_ALLOWED_ANGLE = 180\n",
"\n",
"def turn_telescope(angle):\n",
" if is_safe_angle(angle):\n",
...
...
@@ -2573,6 +2736,15 @@
" else:\n",
" report_unsafe_angle(angle)\n",
"\n",
"def is_safe_angle(angle):\n",
" return angle >= MIN_ALLOWED_ANGLE and angle <= MAX_ALLOWED_ANGLE\n",
"\n",
"def turn_to(angle):\n",
" print(\"Turning to angle:\", angle)\n",
"\n",
"def report_unsafe_angle(angle):\n",
" print(\"Unsafe angle:\", angle)\n",
" \n",
"turn_telescope(angle=44)"
]
},
...
...
@@ -2583,7 +2755,72 @@
"source": [
"## How to solve problems with programming\n",
"\n",
"Breaking up and composing"
"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",
"\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",
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.
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.
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.
Programming is solving problems with computations. Its describing a process in an accurate way with discrete steps.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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/
### Windows
* Windows Store
* Python website (see above url)
* Windows Subsystem for Linux (WSL)
### GNU/Linux
* Package manager
### MacOS
* Homebrew package
* Python website (see above url)
### Anaconda
Another popular python distribution in the (data) science world is the Anaconda distribution: https://www.anaconda.com/products/individual. It has installation options for windows, GNU/Linux and MacOS
### Jupyter lab
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.
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).
A clear hint that code is python 2 is the old way of printing strings
Maybe both python2 and python3 are installed. In that case it could be that the python binary/link still points to python2. You could check if there is a python3 binary installed.
>Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.
>
>Python is dynamically-typed and garbage-collected. It supports multiple programming paradigms, including structured (particularly, procedural), object-oriented and functional programming. It is often described as a "batteries included" language due to its comprehensive standard library.
Executive summary from python.org
https://www.python.org/doc/essays/blurb/
> Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms, and can be freely distributed.
> Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying to Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.
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.
#### Comments
A comment is a piece of text that gets ignored by the language and is used to communicate with the reader of the code.
In Python comments start with a '#'. And it will ignore the entire text till the end of the line.
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.
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.
You can place your code into a plain text file. The file usually has the .py extension. You can then run the code with the following command:
`python filename.py`
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`.
Under MacOS you can also assign the Python Launcher 3 to python files.
Under windows a simple double click would work as well if the python interpreter is assigned to the py extension.
#### Directly in the REPL
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.
Python has a build in documentation system. In the REPL you can ask for help on object and concepts. For example we can run help on functions.
https://docs.python.org/
The official python documentation mostly cover python and its libraries. For all the other questions you can use your favorite search engine. Be careful with what you copy and paste because it might have bugs or vulnerabilities in them. Many searches end up at stack overflow. This is a question and answer site for technical questions.
https://stackoverflow.com/
Many of the external python libraries are documented on "read the docs".
This is a small script / program that outputs the string plus a newline on the standard output.
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
Then we have a empty line that gets ignored by Python. The last line contains a built-in function called "print" and this function accepts a couple of arguments. In this case I gave it the string "Hello World".
### Exercise
Run the program directly in the terminal. And put the program into a file and run it that way.
Print objects to the text stream file, separated by sep and followed by end. sep, end, file and flush, if present, must be given as keyword arguments.
All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.
Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.
Changed in version 3.3: Added the flush keyword argument.
So based on the information given in this first section I could spend days talking. But this is a beginners course on programming. We happen to use Python as our tool of learning so lets get some basics out of the way first. Its fine if not everything is understood from the start because I will revisit them later.
The help text of print talks about values, streams, sys.stdout. It mentions functions, objects and modules. It also mentions optional keywords and strings. We only have 2.5 days so I will need to leave out a lot of things that Python offers. Some will be covered in other courses Astron will give, but others will need to be learned through other means.
### Exercise
Run help() in the REPL to see what options it gives. And also run help(print) to see what that provides. Also try out help on some other functions / concepts.
Your programs will be broken a lot, 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. If your program is broken most of the time you will be confronted with an exception. Exceptions are a way of informing the user or programmer that something is wrong.
Now that we have introduced numbers we can start doing some math with it. Out of the box Python provides most of the basic math operations you would expect. +, -, * 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 ')'.
We cant pick any name for a variable. A variable needs to start with letter or an underscore. The remainder of the variable name can only be alpha-numeric characters or underscores.
Ask the user for two numbers and print the result of some math operation on those numbers. You can use the input() function to get the user input. See small example below.
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.
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().
We invoke a function by calling its name and adding parentheses. Between the parentheses we can specify parameters if they are required.
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.
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.
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.
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.
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?
A function can also return a object as a result of the function. We could use that to a calculation on the parameters for example. To indicate what to return we use the reserved word "return".
Create a function that expects two parameters that get added together and is returned by the function. Call the function a couple of time with different values and print the results.
Scope determines the range wherein names get resolved. So when code refers to some variable or function it will try to locate it in a limited space and in a specific order. Some examples:
Python documentation describes as: "immutable sequences of Unicode code points.". So we get a chain of Unicode characters but we cannot change anything in the chain.
Sometimes we need to know the length of a string and we can do that with a function called len(). Len() is a function that will return the number of items in a container. A container is a general term for data types that hold items. A string is a container the holds unicode characters.
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.
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.
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.
This gives us programmers a new form of syntax for dealing with objects. Methods are functions on an object and are called on the object by adding a . to the end of the instance and the name of the method.
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:
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.
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".
Now it up to you as a programmer to pick the right option. And when you do also go for the option that clearest to read. Usually if strings are repeated often or there are many of them the named options is cleaner. If you have only one or two in a short sentence the simple {} will do.
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.
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.
Now we don't want to keep track of the meaning of all numbers so we abstract that away in programming languages. One nice abstractions is a grouping of things. We call that a list in many languages. Other languages sometimes call these arrays. So what does a list look like in Python?
Combining types is allowed, but for most algorithms that loop over a list it is required to have the items to be of the same type. What you see more often is that data of different types get added together in compound data type and those are then placed in a list. We will see some more examples of that later.
Create some lists on your own. Add some together. Try some slices, indexing. Try to replace some items. Also think about a real world problem where lists could be useful.
So when are tuples useful? If you don't want to have the data changed. It is also slightly more memory efficient since it can't be extended. This requirement does not come up often but the wish to extend or remove data is so lists are more popular then tuples.
Although not enforced by the language we see tuples more used a containers of different types and lists as a container for single types.
So what could we do with sets? We could get all the unique items out of a big list. Say we get a list of all the LOFAR stations that had an issue during an observation in the October. We could ask with a set which stations were had an issue during October.
Dictionaries is a useful compound data type that is a set of key value pairs. They are also created by using curly braces but now it contains a key value pair.
Create a program where you ask the user for a multiple items where you ask for some properties for each item. The properties can be stored in a dictionary and be added to a list. Print the list in the end. Try to use functions as well.
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.
Besides False and True other values can be used as truth indicators. Lets take a quote from the python documentation. https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
> By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. 1 Here are most of the built-in objects considered false:
>> constants defined to be false: None and False.
>> zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
Now the fun can begin. We can finally make choices in our programs. The if statement will take a truth value and a block of code to execute if the value is True.
Create a text menu with options. Let the user enter a selection and print something out specific for that option. Use the else to catch an option that you didn't provide. Think about uppercase and lowercase if you don't use numbers as options.
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.
It will perform a block until something becomes false.
We see that we had to do the indexing of the letter in the range examples. This is a repeating patter that used so often it got its own abstraction. Its the enumerate. It returns a tuple with the index and item.
Sometimes there is a need to stop early in a loop. For example in searching for an item in a list. Once found you could break the loop early. This can be done with a break.
So now lets combine a lot of things we have learned. Lets create a calculator. Lets take a while loop that is always True. Ask for input from the user. Say two values and an operator. Then print the answer. If the user provides wrong input we tell the user. If the user provides a 'q' character we want to quit the loop. There is no one right answer here.
Abstraction is a way to hide details and giving it a name.
We can abstract things in Python with compound data structures, functions, objects, etc.
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.
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.
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.
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?
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.
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.
Thanks for joining the course. You now have learned all the basics of programming. There are many more concepts to learn, but you can now consider yourself a programmer. Go out and automate your life and please share your knowledge and experience. You could use the blog feature of confluence to share or use what ever platform you have access to.