-
Notifications
You must be signed in to change notification settings - Fork 0
Programming in python
This page explains programming concepts using python and how to use jupyter notebooks. Additionally there are also some assignments with tests that you can use to test your understanding.
The language we will will use to explain 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 programming_concepts and open the file Using notebooks. If you are done working with these jupyter notebooks, save your progress and shutdown the notebook server by pressing ctrl-c in the terminal where you started the server.
We have given you a lot of guidance via in the notebooks thus far. Now, a few assignments are provided that allow you to test your understand on some of the concepts we discussed, you are free to come up with your own solutions. The starting points for these assignments are defined in /python_tutorials/assignments.
For each assignment you will find several files: template.py, solution.py, test_template.py. solution.py contains the solution to an assignment, feel free to take a look here is you are stuck. template.py is where you will write your code, often any functions you need to write will already be defined here. Lastly test_template.py is a file that contains tests. You can run these tests by executing the command pytest. Before starting you need to install opencv-python.
The first assignment is implementing the game rock, paper, scissors. If you want to test the functions you have defined for yourself, you can put a function in the run function, which is the entry point to the program.
Start by implementing the get_user_action function. This function should use the provided get_user_input function to get user input. If the user does not input the strings "rock", "paper" or "scissors", ask for input again. When the user inputs one of these three strings, return it. Note, a list of possible actions is already provided at the top of the file.
Next lets make the computer generate a move. Do this by implementing the get_computer_move function. To implement this function you should make use of the random library. You can find documentation on this library here.
Now implement the print_result function. This function should not return anything and only print results based on the user_action and computer_action provided as arguments.
First print: "You played {user_action}, the computer played {computer_action}." Then there are several cases:
- If both players did the select the same action, print: "Both players selected {user_action}. It's a tie!"
- If the user selects rock and the computer selects scissors, print: "Rock smashes scissors! You win!"
- If the user selects rock and the computer selects paper, print: "Paper covers rock! You lose!"
- If the user selects paper and the computer selects rock, print: "Paper covers rock! You win!"
- If the user selects paper and the computer selects scissors, print: "Scissors cuts paper! You lose!"
- If the user selects scissors and the computer selects paper, print: "Scissors cuts paper! You win!"
- If the user selects scissors and the computer selects rock, print: "Rock smashes scissors! You lose!"
Next implement the play this function should ask for user input, the generate a computer move and then print the result.
Next implement the wants_to_play_again function. This function is a lot like the get_user_action function. Except it only accepts "yes" and "no" as input.
Lastly, tie everything together in the run function. Run the play function in a loop, after every round ask the user if they want to play again. If they do not, break out of the loop.
Note: you might see the following if-statement at the bottom of the file:
if __name__ == "__main__":
It is a convention to define the entry point to python programs in this way. This if statement will only be true if the file is executed. This means the functions are not accidentally run when this file is imported by another file.
In this assignment we will be implementing functionality to encode and decode Morse code. We will be using the same conventions as found on this site, you can also use it to check your results. Before working on this assignment, take a look at the argparse library and try to understand how we use it here (play around with it).
The first function you should implement is the read_file function. It should read a file at the provided path and then replace all newlines with spaces.
Next, implement the write_to_file function. Which simply write a given string to a file on the provided path.
Lastly, implement the Morse.decrypt function and the Morse.encrypt function. You can find examples of inputs and outputs in the directories plaintexts and ciphertexts.
Try using the CLI!
As a preparation for working with opencv and reading images in the computer vision chapter, in this last assignment we will be manipulating images in several ways. This file contains a list of functions that all manipulate a given input image is some way. Work from top to bottom as the functions are ordered from easier to more difficult to implement.
Before manipulating images, you need to define functions to write an image from and to a file. Implement the read_img and save_image functions. To do this use the opencv library. Before moving on to implementing the next functions, take a look at what an image looks like once you read it from a file. You can use the images in /images/ as examples. You should see a three dimensional array. An array of 3 values for each pixel. These three values represent for each of the colors "g", "r", "b" how much of that color should be in that pixel, these values are often referred to as channels.
Now implement the other functions, we will give a few tips for some of these functions:
-
to_grayscale: take the average of the three channels. -
to_black_white: useto_grayscale, then make a threshold using opencv. -
get_channel: read the explanation in the previous paragraph. -
rotate_nn: this one is tricky, so we will give a little more detailed explanation. Think about this problem in terms of where a certain pixel in the rotated image comes from. Map each row and column in the output to the row and column in the input image that it comes from. It should be clear that when trying to figure out where a certain pixel comes from in the source image, this location might not necessarily be precisely on a row or column in the source image, therefore you need to round off to get an approximation to the nearest pixel, this is also referred to as nearest neighbour interpolation.
- 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