Atharva Bhagwat

Stage 1: Notebooks, text and loops

0Plan
Stage 1Core
★★

How notebooks work, why the kernel's memory matters, and how to work with text and repeat things.

A notebook mixes code, results and notes in one document. It is made of cells. A code cell holds Python and shows its result underneath. A text cell holds notes, headings and reminders for yourself.

The kernel has a memory

Behind every notebook runs a kernel: a Python program that does the work. When you run a cell, the kernel runs it and remembers the variables you created. The notebook shows the code; the kernel holds the values. They can disagree. Try it with two cells:

cell 1
x = 10
cell 2
print(x * 2)

Run both and you get 20. Now change cell 1 to x = 100 but do not run it, and run cell 2 again. Still 20: the page says 100, but the kernel still remembers 10.

When things go wrong with memory

What you seeWhy it happensWhat to do
An old value shows up after you edited a cellYou changed the code but did not run it again.Run the cell, and the cells below it.
NameError: name 'df' is not definedThe cell that creates it has not run in this session (new session, restart, or you skipped it).Run the cells above it, or Run all.
A number grows every time you run a cellCells like count = count + 1 add again each run.Run the cell that sets count = 0 first, or restart.
Results that do not match your codeYou ran cells in a different order than they appear.Restart and run all.
Colab says it ran out of memory, or the session crashedThe data was too big for the free machine, or the session was idle too long.Restart the session. Work on a smaller piece of the data first.

Text

Text in Python is called a string. A slice text[start:stop] takes letters from start up to, but not including, stop. Counting starts at 0, and negative numbers count from the end.

run this
name = "p.Trp18Ter"

print(name[2:5])      # Trp
print(name[5:7])      # 18
print(name[-3:])      # Ter
print(len(name))      # how many letters
print(name.upper())   # capital letters

Text can be turned into a number with int(), which lets you do maths with it:

run this
position = int(name[5:7])
print(position + 1)

Comparisons and decisions

A comparison asks a yes/no question. == means "is equal to" (one = stores a value, two ask a question). Others: != not equal, < > less or more, <= >= at most or at least. if runs some code only when the answer is yes:

run this
score = 1.4

if score < 0.5:
    print("low")
elif score < 1.0:
    print("in between")
else:
    print("like the reference")

Loops

A loop repeats code once for each item in a list. Read it aloud: for each variant in the list, do this. The indented lines are inside the loop.

run this
variants = ["p.Gly12Gly", "p.Trp18Ter", "p.Leu25Pro", "p.Val30Asp",
            "p.Gln36Ter", "p.Ile40Asn", "p.Ser45Thr", "p.Gly46Arg"]

for variant in variants:
    print(variant, len(variant))

Now practise in Colab

You have read the ideas. Next, try them. The notebook repeats the code above with a few blanks for you to fill in, then ends with small exercises. It is all about this stage. Run the cells in order.

Open the practice notebookYou need a free Google account. When it opens, choose File, then Save a copy in Drive, so your work is yours.

Exercises

These are also in the notebook. Try each one yourself before you open the solution.

Count a letter with a loop

Use a loop to count how many times the letter I appears in a protein sequence.
Show a solution (try it first)

The output should look like this:

expected output
3
one solution
seq = "MTAIIKEIVSRNKRR"
count = 0

for letter in seq:
    if letter == "I":
        count = count + 1
print(count)

Pick a variant name apart

A variant name such as p.Val30Asp holds three facts: the old amino acid, the position, and the new amino acid. Use slicing to print each one.
Show a solution (try it first)

The output should look like this:

expected output
Val
30
Asp
one solution
name = "p.Val30Asp"

print(name[2:5])
print(name[5:7])
print(name[-3:])

Break the kernel's memory on purpose

In a new cell write score = 5 and run it. In a second cell write print(score * 2) and run it. Now change the first cell to score = 50 but do not run it, and run the second cell again. Then choose Runtime, Restart session and run all. Write in a text cell what changed each time, and why.
Show a solution (try it first)
one solution
# (no code to submit: describe what you saw)

Check your understanding

First star

In the variants list above, every name has the position in letters 5 up to (not including) the last 3. Use a loop and int() to add all the positions together. What is the total?

Stuck? Nudge and solution

Start with total = 0. In the loop, add int(variant[5:-3]) to total. Print total after the loop.

one way to get it
variants = ["p.Gly12Gly", "p.Trp18Ter", "p.Leu25Pro", "p.Val30Asp",
            "p.Gln36Ter", "p.Ile40Asn", "p.Ser45Thr", "p.Gly46Arg"]

total = 0
for variant in variants:
    total = total + int(variant[5:-3])
print(total)

Special thanks to Nilay!