=========================== Python: Numbers (Solutions) =========================== #. Solutions:: a = 12 b = 23 print a, b print type(a), type(b) # int, int c = 34L d = 45L print c, d print type(c), type(d) # long, long x = 21.0 y = 14.f print x, y print type(x), type(y) # float, float #. Solutions:: print a, b, c, d, x, y print a, ";", b, ";", c, ";", ... #. Solutions:: # simple operations: product = a * b # int * int print product print type(product) # int difference = c - d # long - log print difference print type(difference) # long # division and integer division between various numeric types: quotient = x / y # float / float print quotient print type(quotient) # float result = a // b # int // int print result print type(result) # int result = c // d # long // long print result print type(result) # long result = x // y # float // float print result print type(result) # float # more complex examples, where the result type # is the more complex type between the types # of the operands: result = a * c # int * long print result print type(result) # long result = b * y # int * float print result print type(result) # float result = x * d # float * long print result print type(result) # float # here the type is automatically defined # based on the magnitude of the result: result = 2**0 # int**int print result print type(result) # int result = 2**0 # int*int print result print type(result) # *** long!!! *** result = 2**1.2 # int*float print result print type(result) # float result = 2**-2 # int*int print result print type(result) # *** float!!! *** result = 4**0.5 # int*float print result print type(result) # float result = 2**0.5 # int*float print result print type(result) # float #. Solutions:: >>> print 10 / 12 0 >>> print 10 / 12.0 0.833333333333 >>> print 10 // 12 0 >>> print 10 // 12.0 0.0 As you see, integer division is consistent with types: when it is applied to floats, the result is also a float, but truncated to the integer ``0``. #. Solutions:: >>> 10 % 3 1 >>> 10 % 3.0 1.0 As you see, ``%`` returns the remainder of ``10 / 3``:: 10 = 3*3 + 1 # ^ # remainder Operand types don't influence the value of the result, only its type. #. Solution:: pi = 3.141592 r = 2.5 circumference = 2 * pi * r print circumference area = 2 * pi * r**2 print area area = 2 * pi * r * r print area volume = (4.0 / 3.0) * pi * r**3 print volume Be careful: ``4 / 3 != 4.0 / 3.0``. #. Solution:: a, b = 100, True a2 = a b2 = b b = a2 a = b2 print a, b or:: a, b = 100, True x = a a = b b = x print a, b #. Solution:: gene1_start, gene1_end = 10, 20 gene2_start, gene2_end = 30, 40 # handy pictogram: # # 5' 3' # ~~~~~xxxxxxxx~~~~~xxxxxxx~~~~~> # 10 20 30 40 # \______/ \_____/ # gene_1 gene_2 # two options condition_1 = (10 <= pos <= 20) condition_1 = (pos >= 10 and pos <= 20) condition_2 = (30 <= pos <= 40) condition_3 = (10 <= pos <= 40) # two options condition_4 = condition_3 and not (condition_1 or condition_2) condition_4 = (20 <= pos <= 40) condition_5 = pos < 10 or pos > 40 # Be careful: # # pos < 10 and pos > 40 # # doesn't make sense: it's always False! condition_6 = condition_1 or condition_2 condition_7 = (0 <= pos <= 20) Test the code with multiple values of ``position``, in order to check that conditions have been correctly formulated: ``True`` when the position satisfies the condition, and ``False`` otherwise. #. Solution:: all_the_three = t and u and v t_or_u_but_not_both = (t or u) and not (t and u) # NOTE: here backslash at the end of lines are used to start a new line, you can ignore them. one_of_three_false = \ (t and u and not v) or \ (t and not u and v) or \ (not t and u and v) one_of_three_true = \ (t and not u and not v) or \ (not t and u and not v) or \ (not t and not u and v) Again, the code needs to be tested with different values of ``t``, ``u`` and ``v``. There are 8 possible combinations:: t, u, v = False, False, False t, u, v = False, False, True t, u, v = False, True, False t, u, v = False, True, True # ... #. Solution:: replication_time = 20 total_time= 480 final_bacteria = 2**(total_time//replication_time) print final_bacteria