Find the danish version in the side bar (bottom of the page if you are using a mobile device)

Slides

Code snippets

Setup your computer and start engineering the future

Slides

2019-09-06 Tommerup

Code snippets

Writing code is a discipline, like writing a book. You need a story / a goal for your book or application. An application is constructed with classes (chapters in the book terminology), each class has several units (sections), which again have smaller structures (paragraphs). Here is the three most used structures: loop, branching and mathematical operators.

Looping is a structure you can use to repeat tasks, like counting all words in a book. Another important structure is branching, dependent on a test, do this or do something else. And lastly is the mathematical operators, where you can use all the simple operators you find on a calculator (add, subtract, divide, multiple, exponential and modulus).

Code structure: loop

A loop can be used for repeating tasks. For example here, where it is a for-loop, which prints Hello 3 times, because of the range(3) argument.

In this loop, the loop-variable i is used, to calculate i^2 or simply i*i. As you can see, the used range here is range(10), mening i will have all the values between 0 and 9. In all programming, the first number i 0.

Now let’s have all the calculations in between, and print a matrix, which can be used for finding the small table, eg the table for 7 (row 8 or column 8). In this example, 2 for-loops are used, and print(j*i, end=' ') has an argument, for printing an empty space after each number. Furthermore, there is a print() statement, to ensure an empty line.

Libraries: pandas

Data science via Pandas

Data structures, age and weight are lists

A little application: Calculator

Let’s see what a few lines of code can do, if we combine some of the snippets above. Let’s create a simple and stupid calculator with a few lines of code. The first three lines are variables, imagine those coming from your keyboard (where you just type 15 + 15). Next is a branch (you remember that code is just loops, branches and operators?), testing for our implementation can handle the assigned operator. Then based on the value of the operator, the results is calculated and then printed. If the operator is not supported, then it is printed to the end user. Be aware, that the print uses %i and %s, where %i is used to print integers and %s is used for printing strings (text).

Try adding multiplication and division in the snippet above – remember to test that you cannot divide by 0, then you will get a ZeroDivisionError

Setup your computer and start engineering the future

To setup your computer, you will need two applications, Git and Docker. Git is a version control system – an application used for sharing code between developers. Docker is tool you can use for easy deployment of software-as-a-service. Docker enables you to have an environment running on your machine that is identical with the one running on mine (it can take hours to setup an environment, but not with Docker).

Install Git

Install the Git version control system, then you can access the majority of code related to open source applications. Find a guide for installing Git for your OS (MacOS/Linux/Windows) here:

https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

All materials for my presentation (and this page), can be accessed via git, use the following from cmd / terminal:

git clone https://gitlab.com/emilholmegaard/engineer-the-future.git

Install Docker

Install Docker, which enables you to install almost any software in the world on your own computer. Find a guide for installing Docker for your OS (MacOS/Linux/Windows) here:

https://runnable.com/docker/

Play with the code snippets

If you have run the git clone command, then do the following afterwards:

cd engineer-the-future/notebooks
./start-notebooks

Your standard browser will now open a Jupyter application (a framework for execution Python in your browser). All this is setup via Docker – so it might take some time, the first time you run the script.

For Windows use the following – because I did not have time to setup a windows machine to create a bash script for running everything automated. Replace [NOTEBOOKS_DIR] with the path to where engineer-the-future/notebooks is located. After running this command – copy the URL that is printed in the terminal

docker run -p 8888:8888 -d -v [NOTEBOOKS_DIR]:/home/jovyan jupyter/scipy-notebook