I came to Python later than I probably should have. My lab ran most things in MATLAB, and for the first couple of years I just went along with that. Then I started building experiments that needed real-time feedback loops and hardware interfacing, and MATLAB started feeling like a real headache. This series is basically the guide I wish I had when I was starting out.
Something I wish someone had said plainly: learning to program also means learning to look things up well. Good programmers Google constantly. More recently, they also ask LLMs. Syntax, error messages, library functions, weird installation problems, all of it. That is normal. The skill is not memorizing everything. The skill is knowing enough to ask a better question, check the answer, and keep moving.
Why Python?
Python gets recommended constantly, to the point where it almost sounds like hype. Some of the praise is earned. The scientific ecosystem is mature, the syntax is readable enough that old code is usually still understandable, and if you hit a problem, someone else has probably hit it too. For research work, that matters.
How Programming Helps with Research Workflows
In my research, I use Python for:
- Data analysis and visualization
- Statistical modeling
- Automating repetitive tasks
- Processing experimental data
- Running motor control experiments with psychopy
- Building models of motor control
Getting Started with Python
First you need Python on your computer. I recommend Anaconda because it comes with many scientific computing packages already set up.
Small update: I use uv for most of my own Python work now. It is fast and tidy, but I would still point most people to Anaconda when they are just getting started. The setup is more forgiving, and the graphical tools help when the terminal is still new.
What is Anaconda?
- Anaconda is a distribution of Python and many other packages
- You can use it to install Python and organize all the libraries you may use
- You will also use it to create virtual environments, which is a great way to manage your Python libraries
- There are other Python distributions. I recommend Anaconda here because I use it, I know its quirks, and the GUI is useful if the terminal is still new to you
How to Install Anaconda
- Visit Anaconda's download page
- Download the installer for your operating system
- Run the installer and follow the prompts
How to Use Your Terminal
If the terminal feels unfamiliar, start with Anaconda Navigator. It gives you a graphical way to manage environments and packages. Still, it is worth learning a few terminal commands. Otherwise you end up jumping between separate apps for environments, Git, file management, and scripts.
Opening the Terminal
macOS
On macOS, you can open the terminal by searching for it in Spotlight or by pressing Command + Space and typing "terminal".
Windows
On Windows, you can open the terminal by searching for it in the start menu or by pressing Windows + R and typing "cmd".
I would not spend much time in Command Prompt if you are learning research programming. Git Bash is the easier recommendation for most people. It gives you a bash-style terminal, and you will probably want Git installed anyway. You can download it here. To connect Git Bash with Conda, follow the steps in this post here.
WSL is also a good option, especially if you want something closer to a Linux setup on Windows. It is what I would choose if you are comfortable with a bit more setup, or if you expect to use command-line tools often. For a first pass, though, Git Bash is usually enough.
Linux
If you are on Linux, I am going to assume you know how to open the terminal. But do check that your Conda is installed properly (see below).
Checking That Conda is Installed
To check that Conda is installed, you can type conda --version in your terminal. If it is installed, you will see something like conda 4.14.0.
If you see something like conda: command not found, then check that you completed the installation steps above.
Basic Terminal Commands
If you want a broader command line tutorial, start with Codecademy's command line course. For now, try these few commands in your terminal:
$ ls
Documents Downloads Pictures
The command ls shows the contents of your current directory. Now let's create a new directory using the command mkdir:
$ mkdir test
$ ls
Documents Downloads Pictures test
Notice the new 'test' directory in the list. Let's move into it using the change directory command cd:
$ cd test
$ ls
The directory is empty. Let's create a new file using the touch command:
$ touch test.txt
$ ls
test.txt
Now let's remove the file using rm:
$ rm test.txt
$ ls
The directory is empty again. Here's a summary of what each command does:
ls- Lists files and directories in the current locationmkdir test- Creates a new directory named "test"cd test- Changes into the "test" directorytouch test.txt- Creates an empty file named "test.txt"rm test.txt- Removes the file named "test.txt"
Creating a Virtual Environment
A virtual environment keeps one project's Python packages separate from the rest of your computer. It helps you:
- Install specific versions of Python packages without affecting other projects
- Keep your project dependencies isolated and organized
- Easily share your project setup with others
- Avoid conflicts between different projects' requirements
Here's how to create and manage a virtual environment using Conda:
1. Create a New Environment
Note: the exact text and output may vary depending on your operating system and current directory. The examples here are simplified.
First, create a new environment with conda create -n learn-python python=3.11.
$ conda create -n learn-python python=3.11
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /Users/username/anaconda3/envs/learn-python
The following packages will be downloaded:
package | build
---------------------------|-----------------
python-3.11.0 | hb7a2778_3 21.2 MB
------------------------------------------------------------
Total: 21.2 MB
Proceed ([y]/n)? y
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
This command creates a new environment named "learn-Python" with Python 3.11 installed. The prompt will ask for confirmation before proceeding.
2. Activate the Environment
Now that you have created an environment, you can activate it by running the following command: conda activate learn-python
$ conda activate learn-python
(learn-python) $
The prompt changes to show the active environment name in parentheses. That means you are working inside the "learn-python" environment.
3. Verify Your Environment
You can verify that you are using the correct Python version by running the following command: python --version
(learn-python) $ python --version
Python 3.11.0
(learn-python) $ conda list
# packages in environment at /Users/username/anaconda3/envs/learn-python:
#
# Name Version Build Channel
python 3.11.0 hb7a2778_3
These commands confirm you're using the correct Python version and show the installed packages.
4. Deactivate When Done
When you're finished working, you can deactivate the environment to return to your base environment using the command conda deactivate.
(learn-python) $ conda deactivate
$
Additional Tips
- To see all your environments:
conda env list - To remove an environment:
conda env remove -n learn-python - To update all packages:
conda update --all
With the environment set up, you can move on to basic Python syntax.
Installing a text editor/IDE
Next, install a text editor or IDE. I usually recommend VS Code or PyCharm. If you already have one you like, use that. Early on, the goal is learning Python, not learning five editors at once.
You may have also heard of JupyterLab, which is common for notebooks. I do not use it much, mostly because VS Code and PyCharm/DataSpell fit my workflow better. If you want JupyterLab, install it in your environment with conda install -c conda-forge jupyterlab. The JupyterLab installation page has more detail. To launch it, type jupyter lab in your terminal while that environment is active. It should open in your browser.
Basic Python Syntax
Open a new file in your editor and save it as hello_world.py. Hello World is the usual first program, so we may as well start there. In your file, write:
print("Hello, world!")
Now, you can run the program by clicking the run button in your text editor/IDE. Or you can run it in your terminal by typing python hello_world.py and pressing enter. If you are in the environment you created earlier, you should see the following output:
Hello, world!
That is a Python program. Small, but it counts. Next are a few basic concepts you will use constantly.
Variables and Data Types
# variables can store different types of data
name = "Gregg" # string
age = 28 # integer
height = 1.75 # float
is_student = True # boolean
# print variables
print(f"My name is {name} and I am {age} years old")
Comments
# this is a single-line comment
"""
This is a multi-line comment
It can span multiple lines
"""
Python as a calculator
# addition and subtraction
x = 5 + 7 # x is now 12
y = 10 - 4 # y is now 6
z = x + y # z is now 18
# multiplication
a = 3 * 4 # a is now 12
# division
b = 15 / 3 # b is now 5.0 (result is always a float)
# floor division
c = 15 // 2 # c is now 7 (integer division)
# modulus (remainder)
d = 15 % 4 # d is now 3
# exponentiation
e = 2 ** 3 # e is now 8
One thing I'd say: don't worry about writing clean code when you're starting out. Half the code I wrote early on is embarrassing to look at now, but it worked, and I learned from it. The goal right now is just to get things running.
Next up: data structures and control flow, the building blocks you'll use in almost every script you ever write.