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

Exercise 8.1

  1. Assign the decimals 17.5 and -34.6 to the variables x and y respectively.

  2. Test if x is greater than y and print whether it is or not.

  • Hint: Use if ... else.

x = 17.5 y = -34.6 if x > y: print('greater') else: print('not grater')
greater

Exercise 8.2

The wavelength of visible light ranges from 380 to 750 nanometers (nm). While the spectrum is continuous, it is often divided into 6 colors as shown in this table:

ColourWavelength (nm)
Violet380 to less than 450
Blue450 to less than 495
Green495 to less than 570
Yellow570 to less than 590
Orange590 to less than 620
Red620 to 750

Write some code that asks for input of a wavelength and prints its corresponding colour. Display an appropriate error message if the wavelength entered is outside of the visible spectrum.

  • Hint: Use if ... elif ... else with one elif for each colour.

wavelength = int(input('input of a wavelength'))

if wavelength < 380: print('too low')

elif wavelength < 450: print('violet')

elif wavelength < 495: print('blue')

elif wavelength < 570: print('green')

elif wavelength < 590: print('yellow')

elif wavelength < 620: print('orange')

elif wavelength < 750: print('red')

else: print('too high')

Exercise 8.3

  1. Ask the user to input a word.

  2. Test and print whether the letter 'e' is in the word or not.

word = input() if 'e' in word: print('yes') else: print('no')
yes

Exercise 8.4

Modify Exercise 8.3 so that if e is in the word the index of its first occurrence is printed, otherwise print that it is not in the word.

  • Hint: See Notebook 7 on how to find the index of a character.

word = input() if 'e' in word: print(f'yes on the idex {word.find("e")}') else: print('no')
yes on the idex 0

Exercise 8.5

  1. Ask for input of an integer.

  2. Test and print whether the integer is odd or even.

  • Hint: In Task 1.3 we asked what arithmetic operator could be used to test if an integer was odd or even?

x = int(input()) if (x % 2) > 0: print('odd') else: print('even')
odd

Exercise 8.6

  1. Access the second to the last characters in the following variable protein_seq and re-assign them to the same variable.

  2. Print protein_seq.

The answer is APYITTRRFFFCTRSVGILE
protein_seq = 'MAPYITTRRFFFCTRSVGILE' protein_seq = protein_seq[1:] print(protein_seq)
APYITTRRFFFCTRSVGILE

Exercise 8.7

During protein synthesis methionine is the first amino acid of a protein sequence. After translation proteins are modified, one of these modifications is removal of methionine from the start of the protein sequence. However, this is more likely if the second amino acid in the sequence is an alanine.

Write some code to test if the second amino acid in each of the following two protein sequences is alanine (A). If it is modify the protein sequence to remove methionine (M). If it isn't do not change the protein sequence.

Print the resulting protein sequences.

protein_seq1 = 'MAPYITTRRFFFCTRSVGILE' protein_seq2 = 'MLGMLLAI' if protein_seq1[1] == 'A': protein_seq1 = protein_seq1[1:] print(protein_seq1) if protein_seq2[1] == 'A': protein_seq2 = protein_seq1[1:] print(protein_seq2)
APYITTRRFFFCTRSVGILE MLGMLLAI