Stage 1: Notebooks, text and loops
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:
x = 10print(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 see | Why it happens | What to do |
|---|---|---|
| An old value shows up after you edited a cell | You changed the code but did not run it again. | Run the cell, and the cells below it. |
NameError: name 'df' is not defined | The 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 cell | Cells like count = count + 1 add again each run. | Run the cell that sets count = 0 first, or restart. |
| Results that do not match your code | You ran cells in a different order than they appear. | Restart and run all. |
| Colab says it ran out of memory, or the session crashed | The 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.
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 lettersText can be turned into a number with int(), which lets you do maths with it:
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:
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.
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
I appears in a protein sequence.Show a solution (try it first)
The output should look like this:
3seq = "MTAIIKEIVSRNKRR"
count = 0
for letter in seq:
if letter == "I":
count = count + 1
print(count)Pick a variant name apart
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:
Val
30
Aspname = "p.Val30Asp"
print(name[2:5])
print(name[5:7])
print(name[-3:])Break the kernel's memory on purpose
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)
# (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.
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)Second star
Sort each variant into a kind: synonymous if the old and new amino acid letters (variant[2:5] and variant[-3:]) are the same, nonsense if the new one is Ter, otherwise missense. Count each kind and type the three counts in that order, separated by commas.
Stuck? Nudge and solution
Keep three counters at 0. In the loop use if, elif and else to add 1 to the right one. Print them at the end.
variants = ["p.Gly12Gly", "p.Trp18Ter", "p.Leu25Pro", "p.Val30Asp",
"p.Gln36Ter", "p.Ile40Asn", "p.Ser45Thr", "p.Gly46Arg"]
syn = 0
non = 0
mis = 0
for variant in variants:
if variant[2:5] == variant[-3:]:
syn = syn + 1
elif variant[-3:] == "Ter":
non = non + 1
else:
mis = mis + 1
print(syn, non, mis)Special thanks to Nilay!