Wilbert's website at SocSci

has a subtitle

psychopy/01python.html 2017-04-06

Donald E. Knuth: The most important thing in the programming language is the name.

Programming with PsychoPy, 1 Python

We have started the PsychoPy coder environment. Let's use it. Type the examples in the coder window. Save the files somewhere on your personal network drive. Use a different folder for each lesson.

Python is a general purpose programming language. It is suitable for a large number of tasks. Programs such as the Calibre e-book library are written in Python. Python is used a lot in the academia for computational work, as an alternative for Matlab and Matlab-like environments such as Octave and Scilab. We will be using Python for stimulus presentation and response collection and it is also very suitable for data analysis, as an alternative for Spss and Gnu-R.

Since PsychoPy is built on Python, we will have to understand a bit of Python before we use PsychoPy.

Go to the coder view and type the following. Please do not copy-paste, I want you to understand every character that you type):

print("Hello World")

Press the running man button. The text Hello World appears in the Output window. Congratulations. That was a working Python program.

What did we do?

The word print refers to a function. Putting a function name in your program followed by opening and closing parentheses is called calling a function. The function is executed. This may have some side effects, ranging from showing a text in the output window (that is what the print function does) to launching a rocket. Between the parentheses there are zero or more variables. In the example above, there is one variable of the type string. Strings are used to contain texts.


Lines started with a hash (#) are comments. You can also use a hash in the middle of a line. Whatever is after the hash will be ignored. Use comments to make notes to yourself. Please use comments a lot, you can hardly overdo it.
# the following line will show a text in the Output window
print("Hello World") # this is the line that will show a text
# this line will do nothing, it is a comment

Variable types

In addition to strings, there are variables that contain numbers. There is a difference between whole numbers and real numbers. There is a special type called boolean, which can only be True or False.

print("Hello World") # string
print(123)           # integer number
print(3.14)          # real number
print(False)         # boolean False, the opposite of True

Variable names

Variables can have names. These are place holders to store values. You can choose almost any name for a variable, although it is common to use names that clearly indicate what they actually do. And it makes the code more readable.
i = 17
peter_smith_age = 17.5
password = "None Shall Pass"
answer = True

In the first line of code, the variable with the name i becomes the integer number 17. In the second line peterSmith becomes the real number 17.5. In the third line the variable s becomes the text string None shall pass. We have to enclose text strings in quotation marks. In the last line the variable named answer receives the value True.


integer number
positive or negative whole number
real number
number with decimals
text string
character string variables (word or sentences)
boolean value
boolean variable (True or False)

Note that we always write variable with a lowercase first letter. This is not something that PsychoPy demands. It is just our convention.


Operators are things that operate on variables. Often what they do is obvious:

a = 123
b = 456
c = a+b
print(c) # output 579
s = "Hello"
t = " "
u = "World"
w = s + t + u 
print(w) # output: "Hello World"

Which will print 579 and Hello World.

Most often you will be able to gues what an operator does. The operators +, -, *, / have their usual meaning.


There are literally millions of functions (such as the print function) that you can use in Python. If all these functions were available from the start, there would not be enough words for all these functions. Therefore the the functions are bundled in modules. To import the math module and call the cosine function at point x=0.0 you can for instance do this:
import math
y = math.cos(0.0)

In the next chapter we will import the PsychoPy module and make our first stimulus.

Assignment 1: Python

  1. Can you run all the example above?
  2. What happens when you use multiple arguments (separated by comma's) for the print function?
  3. You can use the + operator for adding number to numbers, and for adding string to strings. What happens when you add an integer to a real number? Is there a difference between 1+1 and 1.0+1? Can you add strings and numbers? Try to understand Python's reaction.
  4. What characters are valid in variable names? Can we use capitals? Is there a difference between the variables length and Length.

    Continue with the next lesson