Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Task 2.1

  1. Ask for input of two integers: a numerator and a denominator.

  2. Calculate and print their integer division and remainder.

x = int( input('Enter first integer number: ') ) y = int( input('Enter second integer number: ') ) print( x // y ) print( x % y )

Task 2.2

Using just a single print() statement, print a table of two columns and three rows that resembles the following table of two crab species commonly found on the Scottish coast.

  • Hint: Use tab and newline escape characters.

Common nameSpecies name
Shore crabCarcinus maenas
Edible CrabCancer pagurus
print( 'Common name\tSpecies name\n-------------------------------\nShore crab\tCarcinus maenas\nEdible Crab\tCancer pagurus' )

Task 2.3

  1. Assign the string TTTTGTATCCTTATATCACAACTCGAAGATTCTTCTTCTGCA to the variable dna_seq.

  2. Print the number of bases in dna_seq, i.e., find its length.

dna_seq = 'TTTTGTATCCTTATATCACAACTCGAAGATTCTTCTTCTGCA' print( len(dna_seq) )

Task 2.4

  1. Create the empty string protein_seq.

  2. Proteins are chains of amino acids of which there are 20. Add "M" (which is methionine) to end of protein_seq and print it, then add "A" (which is alanine) to the end and print it.

protein_seq = '' protein_seq += 'M' print( protein_seq ) protein_seq += 'A' print( protein_seq )

Task 2.5

Video answer>

  1. Access the last five characters in the following variable protein_seq using negative indexing and re-assign them to the same variable.

  2. Print protein_seq.

protein_seq = 'MAPYITTRRFFFCTRSAGILE'
protein_seq = protein_seq[-5:] print( protein_seq )

Task 2.6

  1. Access the first to third characters in the variable dna_seq and assign them to the variable codon1.

  2. Next access the fourth to sixth characters and assign them to the variable codon2.

  3. Print codon1 and codon2.

dna_seq = 'ACAACTCGAAGATTCATGTATCCTTATATCA'
codon1 = dna_seq[0:3] codon2 = dna_seq[3:6] print(codon1, codon2)

Task 2.7

Video answer>

  1. Using a Python string method, convert the following DNA sequence into RNA. That is, replace thymine bases (T) with uracil bases (U).

    • Hint: We haven't covered this in the Notebooks so Google how to do it with "python replace character in string with another character".

  2. Remove the spaces between the codons.

    • Hint: To remove a space character, what do you need to replace it with?

codons = "ATG GCT GCG TCA ACT CGC AAA TGC GTA GGC CAA CCG"
codons = codons.replace('T', 'U').replace(' ', '') codons

Task 2.8

Convert the following DNA sequence to uppercase and print it. Use Google if you need to.

dna_seq = 'atagctacacatgctaggcatgttgctagctatctagc'
print( dna_seq.upper() )

Task 2.9

Video answer>

In the genetic code the codon "ATG" is the start codon which is used by the ribosome to initiate the synthesis of a protein.

The following DNA sequence contains the start codon "ATG".

  1. Find its index and print it.

  2. Using the index you just found, use string slicing to create a new sequence from the old sequence in which only the bases from "ATG" onwards occur.

  3. Print the new sequence

dna_seq = 'ACAACTCGAAGATTCATGTATCCTTATATCA'
idx = dna_seq.find('ATG') print( idx ) dna_seq = dna_seq[idx:] print(dna_seq)

Task 2.10

Using the in comparison operator, test if "ATG" is in the following DNA sequence. Print the outcome.

dna_seq = 'ACAACTCGAAGATTCATGTATCCTTATATCA'
if 'ATG' in dna_seq: print( 'ATG is in the DNA sequence') else: print( 'ATG is not in the DNA sequence')

Task 2.11

Using the find() string method, test if "ATG" is in the following DNA sequence. Print the outcome.

  • Hint: Remember that find() returns -1 if a substring is not in a string (see Notebook 7).

dna_seq = 'ACAACTCGAAGATTCATCTATCCTTATATCA'
idx = dna_seq.find('ATG') if idx == -1: print( 'ATG is not in the DNA sequence') else: print( 'ATG is in the DNA sequence')

Task 2.12

A word palindrome is a word which reads the same backward as forward, e.g., "level". In this task you will write code to test if a word is a palindrome.

  1. Reverse the word in the following code and assign it to the variable rev_word.

  2. Test whether word and rev_word are equal. If they are, use an f-string to print that the word is a palindrome.

word = 'jump'
rev_word = word[::-1] if word == rev_word: print( f'{word} is a palindrome' )

Task 2.13

Video answer>

A sentence palindrome reads the same backward as forward when capital letters are made lowercase and spaces and punctuation are removed.

Write some code to test whether the following sentence is a sentence palindrome.

  • Hint: Use what you learnt in Task 2.7.

sentence = "Was it a car or a cat I saw?"
new = sentence.lower().replace(' ', '').replace('?', '') rev_new = new[::-1] if rev_new == new: print( f'{sentence} is a palindrome' )

Task 2.14

Video answer>

Test if the following DNA sequence contains the start codon "ATG". If it doesn't, print that is doesn't. If it does, test and print whether it contains one of the stop codons "TAA", "TAG" or "TGA".

dna_seq = 'TTATGTATCCTTATATCACAACTCGAAGATTCTTCTTCTGCACGAGAAGCGTGGGAATCCTGGAATTA'
if 'ATG' in dna_seq: print( 'DNA sequence contains a start codon' ) if 'TAA' in dna_seq or 'TAG' in dna_seq or 'TGA' in dna_seq: print( 'DNA sequence contains a stop codon' ) else: print( 'DNA sequence does not contain a stop codon' ) else: print( 'DNA sequence does not contain a start codon' )

Task 2.15

Video answer>

Ask for input of a year and test and print whether it is a leap year or not.

  • A leap year is divisible by 4, except for years which are divisible by 100 but not divisible by 400. For example, the years 1600 and 2000 are leap years, but the years 1700, 1800, and 1900 are not.

  • Hint: You may wish to use the modulus operator % (see Notebook 3).

year = int( input('Enter a year: ') ) # Method 1 if year % 4 == 0: if year % 100 == 0 and year % 400 != 0: print('Not a leap year') else: print('A leap year') else: print('Not a leap year') # Method 2 if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0): print('A leap year') else: print('Not a leap year')