Open and Reproducible Neuroscience

Variables and Assignment

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How can I store data in programs?

Objectives
  • Write programs that assign scalar values to variables and perform calculations with those values.

  • Correctly trace value changes in programs that use scalar assignment.

Use variables to store values.

roi_vol = 130
roi_label = 'hippocampus'

Variables can be displayed by simply executing them as an expression

roi_vol
130
roi_label
'hippocampus'

Variables must be created before they are used.

brain_volume
----------------------------------------------------------
NameError                Traceback (most recent call last)
<ipython-input-12-dec7c88e2504> in <module>()
----> 1 brain_volume

NameError: name 'brain_volume' is not defined

Use comments to add documentation to programs.

# This sentence isn't executed by Python.
# print(brain_volume)
brain_volume = 1351   # Neither is this - anything after '#' is ignored.

Variables can be used in calculations.

corrected_roi_vol = roi_vol + 3
# Volume with a correction for a systematic bias: 
corrected_roi_vol
133

Use an index to get a single character from a string.

roi_label = 'hippocampus'
roi_label[0]
h

Use a slice to get a substring.

roi_label[0:5]
hippo

A slice with a different step

roi_label[0:5:2]
hpo

Use the built-in function len to find the length of a string.

len(roi_label)
11

Python is case-sensitive.

Use print to display values.

print(roi_label)
hippocampus
print('The', roi_label, 'is', roi_vol, 'cubic mm')
The hippocampus is 130 cubic mm

Use meaningful variable names.

flabadab = 42
ewr_422_yY = 'Ahmed'
print(ewr_422_yY, 'is', flabadab, 'years old')
flabadab = 700
ewr_422_yY = 'frontal cortex'
print('The',ewr_422_yY, 'is', flabadab, 'cubic mm')
Ahmed is 42 years old
The frontal cortex is 700 cubic mm

Variables exist in the IPython environment

We can list the variables we have defined, along with some details about them, by executing the IPython magic %whos:

%whos
%reset
Once deleted, variables cannot be recovered. Proceed (y/[n])? 
y

Swapping Values

Draw a table showing the values of the variables in this program after each statement is executed. In simple terms, what do the last three lines of this program do?

lowest = 1.0
highest = 3.0
volume = lowest
lowest = highest
highest = volume

Predicting Values

What is the final value of important_label in the program below? (Try to predict the value without running the program, then check your prediction.)

label = "hippocampus"
important_label = label
label = "visual"

Challenge

If you assign roi_vol = 130, what happens if you try to get the second digit of roi_vol?

Solution

Numbers are not stored in the written representation, so they can’t be treated like strings.

roi_vol = 130
print(roi_vol[1])
TypeError: 'int' object is not subscriptable

Choosing a Name

Which is a better variable name, m, min, or minutes? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:

  1. ts = m * 60 + s
  2. tot_sec = min * 60 + sec
  3. total_seconds = minutes * 60 + seconds

Solution

minutes is better because min might mean something like “minimum” (and actually does in Python, but we haven’t seen that yet).

Slicing

What does the following program print?

sequence_type = 'MP_RAGE'
print('sequence_type[1:3] is:', sequence_type[1:3])

Solution

sequence_type[1:3] is: P_

Key Points