Wilbert's website at SocSci

> Software> PsychoPy Course> 1 Python

psychopy/01python.html 2018-04-26

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. It is also very suitable for data analysis, as an alternative for Spss and Gnu-R. The Python library for data analysis is called Pandas.

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. The text itself is between quotation marks


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
peterSmithAge = 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 peterSmithAge 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.


We have seen four data types:

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 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 examples 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