-
Notifications
You must be signed in to change notification settings - Fork 0
Programming in python
This page explains some basic programming concepts using python and how to use jupyter notebooks.
The language we will will use to explain basic programming concepts in this guide is python3. This should be installed by default, but you can check for sure by running the command python3 --version.
A computer is built up from simple logic gates that can only operate on a binary input represented by either a high voltage level or a low voltage level, with each representing either a one or a zero. That means that when we want to give a computer instructions, these have to be given in binary, referred to as machine code. Writing machine code directly takes up way to much time as it is very difficult to read and very low level. Therefore, we usually write instructions for a computer in a programming language, an abstraction that is easier for us to understand. The code written in a programming language is usually translated to machine code in several steps and the resulting binary is then executed.
There is a distinction to make here between compiled languages and interpreted programming languages. Compiled languages are usually first compiled to assembly language, a lower level abstraction, with a single statement corresponding to a single machine instruction. What instructions are provided depends on the platform, making assembly platform dependent. Assembly is then converted to machine code by the assembler. Compiled language are languages like, c, c++, go, etc.
Python is an interpreted language, this means that code is not actually converted to machine code, but that the code is executed by the interpreter. Interpreters run through the code line by line executing each command.
Compiled languages tend to faster as they can be directly executed and optimized for the processor, whereas the translation of code by interpreters at runtime causes an extra overhead. However, they take an extra step to actually compile to machine code and are more platform dependent, while interpreted languages can immediately be executed by the interpreter.
If we are really precise, most languages can have both compiled and interpreted implementations – the language itself is not necessarily compiled or interpreted. However, for simplicity’s sake, they’re typically referred to as such and when you run python code on your computer it will be interpreted and not compiled. The differences between compiled and interpreted language have many other consequences, that we will not go into here.
To write a simple program in python, first make a new file named "helloworld.py". Write the following code in that file and save it.
print("hello world!")
You can now run this program by executing the following command.
python3 helloworld.py
You should see that the program prints "Hello world!" to the console. That is it, you wrote your first program. We will go into what this program did exactly in a moment.
You have just seen how to write a simple python program. Instead of taking you through all the concepts like this, just through text in this guide, we will be using jupyter notebooks from now on. These provide a more interactive way of explaining what exactly is happening in the code. When later developing python code by yourself however, you will usually write the code in python files and execute those just as in the example above.
Before trying to install jupyter notebook, first check if it already installed with the command:
jupyter notebook --version
To install jupyter notebook, you will first need the python package manager for python3, pip3. Install pip3 by running the command:
sudo apt install python3-pip
Now you can use pip3 to install jupyter notebook with the command:
pip3 install notebook
Pip3 will also install the necessary dependencies. Check if the installation was successful with the following command.
jupyter notebook --version
In the previous chapter Working with ubuntu, you cloned this repository. If you go into this repository you can now run the following command.
jupyter notebook ./python_tutorials
This will start the jupyter notebook server in the python_tutorials directory, where the files are stored for this chapter of the guide. When you run this command it should open the interactive environment in the browser where you have an overview of the folders that contain all the files you will need this chapter. To get started click on the directory basic_programming_concepts and open the file Using notebooks.
- Home
- Organisation
- Working with Ubuntu
- Git
- Python
- Rust
-
Robot usage
- Setting up DNT
- pyNaoqi Setup Guide
- Connecting and compiling
- Teamcopy
-
Playing a match
- Setup
- Rules
- Working day
- Gamecontroller
-
Framework
- Overview
- Taal
-
Framework Applied
- Dummy
- Assignment