Pre-Exercises and Instructions¶
These pre-exercise page contains installation instructions for important tools. It also contains some very basic information about how to use Python.
Learning goals: After this exercise you should know how to use Python. In particular you know what the
Python consoleis. You also know what
code filesare and how to
runthem from the
We recommend following these guides when installing necessary software to your home computer. If you only use the computers in computer classrooms you can skip this part. However please at least check Notepad++ settings from below because the defaults in the classrooms are not adequate.
In these instructions we're going to install Python because it would be rather difficult to complete a Python course without it. In addition we're installing a better text editor that's practically mandatory. Each part has separate instructions for each OS. However in general the Windows instructions are more detailed.
First and foremost it's important to note that we're using Python 3 - by now Python 2 is basically dead. The present version of Python is 3.8 but some instructions may refer to older versions if we've missed them during the yearly material updates.
All packages are found from the official source: Python Software Foundation pages. More detailed instructions below.
We recommend doing installation in Windows using admin privileges. Installing without admin privileges has led to all sorts of tricky problems in the past years. Just to make things easier for everyone, use admin privileges to install.
- Grab the installation executable from the official source and save it to an appropriate folder on your computer.
- Double click or otherwise execute the
python-3.8.1-amd64.exefile you just downloaded.
- Go through the installation process paying attention to the highlighted parts in the screenshots below.
Everything should be done after this.
Max OS X¶
At least older OS X systems come with Python 2 pre-installed. Since it's practically dead, Python 3 has to be installed as well.
- Download the official installer if your OS is version 10.9 or newer. If your OS version is older but not older than 10.6, grab this Python 3.7 installer instead.
- Click through the installation, accepting all terms. Defaults should work just fine.
Please not that if you have both Python 2 and 3 on your system, you must always include the 3 when running commands, e.g.
python3- otherwise you'll end up using Python 2 instead.
Most Linux distributions come with Python 3 installed. Even if the version is a bit older it should not matter for this course. Python 3.6 is perfectly compatible with this material and Python 3.5 may have to make some minor compromises. Note that if your distribution comes with both Python 2 and 3, you must always include the 3 when running commands, e.g.
python3- otherwise you'll end up using Python 2 instead.
If you do need to install Python 3, it can usually be done from the package management. In general we expect you to know your distribution better than we do - just follow the proper installation procedures. In case your distribution doesn't have Python 3 available at all, you have to compile it from source code. You can download the source code from here. After that go to the
terminaland input these commands:
- cd /your/download/folder
- tar xvzf Python-3.8.1.tgz
- cd Python-3.8.1
- make install
Pay attention to warning about missing libraries (e.g. the developer package of readline library). Install the ones you think you will need, and then install Python again.
A proper text editor (or development environment) is essential in programming. Majority of time is spent writing and editing
code. A good text editor has a lot of convenience tools to assist in this. At the very minimum your text editor should be able to:
- display line numbers;
- keep multiple files open in tabs;
- highlight Python syntaxwith colors;
- and replace tabulator characters with a desired number of spaces
Good text editors generally do more than this. Instead of a simple text editor, you can also install an entire development environment (e.g. PyCharm). Development environments offer even more features and tools. They do take a bit more effort to learn however, and are not as essential to Python programming as they are for other languages. They are also a lot heavier on the performance side.
For Windows we recommend Notepad++. It's open source, completely free and fulfills all our requirements for a good text editor. It's also available on the computers in the university classrooms.
- Download the installer from this page.
- Double click or otherwise execute the installer.
- If needed, let Windows know that yes, do indeed wish to install this program.
- Follow instructions. Defaults should be ok.
Once the program has been installed, there are a couple of default settings that need to be changed.
- Go to Settings -> Preferences
- Find the "Language" tab from the list and check the box next to "Replace by space"
- Find the "New Document" tab from the list and make sure encoding is "UTF-8" (important: NOT "UTF-8 with BOM") and check the box beneath it. Finally select "Windows" from the Format settings.
You can change the other settings to your liking. These two settings will help you avoid problems if you are transferring files between two computers and if you are editing examples downloaded from the course material.
Mac OS X¶
For Mac OS X we recommend using Atom since it is open source and free. It also should come with good default settings. After installation you can easily open Atom from the
Most Linux desktop environments come with a default text editor that is perfectly adequate for this course, e.g. Gedit in Gnome or Kate in KDE. You can also install a variety of other text editors.
Command Line Tools¶
Both Linux and Mac OS X come equipped with very adequate
terminalsfor programming use. The Windows command prompt is a bit lacking but its newer versions are sufficient for this course. Please be aware that command line tools go by multiple names: terminal, shell, command line, command prompt, console...
Tool Roles and Use¶
Each tool mentioned in the previous section have their own role in programming:
- Python: runs code (i.e. translates it to machine code for the processor) that's been written to the interactive consoleor to acode file
- Text editor: used for writing and saving code files
- Terminal: used for typing system commands and starting Python
When you sit down to program, the first thing is usually to start both the text editor and the terminal. It's best to get used to running Python from the terminal right from the start. Later in the course there will be things that can't be done if you just launch Python from the OS menu.
Using the Terminal¶
Terminal, aka command line, aka command prompt, aka console, aka shell is a text-based user interface. For most modern users it's a relic of ancient times when computers didn't even have graphical user interfaces. Even today server maintenance is typically done entirely in the terminal. For Linux users terminal is usually at least somewhat familiar. In Windows it's a bit of a black sheep that's kept hidden.
Starting the Terminal¶
In Windows you can open the start menu and type
cmdto the search. The terminal (or Command Prompt as Windows calls it) should be on top of the search results, and you can start it be pressing Enter.
In Linux you should be able to open the terminal by pressing Ctrl+Alt+T. If that fails you can press Alt+F2 and type "Terminal" to the search bar. In Mac OS X you can find the search bar in the top right corner, and once again type "Terminal" to find what you need.
Terminal and Folders (Windows)¶
Knowing where you are is essential when working in the terminal - this is also know as the active folder (or directory). You can move between folders and view their contents just like you could in Windows Explorer. You just need to use the following commands instead of clicking with the mouse:
cd: (change directory) this command is used for moving between folders using eitherrelativeorabsolutepath.
dir: (directory listing) this command shows the contents of the current folder
When you open the terminal it usually puts you into your home folder. Tricky part about this is that the shortcuts in Windows Explorer are folders that are beneath your home folder. Furthermore if your Windows language is not English, the real names of these locations will be different from what is displayed in the Explorer. Either way you can see the real names of all folders inside your home folder with the
dircommand. Then you can move to these folders with the
For example let's assume you've created a folder called "Programming" inside your "Documents" folder. Since a freshly opened terminal is in your home folder by default, you would need two commands to get to the Programming folder:
C:\Users\donkey>cd Documents C:\Users\donkey\Documents>cd Programming C:\Users\donkey\Documents\Programming>
Alternatively you can combine the paths into one command:
C:\Users\donkey>cd Documents\Programming C:\Users\donkey\Documents\Programming>
The above approach uses relative paths. You can also use absolute path. This is particularly useful when you can find the desired file with Explorer but are not certain how to get there in the terminal. In Explorer you can see the absolute path of a folder by clicking on the address bar:
From here you can conveniently copy the path to the terminal as part of a cd command and you will instantly go to the desired folder:
C:\Users\donkey>cd C:\Users\donkey\Documents\Programming C:\Users\donkey\Documents\Programming>
If you want to go up in the folder structure you will also use the cd command, but instead of a folder name you give it two periods (or dots, however you wanna call them):
C:\Users\donkey\Documents\Programming>cd .. C:\Users\donkey\Documents>cd .. C:\Users\donkey>
Terminal and Folders (OS X / Linux)¶
Using terminal is generally more ingrated into graphical user interfaces in Unix-based operating systems. Just like in Windows, there's always an active folder (i.e. the folder where you are at the moment). Text commands are used to move between folders and to see their contents:
cd: (change directory) this command is used for moving between folders using eitherrelativeorabsolutepath.
ls: (listing) this command shows the contents of the current folder and is quite often used as
ls -lhwhich displays more information.
When you open the terminal it generally puts you into your home folder. The default structure of your home folder usually varies between OS X and different Linux distributions. However you can generally expect to find subfolders like Downloads and Documents. Usually the folders can be seen in the operating system's file manager using their real names (unlike Windows).
For example let's assume you've created a folder called "Programming" inside your "Documents" folder. Since a freshly opened terminal is in your home folder by default, you would need two commands to get to the Programming folder (note: the ~ character is a shorthand for your home folder):
donkey@linuxbucket:~$ cd Documents donkey@linuxbucket:~/Documents$ cd Programming donkey@linuxbucket:~/Documents/Programming$
Alternatively this can be done with one command, combining the folders into one path:
donkey@linuxbucket:~$ cd Documents/Programming donkey@linuxbucket:~/Documents/Programming$
In Unix-based systems it is often possible to open a terminal from the file manager so that the active folder will be the one you started it from. Generally this is done from the menu that opens with a right mouse click in the file manager window. It's also often possible to copy the absolute path of a folder from the file manager and use that in the terminal:
donkey@linuxbucket:~$ cd /home/donkey/Documents/Programming donkey@linuxbucket:~/Documents/Programming$
Finally you can go up in the folder structure with the same cd command, just using two periods/dots instead of a folder name
donkey@linuxbucket:~/Documents/Programming$ cd .. donkey@linuxbucket:~/Documents$ cd .. donkey@linuxbucket:~$
Useful Terminal Tips¶
There are a couple of beginner level tips that make terminal much less tedious to use. First of all you can browse past commands with the up and down arrows. For instance if you run your Python program repeatedly, you don't need to retype the entire command - just press up once and you're ready to go again.
Another useful tip is tab completion. You can type only part of a folder or file's name and press the tab key. If there is exactly one name matching the start that you wrote, the OS will fill the rest for you. If there's more than match, behavior will be different depending on your OS.
- Windows offers the first matching name and these can be cycled by pressing tab again
- E.g. in the home folder typing D and pressing tab gives Desktop, pressing again gives Documents and finally Downloads
- Linux completes the path as far as it can and then expects more characters to identify which one you wanted.
- E.g. typing just D and than tab in the home folder gives nothing - however pressing tab again gives a list of matching names
- In order to identify Documents, you'd need to type at least Doc
Python consoleexamples in the course material have been written in IPython. It's a significantly more convenient replacement for the default Python console. We didn't install IPython earlier because it needs to be done in the
terminal. Furthermore, if Python was installed with admin privileges, you need to install IPython (and other Python libraries later!) with admin terminal. In Windows this can be done by right-clicking on the cmd program in the Start menu and selecting "Run as administrator", as shown below.
pip install ipython
or if you have both Python 2 and 3 on your system, type
pip3 install ipython
If the operating system says it cannot find the pip command your Python's scripts folder is not in the PATH environment variable. The easiest way to fix this is to just reinstall Python and make sure you had "Add Python 3.8 to PATH" selected. This will also happen if you didn't install Python with admin priveleges.
Pip might also inform you that there's a new version of it available with a yellow warning message as displayed in the screenshot below:
There is no particular need to update pip right now, but if you don't want to see this again, you can do what it asks you to, and update it by typing:
pip install --upgrade pip
Python consoleis a program where you can write codes and it executes each line, showing you the result (if any). In order to use the console, go to the
ipython. Once you have opened the console you should see
In :in the beginning of the latest line. This is an indicator that you are now in the Python console.
You can now try some simple mathematical
operationslike addition, substraction, division and multiplication with numbers. Congrats, you can now use Python as a calculator!
In : 1 + 1 Out: 2 In : 5 * 3 - 4 Out: 11 In : 9 / 2 Out: 4.5
In order to continue to the next step you need to return to the
terminalby exiting the
Python console. As long as you see
In [N]:(where N is a number) you are in the Python console. To exit, press Ctrl + D on your keyboard and pressing Enter when asked for confirmation. Alternatively you can type
quitto the console.
Please note that if you rebel against our recommendations and choose to use the vanilla Python console, exiting is done differently in Windows. You need to press Ctrl + Z instead, and when you see
^Zon the screen, press Enter to exit. On Linux and Mac OS X there's no difference, just press Ctrl + D to quit. You can also exit the vanilla console by typing
Running Python Code Files¶
When making Real Programs (tm) instead of playing around with code one line at a time in the console, the code is written in a
code file. In order to run code files from the
terminalyou need to first be in the folder that contains the code file. Moving there is done with
cdcommands. After finding the correct folder you can execute the program by typing
ipython mycodefile.pywhere you would of course replace mycodefile with whatever your code file's name is.
For example we can consider the above code file, saved as hello.py and residing in the Downloads folder. In order to run this program the first step is to move to the Downloads folder with
cd Downloads. After this the code can be ran with
ipython hello.py. Download the file from the link above (the downward green arrow) and try it out!
Please note that the
file extensionmust be included in the name. The extension for Python code files is always
There's actually not much benefit for running code files with IPython - in fact it can be a lot slower. The only reason we're doing it in this course is that IPython decorates error messages into much more readable form. This way we also avoid learning to read two kinds of error messages since we get similar errors from both the console and the code files.
These pre-exercises will make you a little bit familar with writing Python code and giving instructions to the computer. The exercises use the Turtle module - included in the default installation of Python - to draw some simple shapes. Turtle has quite a few handfuls of commands but these exercises only use a few of them. Turtle will also be used later in the course for so called warmup exercises.
Give feedback on this content
Comments about these instructions