Module 03
So you want to learn Python basics
This module gives you enough Python to read beginner notebooks, understand simple scripts, and write small reusable bits of code without feeling like every bracket is a personal attack.
What you should be able to do after this module
- Create variables and store strings, numbers, and boolean values.
- Use lists and dictionaries to organise simple experimental data.
- Write
ifstatements to make small decisions in code. - Loop over values and collect results.
- Define a function so repeated analysis steps do not need to be rewritten.
How to use this module
This lesson works best if you type the examples yourself. Reading the code helps, but typing and running it is where the ideas start to stick.
It is normal if the early sections feel slow. That is not wasted time. It is where the later notebook and package modules become easier.
Variables and basic data types
A variable is a name that points to a value. In a research setting, that value might be a filename, a threshold, a list of measurements, or a label for an experimental condition.
sample_name = "control_01"
cell_count = 42
mean_signal = 137.5
is_treated = False
Here you have a string, an integer, a floating-point number, and a boolean.
Why these types matter
- Strings are useful for filenames, labels, and conditions.
- Integers are useful for counts like cells or time points.
- Floats are common for intensities, areas, and averages.
- Booleans are useful for true/false decisions such as treated or not treated.
Try printing them
print(sample_name)
print(cell_count)
print(mean_signal)
print(is_treated)
Lists and dictionaries
Lists are useful when order matters. Dictionaries are useful when values have labels.
areas = [3.2, 3.5, 4.1, 4.4]
cell_summary = {
"cell_id": "c1",
"condition": "treated",
"area_um2": 4.1,
"mean_intensity": 151,
}
A list is a good fit when you mainly care about sequence. A dictionary is a good fit when you want labelled values.
Accessing values
print(areas[0])
print(areas[2])
print(cell_summary["cell_id"])
print(cell_summary["mean_intensity"])
Conditionals
Conditionals help your code respond to simple questions, such as whether a value is above a threshold.
mean_intensity = 151
if mean_intensity > 140:
print("bright cell")
else:
print("dim cell")
The same pattern later appears in quality-control checks, thresholding rules, and simple analysis decisions.
Another example
area_um2 = 4.3
if area_um2 > 4.0:
print("large cell")
else:
print("small cell")
Loops
Loops let you repeat the same action for each measurement or file.
areas = [3.2, 3.5, 4.1, 4.4]
for area in areas:
print(area)
This pattern becomes useful once you start working through many cells, image files, or conditions.
A simple mental model is: “for each value in this collection, do the following.” That sentence is often enough to decode a loop.
Loop with a running calculation
areas = [3.2, 3.5, 4.1, 4.4]
total_area = 0
for area in areas:
total_area = total_area + area
print(total_area)
Defining functions
A function packages a repeatable step behind a name. That makes code easier to read and reuse.
def classify_signal(value, threshold=140):
if value > threshold:
return "bright"
return "dim"
classify_signal(151)
Function anatomy
defbegins the function definition.valueis the input argument.threshold=140is a default value.returnsends the result back.
A second small function
def mean_of_two_values(a, b):
return (a + b) / 2
print(mean_of_two_values(10, 14))
Why functions matter for researchers
The moment you copy and paste the same logic three times, you are already at the point where a function can make your workflow safer and easier to edit.
Worked example: combine a function and a loop
intensities = [120, 151, 118, 164]
def classify_signal(value, threshold=140):
if value > threshold:
return "bright"
return "dim"
for value in intensities:
print(value, classify_signal(value))
This is an important milestone because many beginner analysis tasks are built from exactly these ingredients.
Worked example: build a small result list
intensities = [120, 151, 118, 164]
labels = []
def classify_signal(value, threshold=140):
if value > threshold:
return "bright"
return "dim"
for value in intensities:
labels.append(classify_signal(value))
print(labels)
This is a useful stepping stone toward later notebook work, where you often loop through measurements and collect outputs.
Mini exercise set
- Create a variable called
experiment_dayand assign it a short text value. - Create a list of four intensity measurements and print the second value.
- Write an
ifstatement that prints"large cell"when an area is above4.0. - Write a loop that prints each item in a list of sample names.
- Define a function called
double_valuethat returns twice its input.
Challenge exercise
Put several ideas together by writing a function that labels a list of
intensities as either "bright" or "dim".
intensities = [120, 151, 118, 164]
# Write a function here and use a loop to classify each value.
If this feels hard, solve it in two pieces: first write the function for one value, then add the loop afterwards.
Common beginner mistakes
- Forgetting quotation marks around text values.
- Mixing tabs and spaces when indenting after
iforfor. - Expecting a variable to exist before assigning it a value.
- Writing a function but forgetting to return the result.
Stop here for today if needed
A strong stopping point is when you can read a short piece of Python and explain what each line is doing, even if you still need help writing code from scratch.
Ready for notebooks
If these ideas feel mostly comfortable, you are in a good position to open a Jupyter notebook and understand what each code cell is doing.