In this 0th exercise we will install useful tools for the course and get familiar with basic usage of Python. This is done now so that you wouldn't have to deal with the practicalities once we get along with the actual programming exercises.
Learning goals: After this exercise you know how to use Python. This includes using the interactive interpreter and running program files from the command line. You will also get familiar with a text editor better suited for the purpose of programming.
We will need to have installed two basic things to get started: Python itself, and a suitable text editor to write our programs in. Some other tools are mentioned that might come in handy at some point. Follow the instructions for your operating system in each section.
Important! The course uses version 3 of Python which is not entirely backwards compatible with code from older versions - so make sure you get the right version when installing! The current version is 3.4.3 and can be downloaded from the https://www.python.org/. Detailed intsructions below.
The Windows install requires administrator privileges on your user.
- Start by getting the install package from the only official source and saving it somewhere convenient.
- Start up the installer python-3.7.0.exe. Windows may think it's from an untrusted source, but just go with it and proceed with the install.
- Follow the install and press Next whenever applicable. When the installer asks for which components to install, find the "Add python.exe to Path" option and make sure to check it (see picture below).
Yay, we has pythons! Keep reading...
Mac OS X¶
OS X provides Python version 2 along with the operating system, which is not what we're looking to use, so we need to install Python 3 ourselves.
- Download the installer from python.org and run it on your computer.
- Click through the installer, there are no options to check.
That was it. Now you should be able to run Python from the command line using the command
python3- more on that further down.
In most Linux distributions you can install Python directly using your package manager. It is however possible that you have Python 3 already installed as
python3- try it. Probably you know your Linux better than we do, so we won't go into details here.
If your package manager does not include Python, you'll have to install it by compiling the source code. Once downloaded, proceed with the following commands:
- cd /directory/of/your/download
- tar xvzf Python-3.7.0.tgz
- cd Python-3.7.0
- make install
If the install complains about missing libraries, install them the way you would otherwise.
A good text editor helps a lot in when programming. Useful features include clearly visible row numbers, possibility to have multiple files open, colour highlighting to make code easier to read. Good text editors can do that, and much more. You may also want to use an entired integrated development environment (IDE), in which case you can pass this section and move on to the next. IDEs offer even more features that may aid your programming, but their use requires a bit more getting used to and may be a bit harder on your computer's memory.
For windows we recommend the program Notepad++. It is free, and fills all requirements of a good text editor. It is also found on the computers in the exercise classrooms, so it will become familiar that way too.
- Download the install package from this link.
- Open the .exe-file
- Tell Windows that yes, you do trust this program.
- Choose english as your language of choice.
- Follow intsructions. Default options are fine.
After install, open up the program.
- Go to Settings -> Preferences...
- Look for the Language Settings tab and check
Replace by space(picture below)
- Look for the New Document/Default Directory tab and from the Encoding preferences choose
UTF-8 without BOMand check the box below. Lastly choose
Windowsfor the Format option.
You can change other options to your liking. These two changes help avoid mysterious errors in the future, especially if you work on multiple computers.
Max OS X¶
There are many good text editors for OS X, for example the free and open source Atom. Atom comes with python support preinstalled and should be straightforward to use without much configuration. It can be customised extended with packages available straight in the editor. You can also open files straight from the command line using
atom example.pywhich is convenient when working with Python.
There are many options for Linux as well, for example built in editors such as Gedit (Gnome) or Kate (KDE). Atom mentioned above should also be alright. If you're an avid command line user you probably know your editor of choice already, but Emacs and vim are some if you want to try.
PyCharm development environment¶
PyCharm is an IDE that does much more than a simple text editor. It features a built-in Python console and interpreter which saves time otherwise spent running your programs from the command line. PyCharm is a significantly much heavier program than the editors mentioned and probably uses a bit more memory. The learning curve is also a bit steeper, as all code must be placed in a project structure which isn't always optimal with simple scripts.
Command line tools¶
Both Linux and Mac OS X com preinstalled with decent command line tools for programming. Windows' command line on the other hand is somewhat lacking. You can manage with it, or you can make your life easier with ConsoleZ, a better command line. It is also found on the virtual machines for you to try.
- Download ConsoleZ from Github.
- Unzip the package into a suitable directory. It requires no separate install.
- Open it by double clicking console.exe.
- You can make a shortcut to the program and place it in our start menu or desktop.
From the preferences you should turn on Copy on select, so you can copy text easily just by selecting.
- Open Edit -> Settings...
- Choose Behavior from the right
- Check "Copy on select"
Using the command line¶
On Windows, open the Run menu via Windows key + R. Write
cmdinto the field and press enter. If you installed ConsoleZ, open it through its shortcut.
On Linux you can open a console by pressing Ctrl+Alt+T or Alt+F2 and writing Terminal. On a Mac you can find Terminal using Spotlight in the upper right corner or Cmd + Space.
In the command line you can navigate the directory tree using the command
cd Documentsto navigate to a directory called Documents.
The Interactive Python shell¶
In the interactive Python shell we can run Python code one row at a time. We can open the shell using the command
pythonon Windows or
python3on most flavors of Linux and OS X. Using
pythonin the latter will typically start up Python 2. If you are sure you have installe Python, but the windows command line complains samething like the following:
'python' is not recognized as an internal or external command, operable program or batch file.
Python has not been installed into the PATH environment variable, which defines what commands are available on the command line.
When you manage to open the Python shell in the command line, you will see the symbols
>>>. Now you can try to write som mathematical expressions for Python to interpret, for example, addition, multiplication and division. You notice that Python is easy to use even just as a calculator.
>>> 1 + 1 2 >>> 5 * 3 - 4 11 >>> 9 / 2 4.5
You can exit the Python shell using Ctrl-D on any system.
Running Python program files¶
When we want to make proper programs and not just run code line by line, we need to write our code in a source code file. To run Python files from the command line, you have to navigate to the directory with your file using
cdand then use the command
python3 myfilehereon Linux & OS X), replacing myfilehere with the file name you want to run.
For example to run the above file heimaailma.py located in the Downloads directory, you would navigate using
cd Downloads, and then running
python3 heimaailma.py. Download the file from the link above to your Downloads folder and try!
Note that the file ending needs to be included in the filename. For Python program files this ending is always
These preliminary exercises help you get to know writing Python code and giving instructions to the computer in a very simplified manner. These exercises use a library called Turtle that comes with a normal Python installation. The library can be used to draw simple graphics on the computer screen. Turtle has a lot of commands for drawing, but we'll use just a handy subset of them - we'll also let you know which ones. Future exercises will also have an introductory Turtle assignment.
Give feedback on this content
Comments about these instructions