Introductory exercices: Loops

Celsius to Kelvin

Print the conversion from Celsius degrees to Kelvin, from 0ºC to 40ºC, with a step of 5. That is, 0, 5, 10, 15...


In [2]:
for t in range(41):
    if t % 5 == 0:
        print(t+273.15)


273.15
278.15
283.15
288.15
293.15
298.15
303.15
308.15
313.15

In [4]:
for t in range(0,41,5):
    print(t+273.15)


273.15
278.15
283.15
288.15
293.15
298.15
303.15
308.15
313.15

Multiples

Print all the multiples of 3 from 0 to 25 that are not multiples of 5 or 7.


In [ ]:
for n in range(26):
    #Finish

Now, instead of printing, generate a list of all the multiples of 3 from 0 to 25 that are not multiples of 5 or 7.


In [ ]:

Messing with loops

What do you expect this loop to do? Check it.


In [2]:
for i in range(10):
        print('before:', i)
        if i==3 or i==7: i=i+2 #Tying to skip values 4 and 8...
        print('after: ',i)
        print('----------')


before: 0
after:  0
----------
before: 1
after:  1
----------
before: 2
after:  2
----------
before: 3
after:  5
----------
before: 4
after:  4
----------
before: 5
after:  5
----------
before: 6
after:  6
----------
before: 7
after:  9
----------
before: 8
after:  8
----------
before: 9
after:  9
----------

From the previous example you should deduce that it is better not to modify the loop variable. So now translate the previous incorrect loop into into a while loop that really skips i==4 and i==8.


In [ ]:

Queuing system

You have a list that should act as a kind of queueing system:

queue=['Mariona','Ramon', 'Joan', 'Quique', 'Laia']

You want to do something (say print it) with each element of the list, and then remove it form the list. (pop can be a useful method). Check that at the and, the list is empty.


In [12]:
queue=['Mariona','Ramon', 'Joan', 'Quique', 'Laia']

while queue:
    print("popping name : ",queue.pop(0), "remaining", queue)


popping name :  Mariona remaining ['Ramon', 'Joan', 'Quique', 'Laia']
popping name :  Ramon remaining ['Joan', 'Quique', 'Laia']
popping name :  Joan remaining ['Quique', 'Laia']
popping name :  Quique remaining ['Laia']
popping name :  Laia remaining []

In [6]:
queue.pop(0), queue


Out[6]:
('Mariona', ['Ramon', 'Joan'])

Factorial

Find the sum of the digits in 100! (answer is 648)


In [5]:
import math

In [7]:
math.factorial(100)


Out[7]:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

In [8]:
result = 1
for i in range(100):
    result = result*(i+1)
result


Out[8]:
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

In [12]:
result = 1
for i in range(1,101):
    result = result*i

result = str(result)
suma = 0
for caracter in result:
    suma = suma + int(caracter)
suma


Out[12]:
648

Dictionaries

Checking for keys

Soft software use keyword to define the type of calculations to be performed. As an example, here we will use the quantum chemistry software Gaussian. Imagine we have stored Gaussian keywords in a dictionary as in: keywords={'basis':'6-31+G', 'SCF':['XQC', 'Tight'], 'Opt':['TS', 'NoEigenTest']} Check that if there is a diffuse function in the basis set, SCF has 'tight' as one of its keywords.


In [ ]:
# Check different possibilities
keywords={'basis':'6-31+G', 'SCF':['XQC', 'Tight'], 'Opt':['TS', 'NoEigenTest']}
#keywords={'basis':'6-31G', 'SCF':['XQC', 'Tight'], 'Opt':['TS', 'NoEigenTest']}
#keywords={'basis':'6-31+G', 'SCF':['XQC',], 'Opt':['TS', 'NoEigenTest']}
#keywords={'basis':'6-31+G', 'Opt':['TS', 'NoEigenTest']}


if #Finish...
        print('When using diffuse functions, "Tight" should be used in the SCF!')

What happens if the 'SCF' keyword is not present as in here? keywords={'basis':'6-31+G', 'Opt':['TS, 'NoEigenTest']}


In [ ]:
#Finish

Common keys

Given two dictionaries, find the keys that are present in both dictionaries. (Hint: you can use sets)


In [ ]:
def common_keys(d1, d2):
    """
    Return the keys shared by dictionaries d1 and d2
    returns a set
    """
    #Finish


#Test it
d1 = makedict(red=1, green=2, blue=3)
d2 = makedict(purple=3, green=5, blue=6, yellow=1)
print(common_keys(d1, d2))

Genetic code (difficult!)

Given the genetic code dictionary, calculate how many codons code each aminoacid. which aminoacid is coded by more codons? The underscore means the STOP codon. (Answer: R and L, 6 times each)


In [ ]:
gencode = {
    'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
    'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
    'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
    'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
    'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
    'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
    'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
    'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
    'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
    'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
    'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
    'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
    'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
    'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
    'TAC':'Y', 'TAT':'Y', 'TAA':'_', 'TAG':'_',
    'TGC':'C', 'TGT':'C', 'TGA':'_', 'TGG':'W'}

Remember that you can iterate a dictionary keys with: for k in gencode: and its values with: for v in gencode.values(): Or access the values like this:

for k in d:
    v =d[k]

This exercice has many possible solutions.


In [ ]:

How many of Leu(L) codons differ in only one point mutations from a Ile(I) codon?


In [ ]:
#Finish

In [ ]: