1. Table of contents
  2. Built-in data types (next)

Getting started

  1. Top
  2. Setting up the working environment
  3. On Being (Pythonic)


Getting started

Congratulations, you've set out to learn to program. You're certainly conscious that it will take time and effort, and that taking shortcuts will only come back to haunt you in the end. If so, the most important prerequisite is met and we can get started.

There is a certain amount of material to get through as you learn your first programming language before you can write any kind of meaningful program. Until then, you'll be dealing with new and abstract concepts which will be difficult to understand and to put into practice. It is only once you start putting things into practice that you'll find you are really starting to "get it". Something else has to keep you interested in the meantime, something familiar, and to that end I'll give you practical examples and applications of each new concept and with each chapter will provide exercises that will yield useful programs. There's only one way to make certain these examples remain interesting and familiar, and that is for you and me to agree on a subject that interests us both. The subject here is music and musical composition. I hope we can agree.

By the end of this course, we will have written several programs that I hope you will find useful in your musical practice. There will be examples of programs dealing with a variety of compositional approaches from classical period tonality to spectral music through post-tonal and serial music. We will be writing programs that can generate MIDI and OSC data, musical notation, and that can generate scores algorithmically. We'll be covering a lot of ground both as composers and programmers. By the end of this course you will have seen the better part of the modules available to you with a standard Python installation and you will have the solid foundational skills you will need to continue your learning independently. You will also have a good basis in the general programming skills you'll need when you decide to learn your second programming language. But before stepping onto the path we must first put on our boots, so let's not get ahead of ourselves and let's start by setting up our working environment.

Setting up the working environment

Installing Python 3.7

First, we'll need to install Python 3.7, which is the version we'll be using throughout this course. Everything you learn here will be applicable to Python 3.7 and above, up to version 4 (which, at the time of this writing, is not likely to be released and become authoritative anytime soon).

If you're running either MacOS or Linux, you already have Python installed, but it's likely to be version 2.7. If you're running Windows it's still worth checking if you have Python installed. Either way, open up a terminal (or a command prompt on Windows, but I'll henceforth use the term terminal) and type python. It might look something like this (with minor differences depending on your platform) :

$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.

You are now in an interactive language shell, that can Read Python commands, Evaluate them, and Print their results in a Loop (also known as a REPL).

To exit the shell, type exit().

You can find binaries of the latest version of Python 3.7 for MacOS and Windows on the official downloads page. If you're running Linux, I'll assume you know how to use your distribution's package manager and recommend you search for the package python3.7.

I won't be spending any more time on how to install Python here, because it is OS dependant and there is an abundance of online resources available. Once you've completed your installation, open up the terminal and this time type python3.7. You should see something like this :

$ python3.7
Python 3.7.1 (default, Nov 26 2018, 10:51:28)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Choosing a text editor

The interactive shell will be a useful tool for prototyping and trying out ideas, but when it comes to writing applications you'll need a text editor for editing program files that will persist beyond an interactive session and that you can share. Text editors come in many flavours and choosing one really comes down to personal preference in the end. I'll list some of the most commonly used editors here, and will recommend Atom as a simple, multi-platform, and powerful editor.

Virtual environments

I understand that the following section won't mean much to you at the moment, but I would like to start developing your good habits early on. I apologize if this seems to be me asking you to "take my word for it".

When you start writing applications, you'll find it useful to isolate your projects from one another. It could be, for example, that they have conflicting requirements or that they run on different versions of Python. To avoid having your hard drive turn into a mess, or to have your programs crash after a system update, it is good practice to create virtual environments for your application to run in while you're developing.

First, you'll need to use Python's package manager to install virtualenv. Type the following command at your terminal :

$ pip3 install virtualenv

You can now create a virtual environment in your project's root directory by typing the following command at a terminal :

$ virtualenv venv -p python3.7

This will create a virtual environment named venv (a conventional name) that runs python3.7. You then need to activate the virtual environment by typing (on Linux and Mac OS) :

$ source venv/bin/activate

On Windows you'll need to type :

$ source venv/Scripts/activate

Your terminal might now look something like this to indicate that the environment is active :

(venv) $ 

Your terminal will work just as it would outside the virtual environment, with the exception that your default version of Python is now 3.7 and that any additional packages and requirements you might install will be safe from changes made outside the environment. To deactivate the virtual environment, type :

(venv) $ deactivate

If you encounter difficulties getting to this point, have a look at virtualenv's documentation.


The last thing you'll need is a version control system. This allows for many things, but primarily it will allow you to save and release stable versions of your program as you continue to develop the application, without having to make several copies of your code. The system used throughout this course is Git. Using Git will also mean you'll be working with the version control system used by Github and Gitlab, platforms for storing your code online, managing your projects, and sharing open-source software. Again, this may seem a little advanced at this stage, but best to start with good habits.

If you're on Linux, Git is already installed on your system. If not, you can find binaries at Git's official downloads page. There will be an introduction to using Git when we start writing program files in a later chapter.

With that, we have a complete working environment.

On Being (Pythonic)

In closing this chapter, and in lieu of exercises, I'd like to introduce two documents that speak of conventions in Python programming. The first is a rather dry style-guide, and the second a witty list of recommendations that is often self-contradictory but that concisely summarizes what it is to write Pythonic (idiomatic) code. I hope you'll find yourself going back to these documents as you learn more about Python. In the case of PEP-8, I expect you'll be refining your style as you progress, and with respect to the Zen of Python, well, I expect you'll find it funnier as you start catching the references.

You can find Pep-8, the official Python style guide, here (and there's really no need to read the whole thing at this stage...).

Finding the Zen of Python is a little trickier, but here's the trick. Open up a Python interactive shell (by typing python3.7 at a terminal) and then type this :

>>> import this