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

Exercise 20.1

Write a function called reverse_string() which prints the reverse of a string passed to it.

The call to the function is already included in the following code cell. You just need to write the function.

def reverse_string(string): print(string[::-1]) reverse_string( 'ebmoG fo seeznapmihc ehT' )
The chimpanzees of Gombe

Exercise 20.2

Write a function called average() that takes a list as an argument and prints out the average values in the list.

  • Hint: See Task 3.10 if you have forgotten how to calculate the average of a list of values.

def average(list): average = 0 sum = 0 for i in list: sum += i average = sum/len(list) print(average) arriving_penguins = [10, 156, 73, 376, 786, 432, 1035, 901, 1102, 2567, 1571, 916, 1560, 632, 943, 246, 654, 1456, 504, 632]
average( arriving_penguins )
827.6

Exercise 20.3

Write a function called multiply_two_numbers() that prints the product of two number passed to it.

def multiply_two_numbers(one, two): sum = one * two print(sum) n1 = float(input('Enter a number: ')) n2 = float(input('Enter another number: ')) multiply_two_numbers( n1, n2 )
Enter a number:
Enter another number:
10.0

Exercise 20.4

Write a function called count_bases() that takes two arguments. The first argument is a DNA sequence and the second is a string of the bases to be counted. If the second argument is missing then all bases should be counted.

The function should print the count of the specified bases.

def count_bases(one, two=''): count = 0 if two == '': count = len(one) else: count = one.count(two) print(count) dna_seq = 'ACGCGCAGATAGGACGCAGGGAGGGAGGAGGTTTTAGAAGAGGATAGACCCA' count_bases(dna_seq, 'A') count_bases(dna_seq, 'AT') count_bases(dna_seq)
17 2 52
count_bases(dna_seq, 'A') count_bases(dna_seq, 'AT') count_bases(dna_seq)