Overview
Teaching: 10 min
Exercises: 10 minQuestions
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.
=
symbol assigns the value on the right to the name on the left.roi_vol
and a name in quotation marks to a variable roi_label
.roi_vol = 130
roi_label = 'hippocampus'
__roi_vol
have a special meaning
so we won’t do that until we understand the convention.roi_vol
130
roi_label
'hippocampus'
brain_volume
----------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-12-dec7c88e2504> in <module>()
----> 1 brain_volume
NameError: name 'brain_volume' is not defined
# This sentence isn't executed by Python.
# print(brain_volume)
brain_volume = 1351 # Neither is this - anything after '#' is ignored.
roi_vol
a few lines ago.corrected_roi_vol = roi_vol + 3
# Volume with a correction for a systematic bias:
corrected_roi_vol
133
roi_label = 'hippocampus'
roi_label[0]
h
[start:stop]
.roi_label[0:5]
hippo
roi_label[0:5:2]
hpo
len
to find the length of a string.len(roi_label)
11
Name
and name
are different variables.print
to display values.print
that prints things as text.print(roi_label)
hippocampus
print('The', roi_label, 'is', roi_vol, 'cubic mm')
The hippocampus is 130 cubic mm
print
automatically puts a single space between items to separate them.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
We can list the variables we have defined, along with some details about them,
by executing the IPython magic %whos
:
%whos
These variables do not exist on the hard disk of the computer. They will not
continue to exist if we stop our current IPython kernel (by executing
exit
). Ideally, in any time in our analysis we should be able to remove
these variables and regenerate them again by running a script file
containing all of the commands we used to get to our current state.
We will learn the tools that IPython provides to do this.
For now we will simply learn how to delete all of the variables in the
current environment. This is done using the %reset
IPython magic and typing
y
and enter to confirm:
%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 ofroi_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
, orminutes
? Why? Hint: think about which code you would rather inherit from someone who is leaving the lab:
ts = m * 60 + s
tot_sec = min * 60 + sec
total_seconds = minutes * 60 + seconds
Solution
minutes
is better becausemin
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
Use variables to store values.
Use
Variables must be created before they are used.
Variables can be used in calculations.
Use an index to get a single character from a string.
Use a slice to get a substring.
Use the built-in function
len
to find the length of a string.Python is case-sensitive.
Use meaningful variable names.
Variables in the current environment can be displayed or deleted using the
%whos
and%reset
IPython magics.