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

Exercise 9.1

  1. Say we have three variables, x, y and z that store decimal numbers.

  2. Write some code to test and print whether y is between x and z.

Test your code on the following sets of numbers.

x = 12.3 y = 7.5 z = 3.6

and

x = -2.5 y = 7.5 z = 13.6

and

x = -2.5 y = 27.5 z = 13.6
x = 12.3 y = 7.5 z = 3.6 if (x < y and y < z) or (z < y and y < x): print( f'{y} is between {x} and {z}' ) else: print( f'{y} is not between {x} and {z}' )

Exercise 9.2

  1. Ask for input of a word.

  2. Test and print whether either of the letters 'a' or 'e' are in the word.

word = input('Enter a word: ') if 'a' in word or 'e' in word: print( f'"a" or "e" are in {word}' ) else: print( f'"a" or "e" are not in {word}' )

Exercise 9.3

  1. Ask for input of a DNA base (A, C, G, and T) in lower or upper case.

  2. First test that the input is a single letter rather than no input or more than one letter. Hint: Use len().

  3. If the first test is True, next test if the letter entered is a valid base and not some other letter. Print whether the base is valid or not.

  4. If the first test is False print that the input is not a valid base.

base = input('Enter a base: ') if len(base) == 1: if base.upper() in 'ACGT': print( 'base is valid' ) else: print( 'base is not valid' ) else: print( 'base is not valid' )

Exercise 9.4

  1. Ask for input of an integer number.

  2. If that number is odd or even (i.e., whether it is divisible by 2) even print True otherwise print False.

n = int( input('Enter an integer number: ') ) print( n % 2 == 0 )

Exercise 9.5

  1. Ask for input of a word.

  2. If the letter "r" is in the word print True otherwise print False

word = input('Enter a word: ') print( 'r' in word )