0% found this document useful (0 votes)
1 views

Programing With Psychopy

Chapter 2 introduces programming, emphasizing Python as a high-level, user-friendly language for giving commands to computers. It covers the installation of Python and PsychoPy, basic programming concepts, and the importance of lists and variables in coding. The chapter also highlights Python's history and community support, while providing resources for further learning and tutorials.

Uploaded by

zoeanwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Programing With Psychopy

Chapter 2 introduces programming, emphasizing Python as a high-level, user-friendly language for giving commands to computers. It covers the installation of Python and PsychoPy, basic programming concepts, and the importance of lists and variables in coding. The chapter also highlights Python's history and community support, while providing resources for further learning and tutorials.

Uploaded by

zoeanwar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Chapter 2

Programming
Is Fun

A programming language is a way to give commands


to a computer. These commands are written as text and
when we put many commands together we call that
a program. Python® is a high-level, easy-to-read, inter-
preted language. We will see how to install Python
together with an application called PsychoPy (which
will be discussed in Chapter 3). We will also see how
to write simple commands, perform mathematical op-
erations, and control the flow of the program with if, for
and while statements.
20 Chapter 2 · Programming Is Fun

We have seen in Chapter 1 that illusions are fun. In Chapter 2 you will see that pro-
gramming is also a lot of fun. OK, maybe not in the same way, and not without a little
effort, but the effort required for what we will do in this book will not take much time,
and the fun will continue forever (once you have started programming).
Programming is about asking a computer to do a series of tasks. Computers are
clever and there is a lot that they can do. This list of commands needs to be written as
text, therefore a computer program is always a text document, called the source code,
but we will use the more general term program.
In order to be understood by the computer the program needs to be in a computer
language, and therefore learning to program is about learning a new language. You
may be familiar with the names of several programming languages such as Fortran,
Basic, Pascal, C, Java™ and Python. They are a bit like French, Italian, Russian and so
on; some things are common to all languages (the distinction between a verb and a
noun) and some are specific (like learning the word for cat).
Because programming, in addition to knowing the rules of the language, requires
a certain organisation and strategy, people debate whether programming is a skill, a
craft, or an art. It is probably all of these things, which is what makes it fun in the sense
that it allows us to be creative.
This is a very brief introduction to one general-purpose language called Python. I
will discuss how to write simple commands, and also mention the few quirky aspects
that may not be intuitive, like the fact that we will have to remember to count starting
from 0 instead of 1.

Python®

It was Christmas time in 1989, in Amsterdam. Guido van Rossum (then 33) decided to
write something that would go beyond the limitations of the languages he had worked
on before, but without trying to come up with the perfect language (as a hobby project,
something fun to do). We have all done that, creating something revolutionary over the
winter holidays just because we were bored!
The name Python has nothing to do with snakes. Guido chose it because he was a fan of
a TV show called Monty Python’s Flying Circus. If you are too young to know who Monty
Python are, do some research and watch some of their classic sketches. Then try to drag
yourself away from YouTube and back to this chapter. The link with the TV show lives on
because the official documentation often contains references to sketches from Monty Py-
thon (so don’t be too surprised if the documentation refers to dead parrots for instance).
The project captured the interest and admiration of lots of other people and today
Python is one of the most used and important languages around. True to its origi-
nal philosophy, it is free and open-source, and has a community-based development
model. The development is managed by the non-profit Python Software Foundation
(www.python.org/psf/).
Because of its popularity you will find a large amount of useful information and
tutorials online. The Python Software Foundation webpage is a good starting point.
This chapter cannot be as detailed as an online tutorial; we will instead focus on the
key concepts. A list of online tutorials is provided in Box 2.1. One word of cau-
tion, Python 2 is a bit different from Python 3, I will only talk about and use Python 2
(which is still the more popular version).
Setting Up 21

Box 2.1. Online tutorials and books

There are the links to some tutorials for beginners specific to Python 2.

 This first one is directly from the Python Software Foundation:


https://docs.python.org/2.7/tutorial/
 This is a tutorial created for programming games but useful for anybody even with no pro-
gramming experience:
http://sthurlow.com/python/
 This is a very short introduction that fits on one (long) page, created by Magnus Lie Hetland:
http://hetland.org/writing/instant-hacking.html
 This tutorial is for non-programmers and is a featured book on Wikibooks:
http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6
 For PsychoPy there is a reference Manual here:
http://www.psychopy.org/api/api.html

These two are tutorials that introduce Python as well as PsychoPy.

 The first one is from the Technical support group of the Radboud University in Nijmegen
(Netherlands):
https://www.socsci.ru.nl/wilberth/psychopy/index.html
 The second is from the GestaltReVision group (University of Leuven, Belgium):
http://nbviewer.jupyter.org/github/gestaltrevision/python_for_visres/blob/master/index.ipynb

Although they are not tutorials, Jon Peirce has also published a couple of journal articles about
PsychoPy. These are aimed at the academic readers.

 Peirce JW (2007) PsychoPy – Psychophysics software in Python. J Neurosci Methods 162:8–13


 Peirce JW (2008) Generating stimuli for neuroscience using PsychoPy. Front Neuroinform 2:10

Finally, although there is so much information available online, if you prefer to hold in your
hands a book made of paper, here are two options (among many):

 Shaw ZA (2013) Learn Python the hard way: A very simple introduction to the terrifyingly
beautiful world of computers and code. Addison Wesley Press, Reading MA
This is the 3rd edition of a book that gives step-by-step instructions.
 Briggs JR (2012) Python for kids: A playful introduction to programming. No Starch Press, San
Francisco
This instead is written for kids, but nevertheless it is a proper introduction to Python and can
be used by anybody new to programming.

Setting Up

Setting up is not the most fun part, but it will not take long and it needs to be done
only once. The first thing to do is to install Python on your computer. Here is the good
news: it is free, easy and possible on any major operating systems. Because of wanting
to create illusions we will install it together with some useful libraries and packages.
In our approach this is going to be a single download. The instructions to download
and install Python together with an application called PsychoPy are in Box 2.2. I will
explain more about this application and these libraries later, so for now just follow the
instructions as a recipe.
22 Chapter 2 · Programming Is Fun

Box 2.2. Downloading Python and PsychoPy

Using a web browser go to www.psychopy.org.


On the right side you will see a link called download. This will take you to a different location
where the files are available. Here you will see a list with many different versions. We will use
version 1.84.02 and therefore you can ignore everything else and select the file for the Operating
system that you are using. For example:

 StandalonePsychoPy-1.84.02-OSX_64bit.dmg (if you use a Apple Macintosh® computer)


 StandalonePsychoPy-1.84.02-win32.exe (if you use Microsoft Windows®)

It is quite possible that by the time you are reading this the latest version has a higher version
number. Older versions remain available by following the link to
https://github.com/psychopy/psychopy/releases.
The word standalone refers to the fact that in a single file you are actually obtaining everything
you need, including Python itself.
Mac® users should place the PsychoPy application inside the Applications folder. After instal-
lation MS Windows® users will find a link to PsychoPy in

 Start > Programs > PsychoPy2

There is also a Configuration Wizard with more information about the setting up.
The standalone file is large and it may take a bit of time for the download to complete.
As there are different versions of Linux, if you are a Linux user you need to read and follow the
more specific instructions available here: http://www.psychopy.org/installation.html
The programs that we are going to write were created on a Mac®. However, it is my belief that
they are suitable for any environment without modification.
The fact that there are many versions of PsychoPy (we will use 1.84.02) is a clue to the fact that
this software is under active development, and improvements are released regularly. However,
do not worry about which version you are using. It is not a good idea to feel that you have to race
and get the one just released (although it is tempting).

Introduction to Programming

Since we have now downloaded and installed both Python and PsychoPy in a single
step, we will use the PsychoPy application to learn a bit more about Python. Remem-
ber, Python is a language and for now we will focus on the structure of the language,
so what we are doing is very general. We will issue a command and execute it by typ-
ing some text and then pressing the return key. This is called using Python in inter-
active mode.
The first step is to start the PsychoPy application that you have installed. Then in-
side the application go to the View menu and select Go to Coder view (on Windows®
this may say Open Coder view). This is an important step because if you are seeing the
Builder view instead of the Coder view nothing in this chapter will make any sense.
You should see a window similar to the one in Fig. 2.1 (this in particular is how it
looks on an Apple Macintosh®). Note the two main horizontal panels. In the lower
one there are two tabs called Output and Shell. Click on Shell and you will see a text
that will inform you about which version of Python you are running. On my compu-
ter it looks like this:
Introduction to Programming 23

Figure 2.1. The PsychoPy application (Coder view) showing two panels. The top panel is the Editor and
is empty for now. The bottom panel has two tabs: Output and Shell

PyShell in PsychoPy – type some commands!

Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39)


[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

We can use this window to try out some Python commands and for now you can
ignore the rest of the interface. Check that you see the prompt inside the Shell win-
dow. It looks like this: >>>
This means that the Python interpreter is listening to you. In other words Python
expects an input. When Python will produce an output there will not be a prompt.
It is traditional to start by asking a computer to type the words “hello there”. So
please type the following followed by the return key.

>>> print("hello there")


hello there

The text should appear on a line below your command (without the prompt) as
shown above. Congratulations, you asked the computer to do something and it did.
Basically the shell environment in which you are working compiles and executes any
command right away.
24 Chapter 2 · Programming Is Fun

You have also learned that quotes identify a piece of text, and the text can include
multiple words. Special words like print will appear automatically in blue, and any text
like “hello there” will appear in red.
Now for some numbers and some variables. A variable is something that can take
values, so the name of the variable is the name of the container.

>>> a = 0.5
>>> b = a * 2
>>> c = "word"

Variable a will have a value of 0.5, variable b 1.0 and c will be a string (text). No need to
worry about the fact that these values are of different types, you can rely on Python to be
able to cope with any kind of data. How? Basically by making an educated guess. This is
known as duck typing. This name comes from the saying that if something walks like a
duck, sounds like a duck, and swims like a duck, then it is probably a duck. I am not kid-
ding, duck typing is a genuine technical term. For example by noticing that you used quotes
around “word” Python knows that it is a string and the text editor has colour coded it as red.
Note that when you typed these lines nothing happened. The command print made
something happen, but to fill a variable with a value will not generate an out. If you want
to see the content of a variable you need to type its name followed by return, or ask for it
to be printed.

Figure 2.2. This is the PsychoPy application (Coder view) showing a few lines of commands in the Shell
window. Here every Python command that is typed is immediately executed. Note that the size of the
panels can be adjusted by dragging the handle (small dot) in the middle (highlighted in red)
Lists and Strings 25

In the case of print you can ask for several variables to be printed by writing the
names separated by commas.

>>> print(a, b, c)
(0.5, 1.0, 'word')

In a new line, without the prompt, you can see the values 0.5, 1.0, and ‘word’, as
shown also in Fig. 2.2. This time the text ‘word’ is within single quotes ('word') rather
than double quotes (''word''). Either way is OK for Python.
A list of the main built-in data types in Python, with examples, is given in Table 2.1.
We have started to see values for integers (int), floating-point numbers (float), and
strings (str). We will discuss lists next, they are very important.
Even though they are in Table 2.1 I will not say much about dictionaries because
we will not have a need for this type of data. Briefly, they map a unique key (before
the :) with a value (after the :). So you can see how they would be quite useful if you
wanted to store for instance details of your cats (for each one a colour, a birthday, and
so on). In this book we will focus on programming visual illusions, and therefore the
most important types are the numerals, the strings for the text, and the lists for things
like positions of vertices.

Lists and Strings

We often need to store a list of values. To do so we format the values with square
brackets and separated by commas.

>>> aList = [12, 8, 4]


>>> aListOfLists = [[12, 8, 4], [2, 0]]
26 Chapter 2 · Programming Is Fun

Lists are extremely important and we will make good use of them. The first exam-
ple is a list with three elements, the second is a list of two elements (each with its own
elements inside) and therefore it is a list of lists. You may have already guessed, but
you could also have a list of lists of lists, and so on. Also, lists can contain elements
other than numbers.

>>> myCats = ['Sheba', 'Simba', 'Smudge']

They can even contain elements of different types.

>>> myList = [1, 'Sheba', 2, 99]

Next we need to know how to read values from a list. They are accessed with an in-
dex for the position of the element, starting from zero. Therefore try the following.

>>> print(aList[0])
12
>>> print(aList[2])
4
>>> print(myCats[0])
Sheba

Note that aList[3] is out of range. That means that we are looking at a position that
does not exist and we will get an error. This is because our list has only three positions,
0, 1 and 2. Python is not the only language that counts from 0 instead of 1, it is some-
thing that computers like to do so we will have to get used to it.
You can also see a convention about naming variables. To make them easy to read
we capitalise any new word within a complex name, for example we wrote aListOfLists.
This is optional, not something necessary in Python, but we will follow this custom in
writing variable names in this book.
Indices are very clever, for example you can give a range. aList[0:2] is a list with two
elements (the first two). Another clever thing is that you can count from the back us-
ing negative numbers, so aList[-1] is the same as the last element which in our example
is aList[2], and therefore it has a value of 4.
Given that we can specify a specific location we can use this to change a value in-
side the list. We specify the element using the index, and then use = to assign it a
new value.

>>> aList = [12, 8, 4]


>>> aList[0] = 1
>>> print(aList)
[1, 8, 4]

What happens if we use round instead of square brackets? Actually more or less the
same, you can also write lists that way, but in Python these are not called lists, they are
called tuples. Once they are created tuples cannot be changed, and therefore are less
Lists and Strings 27

flexible than lists. For instance what we did in assigning a new value to aList[0] would
give an error for aTuple[0].

>>> aTuple = (12, 8, 4)

If we were to check whether aList is equal to aTuple we would find that this is False
(they have the same numbers inside, but they are not the same thing). We will mainly
use lists rather than tuples.

>>> aList == aTuple


False

This comparison is False, but note that we need two equal signs to compare two
variables. One equal sign simple replaces what is in the first (right side) with the sec-
ond (left side). It’s an easy mistake to make. Try to remember that = is always active-
ly doing something, not making a simple comparison. The main Python operators,
like ==, are listed in Box 2.3.
In a complex program we will need comments as well. Comments in Python start
with the hash character, #, and extend to the end of the line (so there is no need to have
an # at the end).

Box 2.3. Python operators

This is a list of the main built-in Python operators to perform comparisons. The result from any
such comparison can only be True or False.

 == equal to
 != not equal to
 > larger than
 >= larger than or equal to
 < smaller than
 <= smaller than or equal to

This is a list of the main built-in Python math operators (to perform arithmetic operations).
The result from any such operation is a number. Some are not used very often, so for example
although the modulo operation (%) can be quite useful do not worry if it seems unfamiliar to
you at the moment.

 a+b addition
 a–b subtraction
 a*b multiplication
 a/b division (can give an integer or a float)
 a // b division and rounding of the result to the lower whole number
 a%b modulo. It gives the remainder of a divided by b.
Examples: 11 % 2 is 1, and 11.5 % 2 is 1.5 (what is left from the division)
 a**2 a to the power of 2, the same as a multiplied by itself: a * a
 a**b a to the power of b
 –a negation (changes the sign of a)
28 Chapter 2 · Programming Is Fun

Python As a Pocket Calculator

Now let’s practice some simple maths. Here is a bit of arithmetic (followed by a
comment, which of course plays no part in the maths, it is shown in green here and
comments will be green also in all our programs). What is the result of this opera-
tion?

>>> 4 + 3 * 2 #a bit of arithmetic, four plus three times two


10

The result is 10. If you thought it should be 14 this is a mistake (easy mistake to
make, it was a bit of a trick question). Just remember the rule of precedence of op-
erations: power, division, multiplication, addition, subtraction (PoDMAS). So the
multiplication happens first even if it is written after the addition. We can avoid any
ambiguity by using brackets when necessary, as in (4 + 3) * 2.

>>> (4 + 3) * 2 #a bit of arithmetic, four plus three, and the result of that times two
14

Next, what is the result of this operation?

>>> 10 / 4 #ten divided by four


2

That’s easy, it should be 2.5. Well, your maths is correct but unfortunately this
was another trick question. Really tricky this time. Because we expressed the di-
vision in terms of integers (whole numbers without decimals) we get an integer
and the result is 2. Annoying perhaps, but easily solved by remembering that if we
want operations with decimals we should include a decimal point.

>>> 10. / 4.
2.5

The result now is 2.5, and you don’t even need to type the zero because 10. (ten
followed by a full stop) is the same as 10.0 for Python.
OK, enough trick questions, now for text, you can use single or double quotes
around text. The latter has the advantage that it copes with having apostro-
phes (single quotes) inside. So you can write “Father’s day” using double quotes
around it.

>>> print("Father's day")


Father's day

You can do clever operations with strings, using + and *.

>>> s = "hello" + " there"


>>> father = "pa" * 2
Python As a Pocket Calculator 29

You may experiment and use print to see the results. Quite logically the results are “hello
there” (placed inside a variable called s) and “papa” (placed inside a variable called father).
We can get the length of a string with len(). Therefore, len("Father's day") is 12, the total
number of characters including spaces. This command applies to lists as well, for exam-
ple len([1, 2, 8]) is 3.
Just like len(aList) provides the length of the list, there are other similar built-in
functions, such as max(aList), min(aList) and sum(aList). They find the maximum value,
the minimum value and the sum of all values.

>>> sum([1, 2, 10])


13

What if a list has text inside instead of numbers? max and min will treat strings in al-
phabetical order, so max will give the element that comes last alphabetically, but sum
will not work with strings. Here are a couple of simple examples using “Father’s day”.

>>> len("Father's day")


12
>>> max("Father's day")
'y'

One final point about numbers and strings. And here you will see how Python can
be really clever and flexible with data types. Suppose you have a number in a variable
called, say, number.

>>> number = 5

What you want is to use it as a string, mixed in with some other text perhaps. You
can turn the number into a string by saying the following:

>>> numberAsText = str(number)

Using the built-in function str() we are asking Python to treat what is inside the
brackets as a string. And you can also go the other way:

>>> numberAsNumber = int(numberAsText)

This gets back a number again, something on which we can do some maths. This
works for other types of variables as well. So here is another way to obtain 2.5 (the
correct result) when diving 10 by 4. We can use float() to make sure that an integer is
treated as a floating point number.

>>> float(10) / float(4)


2.5

As we have seen, inside the Shell window Python tries to execute each command
typed after the prompt, and it will provide an error message if there is a problem. We can
30 Chapter 2 · Programming Is Fun

Figure 2.3. The Shell window of PsychoPy showing the error message generated when trying to divide 10 by 0

generate an error for example if we try to divide a number by zero (something that is not
possible as there is no defined result). You can see the error message below and in Fig. 2.3.

>>> 10 / 0
Traceback (most recent call last):
File "<input>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

This is another example of an error generated when trying to find the sum of a string.
In this case the message says that the operation is unsupported for this data type.

>>> sum("Father's day")


Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Controlling the Flow

We have seen how to issue simple commands, and put values in variables. But the real
power of a program is when we write a series of commands and control how they are
executed. This is done with special instructions that control the flow of the program.
There are three control flow statements in Python: if, while, for.
Controlling the Flow 31

Here is a first example that uses a for loop.

>>> aList = [1, 2, 3, 4, 5] #creates a list with numbers


>>> for item in aList: print(item) #loops over all the elements of the list and prints each one
1
2
3
4
5

This loop prints all the numbers in the list. To do that it uses a new variable item
not used before. This variable is created within the for statement. Also note the :
before the print command, this punctuation is necessary for any control flow situ-
ation, and you can read : to mean do the following (do say it aloud as you read the
program, it does help).
Special words like for, while, but also not, in, print and others are written in blue in this
book and are automatically coded as blue by default in PsychoPy.

>>> if len(aList) > 0: print(aList[0])


1

This will print the first element in the list. We checked that the list was not empty
with an if and the len() command. This is useful because we can avoid the problem of
trying to print from an empty list (which would give an error).
An if can also be combined with an else. After the word else and : there will be com-
mands executed only when the if condition is False. For more complex situations
where we need to deal with multiple possibilities we can use an elif, which is a contrac-
tion of the words else if. It does exactly what it says, it checks something (but only if
the first if condition is False).
In a program you may want to use a for loop over a large range of numbers. Instead of
preparing a long list we can generate the list using range().

>>> range(20)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

This command is very useful and we just have to remember a few facts about how
it works. range(20) is a list of numbers from 0 to 19. Those are 20 elements in total be-
cause we start from 0, so it does make sense. To get the numbers from 1 to 20 we need
to specify the starting number as well as the stopping number: range(1, 21). Again this
will include 20 but not 21.
Therefore, as we have seen, when range() is written with just one parameter it pro-
duces all the numbers from 0 to the number just before the one given (the stopping
number is never included). If range() is used with two parameters the first is the start
and the second is the stopping number. Apart from that the result is as before, that is
a sequence of numbers excluding the stopping number.
Finally we can even use range() with three parameters, in which case the third is
the step.
32 Chapter 2 · Programming Is Fun

>>> range(1, 21, 2)


[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

This is a list of numbers stepping up two at a time. In this case this means all the
odd numbers from 1 to 19 (again no 21, as before the last is always excluded).
You see here an example of how flexible Python is, range() can take one, two, or three
parameters. The three parameters are start, stop and step. Stop is not optional, it has to
be there (we need to know when to stop). If one of the others is missing there is a default
value, so if the start is missing the start will be 0, if the step is missing the step will be 1.
Let us make a loop that prints odd numbers from 1 to 19.

>>> for item in range(1, 21, 2): print(item)


1
3
5
7
9
11
13
15
17
19

Now, before reading the solution (just below, but don’t look!), try and write down
(in the Shell window) a way to print all the even numbers from 2 to 20.

>>> for item in range(2, 21, 2): print(item)


2
4
6
8
10
12
14
16
18
20

Well done. You can start from 2 go until 21, and use a step of 2. Remember that
you still have to put 21 as the end of the range, because if you enter 20 you will only
see the numbers up to 18.
We have seen the for and the if, the while control loop works in a similar fashion.

>>> item = 0
>>> while item < 10: item = item + 1
>>> print(item)
10
Indentations 33

This loop will increment the value of item from 1 (the first time, when we add 1
to 0) to 10 (the last time, when we add 1 to 9), but will not execute anything when item
is greater than 9 (therefore we will never add 1 to 10).
Sometimes it is useful to write down a little table showing the values of our varia-
bles on each turn (called iteration) of the loop. That is the value as we get in and as we
come out. Table 2.2 shows how it would look like for our simple example.
A special command is called pass and does absolutely nothing. This can be useful
when we get into a control flow situation that should have no effect.

>>> if myStomachIsFull == True and IAmSittingByTheFire == True: pass

I have taken the opportunity here to show a if followed by two conditions, joined by
the logical operator and. You can see how this is easy to read, and special words like
and and or work exactly as expected. The fact that they are automatically coded in blue
also helps in making clear that they are special words.
You could read this line aloud like so: if it is true that myStomachIsFull and it is
true that IAmSittingByTheFire, then do the following: absolutely nothing.
Here is a different example that uses or instead of and. Note that the two statements
with == are either True or False, and only one of them needs to be True for the if state-
ment to be executed. After the : the command with = assigns a value to the variable
isTimeToEat. = and == should not be confused.

>>> if isLunchTime == True or isDinnerTime == True: isTimeToEat = True

Indentations

There is an important feature of Python, which is very specific to Python and that I can-
not easily demonstrate from the prompt. Often we need to group a whole set of com-
mands together. For example we may need to do that after a if or a for. Instead of using
brackets Python uses indentations. This makes the text very clean and uncluttered, but it
requires careful attention to the size of the indentation, as there can be multiple levels.
34 Chapter 2 · Programming Is Fun

You will see many examples of indentations in the programs but here is a snippet
of code with two levels.

for x in range(-10, 11): #remember these are numbers from -10 to 10 (not 11)
→ for y in range(-10, 11):
→ → position = [x, y]

Now I will write something very similar but with the wrong indentation. This will
produce an error.

for x in range(-10, 11): #remember these are numbers from -10 to 10 (not 11)
→ for y in range(-10, 11):
→ position = [x, y]

This is known as an indentation error. After : (read it as do the following) Python


expects some commands, but the next line is not indented and therefore it is not part
of what should happen within the second for loop. As there are no commands after the
second : this is an error.
When running a script the error message will appear in the Output panel (the one
next to the Shell panel). For an indentation error it will print out the file name (con-
taining the script) and the line on which the problem occurred.

File "/Users/marco/Projects/PythonIllusions/myProgram.py", line 78


position = [x, y]
^
IndentationError: expected an indented block

This is very useful and most of the times you can then fix exactly the line where the
problem is. The example above refers to the name of a file from my computer. We will
learn more about how to save scripts in files in the next chapter.
Box 2.4 illustrates the way to look at indentations. Basically indentations create
blocks, and these blocks are separate groups of commands. Therefore a change in in-
dentation has a direct effect on the execution of the script.
We can fix the problem by moving the third line to be just after the :

for x in range(-10, 11): #remember these are numbers from -10 to 10 (not 11)
→ for y in range(-10, 11): position = [x, y]

However, in most cases it is not practical to write what follows after : on a single
line. We have done it in this chapter because we were just practicing simple one line
commands. In the scripts that we will write as full programs we will need indentations.
When we will have a loop, the many lines of commands that need to be executed with-
in the loop should increase by one level of indentation.
In the example above we have seen a message about the indetation error gener-
ated by Python. However, for complex scripts an indentation error may also simply
change the flow of the commands in a way that does not generate an error. The con-
sequence is that what should be inside a block ends up outside that block. These
Indentations 35

errors are harder to find and solve. The moral is that we always have to pay close
attention to indentation.
By controlling the flow of the commands programs can be clever. We will also see
how to group commands so that these groups will carry our specific tasks. When they
are given a specific definition (a name and maybe some parameters) we will call these
structures, logically, functions, but they will have to wait a bit.

Box 2.4. Indentations

Indentations in Python create blocks of commands. For example block 2 may only be executed
after a condition is checked with an if in block 1, and block 3 may be part of a loop and be control-
led by a for in block 2. In the book the tabs that create indentations are always shown as arrows.
You will not see these arrows when you type in the Editor.
Moreover, you can use tabs or you can use spaces (Python will replace tabs with spaces behind
the scenes). In general is a good idea not to mix tabs and spaces.

Line 1 in block 1
Line 2 in block 1
Line 3 in block 1
→ Line 1 in block 2
→ Line 2 in block 2
→ → Line 1 in block 3
→ → Line 2 in block 3
→ → Line 3 in block 3
→ Line 3 in block 2
→ Line 4 in block 2
Line 4 in block 1

Box 2.5. The Zen of Python in 20 aphorisms

 Beautiful is better than ugly.


 Explicit is better than implicit.
 Simple is better than complex.
 Complex is better than complicated.
 Flat is better than nested.
 Sparse is better than dense.
 Readability counts.
 Special cases aren’t special enough to break the rules.
 Although practicality beats purity.
 Errors should never pass silently.
 Unless explicitly silenced.
 In the face of ambiguity, refuse the temptation to guess.
 There should be one – and preferably only one – obvious way to do it.
 Although that way may not be obvious at first unless you’re Dutch.
 Now is better than never.
 Although never is often better than *right* now.
 If the implementation is hard to explain, it’s a bad idea.
 If the implementation is easy to explain, it may be a good idea.
 Namespaces are one honking great idea – let’s do more of those!
36 Chapter 2 · Programming Is Fun

This chapter has probably set a record as the briefest and most incomplete intro-
duction to Python. Not to worry, what we have learned is enough to start putting
together some interesting programs, or at least it will be after we learn about the
PsychoPy commands in the next chapter. What is necessary is, however, not to simply
read about the way that Python works but to experiment by typing lots of slightly dif-
ferent things, lists, strings and operations from the prompt. Do spend at least ten min-
utes doing that from within the Shell panel before moving on to Chapter 3.
Finally, before we start using Python you may also be interested in the famous
20 aphorisms written by long-time Python developer Tim Peters (Box 2.5). They
give a flavour of the programming philosophy, and some may apply to life in general
(and yes, there are only 19 in the list, it’s a Zen thing).
Chapter 3
PsychoPy
Is Fun

In this chapter we start to use Python within PsychoPy.


PsychoPy is an open-source Python tool that makes it
easy to get images and animations on the screen of
a computer. It includes a text editor and we will see
how to start a program by writing lines and saving
the script to a file. In the process of learning how to
use PsychoPy we also start learning about objects and
object-oriented programming. Once we have created
objects we will learn how to draw them on screen and
after that we will be ready to write our first program
(in Chapter 4).
38 Chapter 3 · PsychoPy Is Fun

But what is PsychoPy? Unlike illusions and computer programming which were the top-
ics of the first two chapters you may have never heard of PsychoPy. PsychoPy is an
open-source Python® tool that makes it easy to get images and animations on the screen
of a computer. It was created for researchers working in psychology or neuroscience,
because they have a need to control images on a computer screen, but this goal of mak-
ing nice images is not confined to those fields and everybody can use it. It is also plat-
form-independent so the same program can run on Windows®, Mac OS X® or Linux.
In Chapter 2 I have introduced the idea that writing a computer program is like
speaking a new language. We have also learnt some of the words of this language. In
Python we know how to say assign a value ten to a variable called a. But thanks to
PsychoPy we will learn more useful phrases, such as draw a red square please.
PsychoPy was created by Jon Peirce (University of Nottingham), and we can read a
special welcome note from him in Message 3.1.

Message 3.1. “Welcome to PsychoPy” from Jon Peirce

I should note immediately that, like Marco


Bertamini, I am not a professional programmer.
My degree and PhD were in psychology and neu-
roscience. I just enjoy writing code. For me pro-
gramming is problem solving, and I really enjoy
problem solving. Some people like the challenge of
cracking sudoku puzzles, but I enjoy working out
what code will make my computer do what I want.
As with sudoku, I sometimes hit a barrier that can
drive me crazy for a while, but usually I solve the
problem and then I’m even more happy that I
worked it out. The difference is that I then have
something tangible to take away and keep (what
do people do with their solved sudokus?).
I began writing PsychoPy because I needed some-
thing where I could change my stimulus dynami-
cally and easily. This was in 2002 and there weren’t
many options for that in those days. Psychophysics
tool-box, the nearest possibility, was not capable of
using OpenGL (see Box. 4.1) for real-time stimuli
back then, so I began writing my own system in
Python. Initially this was written as a proof-of-principal that we could use Python and OpenGL to do
some cool graphical tricks but it just grew. By late 2003 I was developing PsychoPy for my own experi-
ments. I made it available to others online and, gradually, people starting using it and asking me to add
things. Remember, I like problem solving, so when I was asked to add something interesting (“Could
PsychoPy present a movie stimulus?”) I often couldn’t resist the temptation and it appeared as a feature.
Gradually other volunteer developers (mostly academics) got involved as well and, over the
years, the PsychoPy library and application became relatively bug-free and widely used.
In short, it was never my intention to write such a substantial package. It all just began with me
programming a few stimuli, in much the same way that you are learning the basics of presenting
some stimuli now. So just watch out in case your programming addiction snowballs like mine did!

Jonathan (Jon) Peirce received his degree in psychology from the University of St Andrews and his PhD
from Cambridge University. This was followed by a period in America at New York University. He
has been at the University of Nottingham since 2003. His research interests are in visual neuroscience.
Writing a Script in the Coder Editor 39

In this chapter you will learn about PsychoPy. The focus is on understanding the
logic and the commands, and therefore the programs are not trying to be elegant or
fast. The main criterion will be a consistent way of doing things (across all the illu-
sions) that can be understood and adapted, so that you will know what your program
does and you can change it to experiment with new images. Full details about all the
features of PsychoPy are online at www.psychopy.org, and in particular the so-called
application programming interface (API) is at www.psychopy.org/api/api.html.
If your computer is online the API and its detailed documentation on the website
is the obvious tool for reference. In addition to the API the site includes much infor-
mation specific to creating experiments and collecting responses, this may not be rel-
evant except for vision scientists and advanced users, but www.psychopy.org is still the
key reference for anything to do with PsychoPy.

Writing a Script in the Coder Editor

PsychoPy has a Builder view and the Coder view. I will say nothing about the Builder
(except in Box 3.1), because we want to have fun programming, so remember to switch
view to Coder (from the View menu).

Box 3.1. Builder and Coder

PsychoPy is a package with a specific purpose. It was born to create experiments in the field of psy-
chology and neuroscience, and in particular what are known as studies in psychophysics. Therefore
the typical program is about an experiment in which certain images are shown and certain responses
collected from participants. Imagine for instance that you want to find out how much brightness you
need before a participant is able to read a word. The brightness of the word is changed up and down
and the observer has to try and read it. This is the simplest example I could think of, and count-
less other experiments can investigate how people perceive shape, colour, motion, faces and so on.
Because of this purpose, PsychoPy includes a series of tools to create experiments. In particular
the Builder allow the rapid development of experiments. Instead of asking the programmer to write
commands, it provides two panels in which there is a visual interface with boxes that represent what
in the Builder are called the routines (in one panel) and the flow of the experiment (in another panel).
From within PsychoPy there are two main views, when we are in Builder view we see routines
and flow, when we are in Coder view instead we see the text editor in which we write a series of
commands in Python (this is shown in Fig. 2.1).
I will not describe the details of how the Builder works because in this book we are not interested
in creating experiments. The key idea is that the flow represents the flowchart of the experiment.
This flowchart is created by selecting options from menus, so the Builder allows a user to create a
program without actually doing the programming.
In the background the Builder is doing all the work of translating the options selected by the user
in actual Python commands. The result is that the Builder does create a program that is a Python
script and can be seen and modified by the user.
The bottom line is that there are two good reasons why we are not going to use the Builder. The
first as I said is that we are not putting together experiments. The second is that I have argued that
programming in the sense of writing commands in a programming language is fun. We are learn-
ing bit by bit how to speak Python, and that means that we will be able to communicate much more
effectively with the computer using this language.
If you decide that you do want to know more about the Builder, the logical place to go is the main
PsychoPy website and in particular the following page:
http://www.psychopy.org/builder/builder.html
40 Chapter 3 · PsychoPy Is Fun

Languages like Python have many features that are built-in, and many others that
are available as additional packages. In Python these packages are added by import-
ing modules. Therefore, the first thing that we are going to do is to write a few lines
in the Editor about what modules we want to import. In Python this is simple and
transparent.

import math, numpy, random #to have handy system and math functions
from psychopy import core, event, visual, gui #these are the PsychoPy modules

The first three are modules that allow the program to work with numerical opera-
tions and maths. The second line is a bit different as it says that from PsychoPy we
want a set of specific modules to deal in particular with core commands (such as quit-
ting the program), events (such as pressing keys on a keyboard), visual stimuli (draw-
ing) and the graphical user interface (gui).
These lines and all the following lines can be typed into the main PsychoPy Editor
window. Just click on the first of the icons (the blank rectangle) to open a new docu-
ment. The first two lines in the Editor are shown in Fig. 3.1. A full listing of the programs
in this book is available online (www.programmingvisualillusionsforeveryone.online).

Figure 3.1. This is the PsychoPy application showing the Editor window and the first two lines of a pro-
gram. Special words are in blue and comments are in green. The Editor provides this colour coding for
us automatically
Creating a Python Object (a Window) 41

You can either type the commands or download the script, but I recommend that you
try typing all the commands.
The name module may sound strange. In reality a module is a Python file that
has definitions of variables, functions, and classes, and can be used as a library that
holds useful information. In other words if you were to open up some of PsychoPy
modules, or any other module, you could read all the details of its functions. It is all
in the Python language and in plain text (although the scripts may be long and com-
plex). You will never need to do that, but it’s nice to remember that there is nothing
mysterious about modules, they are just places where lots of stuff has already been
programmed for us to use.
Note the useful colours, within the Editor window PsychoPy will colour as blue
any special keyword (such as import) and as green any comment. As we saw in Chap-
ter 2, comments are whatever you type after a # sign, and they are just there for in-
formation. They play no part in the actual program and if you were to remove all
of them it would make no difference. But do not underestimate the importance of
comments to humans! They are there to make clear what is happening and for fu-
ture reference.
I have added comments for the two lines that you have seen already, saying that
here we are importing the necessary modules. I will also add a comment before every
new function later. However I will not add comments for most of the commands as
the script may end up being too wordy and the explanation is hopefully clear from the
text in the book. Feel free to add your own comments as you type.
Unlike the commands written after the prompt >>>, we are now writing a whole
script in the Editor window, and therefore nothing will happen until we run our script.
That will be in the next chapter, where we have an illusion to look at.
Although it may feel a bit different to write a whole script in the Editor window
compared to issue commands directly in the Shell window, the difference is mostly
superficial. All the lines in our script will eventually be read and executed by Python
one after the other, exactly the same way that they would be executed if we were to
type them all in the shell. The main difference therefore is that we like to put a whole
script together and save it as a file somewhere (to be open up again when we need it).
This long script is what we call a program.

Creating a Python Object (a Window)

Next we specify the window that we want to use. Python supports an object-oriented
style of programming and most stimuli in PsychoPy are Python objects, with various
associated functions and information (see Box 3.2). So what is an object within a pro-
gram? It can be many things. An object is a specific implementation of a type of ob-
jects, which in Python is called a class (think of Sheba, asleep on your sofa, as an im-
plementation of the class cat).
Our window will be an object, so we can learn more about objects by considering
this specific example. We create an object of type window by giving it a name.

myWin = visual.Window(color = 'white', units = 'pix', size = [1000, 1000],


allowGUI = False, fullscr = False) #creates a window
42 Chapter 3 · PsychoPy Is Fun

Box 3.2. Object-Oriented Programming (OOP)

In this book we do not learn object-oriented programming, but we make use of objects. Therefore
in this box I mention a few more things about this type of programming. Traditionally programming
has been thought as a logical way to solve a problem, and therefore a series of instructions, which
perform actions on a computer. In the early seventies the idea was proposed that it is better to think
in terms of data rather than actions. The grandfather of OOP was called Smalltalk, developed for
educational use at Xerox Palo Alto Research Centre and released in 1972.
What objects are needed for a program? Maybe an object could be a person (if I want to create
a contact list) or a button on the screen (to interact with the user) and so on. Each type of object,
also referred to as a class, has its own specific variables and functions, called methods. One ad-
vantage of OOP is that for a class, like cats, we can have subclasses, like for examples male cats
and female cats. They will share most features and have some that are additional. Once there is
a class or a subclass many individual exemplars can be created. These are implementations of a
class, for instance Sheba and Simba could be implementations of the class cat. Or Sheba could be
an implementation of a female cat, which makes her automatically a cat.
Modern languages that allow object-oriented programming include Python, Java™ and C++.
It is interesting to note that not everybody agrees that OOP is always the best way to program.

It is worth spending some time on this, as it is the first PsychoPy object that we
have seen. We start with the name myWin. This is a label and you can select the one
that makes more sense to use, so for instance we could have used myFirstWindow. This
is like choosing a name for your cat, you can call her whatever you like. From now on,
however, for consistency, our window will always be called myWin.
After the name and an equal sign there are two words connected by a full stop:
visual.Window. You will see many of these. It says that we are creating an object based
on the class window, which is inside the module visual. We can use the module visu-
al because we have imported it at the beginning of the script (from psychopy import core,
event, visual, gui). From this point onwards an object called myWin exists and it can be
used. At the time of creation we specify a few things with parameters. Parameters are
inside brackets and separated by commas.
The parameters for our window specify: colour of the background (color = 'white'),
the units of measurement (pixels, units = 'pix'), the window size (1 000 × 1 000 pixels,
size = [1000, 1000]), the fact that it is not a normal window with buttons (therefore no
graphical user interface, allowGUI = False), and the automatic taking up of the full screen
is switched off (fullscr = False).
The class window has many parameters, and we have only set a few of them. This
is fine because what we do not set is left to its default value, which in most cases is
OK. This will also happen for other objects that we will see later. Some arguments are
strings, some are numbers and some are lists, for instance size is a list with two values
(the x and the y, which are the width and the height of the window).
When we have many parameters the line may be long and it may not fit nicely on
your screen. Fortunately it is possible to put parameters on separate lines as long as
we have an open bracket. Remember formatting and indentations are important in
Python so we have to do this like this:

myWin = visual.Window(color = 'white', units = 'pix', size = [1000, 1000],


allowGUI = False, fullscr = False) #creates a window
Our Canvas 43

Actually you can break a line more than once, Python will not get confused because
this is all within brackets. In this case the indentation does not create blocks and is
there only to line things up nicely. Therefore we could also write the following.

myWin = visual.Window(color = 'white',


units = 'pix',
size = [1000, 1000],
allowGUI = False,
fullscr = False) #creates a window

The comment is completely ignored by Python so we could also do the following


(but I think it is confusing so we will never do that).

myWin = visual.Window(color = 'white', #creates a window


units = 'pix',
size = [1000, 1000],
allowGUI = False,
fullscr = False)

You may recall that range() also could take a variable number of parameters (be-
tween one and three: start, stop and step), although in that case they did not have
names within the brackets. This is a general feature of programming in Python, pa-
rameters can be named or not. Importantly, when some parameters are missing and
therefore the values are not specified a default value will be used. Again we had al-
ready seen that with range(). If there was no start the starting number was 0. In other
words range(0, 10) is the same as range(10).
Although PsychoPy was born in Britain it uses American spelling, so note color
and a few other words should be written wrongly (well, from a British point of view
that is).

Our Canvas

PsychoPy is very flexible on many things, for instance in the use of different units,
from pixels to centimetres to screen size. We will use pixels as it is more intuitive and
allows us to work and think in terms of integers.
What we have now, thanks to just three lines, is a canvas on which to work, like a
blank piece of paper. It is a square space (1 000 × 1 000 pixels) and the line that cre-
ates the object myWin will also make it visible on the computer screen. A window of
this size will probably not take over the whole screen, which is useful for now, as it
will give us more control in terms of switching between our program and other ap-
plications, and in particular it makes it easy to go back to the Editor. You can make
the window take over the whole screen easily at any point by changing the parameter
fullscr = False to fullscr = True.
The origin (0,0) by default will be in the middle of the screen. Positive values go to
the right and up, and negative to the left and down, again this should be intuitive and
is illustrated in Fig. 3.2. You can refer back to this figure to know the location of your
coordinates for all our programs.
44 Chapter 3 · PsychoPy Is Fun

Figure 3.2. The coordinates of our window. The centre is [0,0] and the values increase to the right and
upwards. If we move to the left or downwards the values decrease and therefore become negative

Creating a Python Object (a Clock)

Just like we have created a window and given it a name, we can create a clock and give
it a name.

myClock = core.Clock() #this creates and starts a clock which we can later read

As you see we just need to know that a clock can be created using the PsychoPy
core module. Similarly to the naming of the window we chose a logical name for our
clock. Having a clock means that at any point we can read the time. We can also reset
the start of the clock if we want to use it as a stopwatch and therefore use it to check
how much time has elapsed.
For the first time I will now describe a command given to an object, this is a function
specific to that object. For a clock the logical thing to ask is the time. You don’t need to
type this in the window because we will not use the clock in our first program.

timeNow = myClock.getTime() #this places the current time in a variable called timeNow
A Setup for All Our Illusions 45

What you can do with an object depends on the object. PsychoPy is clear and logi-
cal on how it is organised, so it is not surprising that for most objects created using the
visual module (like circles and rectangles, and unlike a clock) there will be a command
called .draw().

myCircle.draw()

Please note the brackets at the end of .getTime() and .draw(). Even though in these cases
there was no need for parameters it is important to note that the brackets are part of how
commands are written. We will see many more examples of this in the next chapters.

A Setup for All Our Illusions

At the end of our script we will close the window and quit the program.

myWin.close() #closes the window


core.quit() #quits

These lines are not very interesting, although they show other examples of com-
mands (the command .close() for a window and the command .quit()). We will always
include these two lines at the end of our script for nice housekeeping.
For now we have described a setup. We will use exactly this setup for all our pro-
grams. In the process of explaining the setup we have seen the object-oriented logic
that is key to how we will create our programs. Within the window we will present dif-
ferent shapes, and we will create these typically once, at the beginning of the script,
and then use them. Conveniently objects can be changed after they have been creat-
ed, so a single object can for instance be presented in a number of different colours.
Think of an object that you have created as something you can play with, if it is a
square you can move it, spin it, change colour and so on.
To repeat this concept, an object is not a unique entity that can only exist in one
place. This would force you to create ten objects if you wanted to show ten objects on
the screen. This is not necessary, you can have one object and show it in ten different
places at once, maybe with ten different colours. Of course we have not seen yet how
to do this, but we will soon.
We have seen in the case of the window that when we create an object we use pa-
rameters to set up attributes. Sometimes attributes will be text, other times they will
be numbers, or lists.
As we have seen in Chapter 2, in Python there are flexible ways to write numbers. If
it is a single integer and floating point we have introduced the term numerals. As we
work in pixels it makes sense to think of x- and y-coordinates as integers, but we can
also use floating-point values (with decimals). These are perfectly reasonable ways to
refer to coordinates, and they will be treated by the program with the best precision
available (given the underlying environment of your computer). Basically if you spec-
ify a line with a starting and ending point you then leave it to PsychoPy to draw it as
smoothly and precisely as possible. That’s great as we can just focus on what we care,
that is the drawing in terms of objects such as lines, circles and so on.
46 Chapter 3 · PsychoPy Is Fun

With respect to x-,y-coordinates, it makes sense to pair them in a list.

aPoint = [0, 0]

This specifies a position in the centre of our window. We have to remember


that we put the x first and the y second. For complicated reasons, mainly to do
with doing sophisticated maths, the numpy module uses its own numpy arrays in-
stead of simple lists. As you remember numpy is one of the modules we have im-
ported, but not yet used. PsychoPy will turn lists into numpy arrays for its own in-
ternal working. Fortunately we do not need to worry about this distinction. Al-
though it is useful to know, we will always specify x,y-coordinates as lists and not
as numpy arrays.
Often an object will need more than one location (two points for a line, four for a
rectangle). These are collections of pairs, so it makes sense to use a list of lists.

verticesOfSquare = [[2, 2], [-2, 2], [-2, -2], [2, -2]]

The equal sign means we have placed these values inside a variable called verticesOfSquare.
These coordinates specify a square with a side of 4 (from –2 to 2). We can practice what
we learned in Chapter 2 and read each of the pairs using indices.

verticesOfSquare[0]

This is the first pair, and it specifies the first vertex. If you were to print this vari-
able you would read [2, 2] and these are x,y-coordinates. Given that they are both posi-
tive this vertex is in the top right quadrant (refer back to Fig. 3.2 for the way the coor-
dinates are set up).
The second vertex is

verticesOfSquare[1]

This is [-2, 2] and therefore this vertex is in the top left quadrant. To practice what
we know about lists, we can see how to access just the x- or y-value of the first pair.

verticesOfSquare[0][0]
verticesOfSquare[0][1]

Each of these is a numeral, not a list. When you see something written like this you
need to think that you are using two indices because you have a list of lists, so the first
example verticesOfSquare[0][0] refers to the first element of the first element.
Sometimes in programming we make compromises, and as one of the Python
principle says simple is better than complex. In the case of x- and y-coordinates
we could have created objects with explicit x- and y-components. Instead we have
chosen to use a list with two elements, and the first (position 0) will always be the
x-value and the second (position 1) the y. We will always specify x,y-coordinates
this way.
Different Types of Functions 47

Different Types of Functions

At this point it is best to go over the different types of commands that we have seen.
In Chapter 2 we have learnt about some built-in commands like len(). This com-
mand is a function that takes a list as an argument (within the brackets) and com-
putes the length.

>>> aList = [2, 4, 8]


>>> print(len(aList))
3

Now we have introduced modules, and some functions come from these modules.
Therefore to use these functions we need the name of the module followed by a full
stop. Here is an example we have seen from the core module.

core.quit()

We have also introduced objects, which are instances of a class. Each class, and
therefore each object, has its own functions. We have seen a few examples like .draw()
and .getTime(). Because they are specific to that object they are attached to the name of
the object with a full stop.

myCircle.draw()
timeNow = myClock.getTime()

It is important to understand the differences and also the similarities. All func-
tions may take parameters, and which parameters can be used is something that
one has to learn. Fortunately there is no need to spend a lot of time trying to mem-
orise how a function works because it is easy to look up the information when it is
needed. As we have seen both Python and PsychoPy are beautifully documented
online.
Functions may or may not return something. We have seen that len() returns a
number (the length of a list) and that .getTime() returns the time. Again, when in doubt
we can always look up the information about what is returned by a function. We have
also seen how to store something returned by a function in a variable.
When starting to program it may seem strange that we can use functions that are
specific to an object that we have created ourselves. So for example myClock is a name we
have chosen. Something with that name did not exist before the line in which we have
created this particular instance of the class Clock(). I am showing that line below again.

myClock = core.Clock() #this creates and starts a clock which we can later read

This is the key idea of object-oriented programming. We create objects that have
properties and functions associated with them. Then we use these objects when we
need them, by calling their functions and also accessing variables that are stored in-
side them.
48 Chapter 3 · PsychoPy Is Fun

I think you will be surprised by how short the programs that we are going to write
are. In most cases they would fit on one page. How is that possible? The reason is that
we are programming at a high level. That does not mean we write sophisticated pro-
grams, it means we write commands that are very powerful, that are logical and are
not tied to the specific hardware of the computer. In other words we take advantage of
work that has been done before. Python and PsychoPy can be used to combine a lot of
operations in simple high-level commands (and objects).

You might also like