You are on page 1of 4

if statements – Part 1

Ex2_1 Convert the following sentences into logical expressions (conditions) using
Python.

i. a belongs to the interval [-5; 6) a <= -5 and a <6


ii. a is smaller than 3 or larger than 15 a < 3 or a > 15
iii. a is equal to b and c a == b and b ==c
iv. a is not equal to 3 a != 3 , other solution not (a=3)

Ex2_2 Find the logical result (True or False) from these conditions, given that
the variables are assigned to the following values:
Α = 10, Β = 2, C = -4, D = 9 and Ε = 1

i. (Α>Β) or (D=10) ~ True or False ~ True

ii. (D >= Β) and (Ε != C) ~ True and True ~ True

iii. not (Ε<=C) or (D<=C) ~ not False or False ~ True or False ~ True

iv. not ((Β<=C) and (D<2)) ~ not (False and False) ~ not False ~ True

Ex2_3 Find the output of the given code below?

X = 2
Y = X ** 2 - 1
Z = 2 * X + Y - 1
if X > Y:
Y = Z % X
Z = X ** 2
else:
X = Z % Y
Z = Y ** 2
print(X)
print(Y)
print(Z)

Χ : 2 0
Υ : 3
Z : 6 9
Ex2_4 Find the output of the given code below?

a = 3
b = 1
c = 5
if (a % 2 == 1) or (b >= 2):
c =c + 2
if (c < b):
a = a ** 3
else:
b = 4 * b
a = a % b
b = b % c
c = c % a
print(a)
print(b)
print(c)

(3 % 2 = 1) or (1 >= 2) 7 < 5
True Not True
a : 3 3
b : 1 4 4
c : 5 7 1

Ex2_5 Write an algorithm that reads the e-value of a BLAST similarity search
and prints whether the e-value is significant or not (significant E-value 1e-5 or
lower.

E = input ('Enter an evalue: ' )


if E < 1e-5:
print('significant')
else:
print('not signifcant')

Ex2_6 Write an algorithm that reads an integer x and calculates and prints
the value of the following mathematical function. Is there an “x-value” that
should not be accepted / processed (Tip: a division with zero cannot be defined)?
x = input ('Enter a value for x: ' )
if x != 1:
fx = (3*x)/(x-1)**2
print(fx)
else:
print('not defined')

Ex2_7 Write an algorithm that reads an integer x and calculates and prints
the value of the following mathematical function.

x = input ('Enter a value for x: ' )


x = float(x)
fx = 0
if x < 1:
fx = 5/((x-1)**2)
elif x > 1:
fx = 5/((x-1)**3)
else:
fx =2
print(fx)

Ex2_8 Write an algorithm that reads the gene copy number from deep
sequencing experiments and prints corresponding messages according to the
following: If the copy number is less than 2 the gene is expressed poorly, if the
copy number is more than 2 but less than 5, the gene is moderately expressed, if
the copy number is more than 5 but less than 15, the gene is actively expressed,
finally if the copy number is more than 15, the gene is expressed very actively.

exp = input ('Enter expression value: ' )


exp = int(exp)
if exp < 2:
conclusion = 'gene poorly expressed'
elif exp < 5:
conclusion = 'gene expressed moderately'
elif exp < 15:
conclusion = 'gene is actively expressed'
else:
conclusion = 'gene is extremely active'
print(conclusion)

Ex2_9 Write an algorithm that reads three numbers and prints out the
smallest of the three.

num1 = input ('Enter a number: ' )


num2 = input('Enter another number : ')
num3 = input('Enter another number : ')
min = num1
if num2 < min:
min = num2
if num3 < min:
min = num3
print(min)

Ex2_10 Write an algorithm that reads two RNA sequences and checks if the
first is a subsequence of the second. (NB:- you should check that the sequence is
RNA (string sequence of any A,U,G,C letter) and not DNA (string sequence of
any A,T,G,C letter) and only if it is RNA continue to check if the first is a
subsequence of the second).

RNA = input ('Enter a RNA sequence: ' )


RNA2 = input('Enter another RNA sequnece : ')
if 'T' in RNA or 't' in RNA:
print ('The first sequence you entered is not RNA')
elif 'T' in RNA2 or 't' in RNA2:
print ('The second sequence you entered is not RNA')
else:
if RNA in RNA2:
print ('the sequence is subsequence of the
2nd')
else:
print ('the 1st sequence is not subsequence of
the 2nd')

You might also like