Python Information and Tutorials Help Center
Python in general and in this course
Python is a widely used programming language with powerful functionality. To use it, you generally start with a base installation and then download and install the packages and libraries you need for your project. Thanks to many useful libraries and packages dealing with mathematics, statistics, and visualization, it is being used increasingly in the scientific community. Python is free to download and install (and in fact there is a very high change your computer already contains a Python installation).Python is available in many different distributions, so if you are already an experienced user, feel free to use whichever one suits your fancy; the packages we will be using in this course are numpy and matplotlib, which you can install by following the instructions on their websites. If you haven't used Python before, or simply want to start again from the beginning, continue reading!
The Anaconda Scientific Python Distribution and the Spyder IDE
For those of you who haven't programmed in Python before but are looking to start, we recommend the Python IDE (integrated development environment) called Spyder, which is included with the free Anaconda Scientific Python Distribution. Download and installation instructions are available here. What's nice about Anaconda/Spyder is that it comes with all the packages we'll be using pre-installed, so you can get going right away.When you launch Spyder, you will be greeted with a console and an editor, as well as some other useful windows. The console contains the interpreter and allows you to run commands one-at-a-time. The editor allows you to write larger sets of commands (such as scripts or modules) that you can save on your hard drive and run whenever you please. The most recent version of Spyder opens the Kernel window by default, so to get to the console, you will have to click on the tab that says "Python 1" (if you don't see a console window, click on the Consoles menu and select "Open a Python console").
If hard-drive space or Internet bandwidth is a concern, you might want to use Miniconda instead, which is 30-40 MB instead of 300MB.
Tutorials
A fantastic python tutorial is available in the official docs. We specifically recommend reading (and following along with) sections 3 - An Informal Introduction to Python, 4 - More Control Flow Tools, 5 - Data Structures, and 6 - Modules.Note that while in these tutorials they access the interpreter through the command line, everything is equally applicable when you use the interpreter included in Spyder (which is located in the console described above). One other nice thing about Spyder is that you don't have to run scripts through the command line. Instead, you can write a script in the editor, save it, and then to run it simply hit f5, or select "Run" from the Run menu -- this will execute all of the code in the script and all output will be written to the console (note that you have to have the Python console tab selected before you run a script).
The packages we'll be using along the way
Since we will also be using the numpy package to help us with some of the math and the matplotlib package to help us visualize our results, we also recommend going through this numpy tutorial, as well as this plotting tutorial.A couple of notes about Python, numpy, and matplotlib:
It is customary to import numpy as matplotlib in the following way at the beginning of each script (note: this is not how it's done in the numpy tutorial)import numpy as np
import matplotlib.pyplot as pltReferences to functions and objects within numpy and matplotlib's pyplot can then be made by prepending "np." or "plt." to your command. For example, you could make an array containing [1, -1, 2, -2, 3] and then plot it in the following way:x = np.array([1, -1, 2, -2, 3])
plt.plot(x)
plt.show()One other thing that messes up beginners trying to use Python to do mathematics is integer division. For example, when using the interpreter you might see the following result>>> 3/5
0This can be corrected by specifying that either 3 or 5 is a floating point number by appending a decimal point to it:>>> 3./5
0.6
>>> 3/5.
0.6However, for our purposes the best way of correcting this problem is to import "division" from the __future__ module:>>> from __future__ import division
>>> 3/5
0.6This way all division will be treated as floating point division, and you won't have to worry about anything accidentally getting rounded to the nearest integer. You won't have to worry about this if you're using Python 3, as Python 3isthe future we're importing division from.In summary, if you're new to scientific Python, we highly recommend inserting following three lines at the beginning of each of your scripts:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
Opening data files using the pickle module:
Oftentimes data is stored using the pickle format. Data can be stored in or loaded from this format using the pickle module in the following way:
import pickle
# create dictionary containing all your data
data = {'stim': np.array([1, 2, 3]), 'response': np.array([6, 2, 0])}
# save data in pickle format
with open('my_data.pickle', 'w') as f:
pickle.dump(data, f)
# open data from file
with open('my_data.pickle', 'rb') as f:
new_data_variable = pickle.load(f)
# now new_data_variable is equal to the dict {'stim': np.array([1, 2, 3]), 'response': np.array([6, 2, 0])}
Testing your knowledge
You can test your newfound knowledge of Python, numpy, and matplotlib by checking out our optional Python programming quiz (located in the Homework Quizzes section of the class site). Another great way to test your knowledge is simply by writing your own creative scripts!Remember
When writing code, Google is your friend. There are thousands of posts on various forums (like StackExchange) about how to do different things in Python, so chances are if you have a question someone has probably already asked it. And if by chance no one has asked it, don't be afraid to ask it yourself!For MATLAB users
For those of you have used MATLAB before but have decided to make the switch to Python, you'll find that while you can do all of the same things in both languages, there are a few important differences in syntax. For example, whereas in MATLAB indexing begins at 1, in Python indexing begins at 0, and whereas in MATLAB parentheses (...) are used for both indexing and function calls, in Python brackets [...] are used for indexing and parentheses (...) are used for function calls.This page contains several other examples of differences in the language, as well as a lot of helpful information about making the conversion.
Created Wed 29 Apr 2015 4:01 PM CEST
Last Modified Tue 5 May 2015 3:38 AM CEST
Last Modified Tue 5 May 2015 3:38 AM CEST