Exercises about statements, assignments, lists and generators, control flow and packaging#
1. Statements#
Exercise: Print your favorite kind of enzyme. Hint: enzyme kind should be printed as a string.
Level: Easy.
# Your code here
Exercise: Sum 4 and 5 and divide the result by 2. Hint: don’t forget the parenthesis.
Level: Easy.
# Your code here
Exercise: Print your age as an integer.
Level: Easy.
# Your code here
Exercise: Print your age as a string.
Level: Medium.
# Your code here
Exercise: Calculate the square of 16, then divide the result by the square of 3. Only return 2 decimal points.
Level: Medium.
# Your code here
2. Assignments#
Exercise: Bind your favorite enzyme to a variable. Choose a descriptive name for your variable.
Level: Easy.
# Your code here
Exercise: Create a variable called total
, which is the result of the following calculation:
your_birth_year * your_birth_day / your_birth_month - your_current_age
Level: Easy.
# Your code here
Exercise: Given the following variables, create two lists containing all possible combinations (sum) of the allowed pairs. Hint: Remember that you cannot add a string
and an integer
. Don’t do it manually, try to use a loop.
Level: Hard.
string_a = 'a'
string_b = 'b'
integer_1 = 1
integer_2 = 2
string_4 = '4'
float_1 = 1.0
float_2_5 = 2.5
Expected results, order does not matter:
list_a = [‘ab’, ‘a4’, ‘ba’, ‘b4’, ‘4a’, ‘4b’]
list_b = [3, 2.0, 3.5, 3, 3.0, 4.5, 3.0, 2.0, 3.5, 3.5, 4.5, 3.5]
# Your code here
*Possible solution in Notebook Exercise 2.ipynb
3. Lists and generators#
multiple_5 = [5, 10, 15, 20]
Exercise: Given multiple_5
, create a list MANUALLY containing each element in multiple_5
summed by 2.
Level: Easy.
# Your code here
Exercise: Given multiple_5
, create a list USING A FOR LOOP containing each element in multiple_5
summed by 2.
Level: Easy.
# Your code here
Exercise: Given multiple_5
, create a list USING A LIST COMPREHENSION containing each element in multiple_5
summed by 2.
Level: Medium.
# Your code here
Exercise: Given multiple_5
, create a generator containing each element in multiple_5
summed by 2.
Level: Easy.
# Your code here
4. Compound statements#
Exercise: Translate the following flow chart to check why the unit for the hydrodynamics condition in packed tower experiment is not working.
Level: Easy.
# Your code here
Exercise: Calculate the 10th element F10 in the Fibonacci sequence, where F0 = 0, F1 = 1 and Fn = (Fn-1) + (Fn -2) for n > 1.
Level: Medium.
# Your code here
5. Packaging#
Exercise: Write four functions for:
Summing two numbers
Subtracting two numbers
Multiplying two numbers
Dividing two numbers
Level: Easy
def plus(a, b):
# Your code here
return
def minus(a, b):
# Your code here
return
def multiply(a, b):
# Your code here
return
def divide(a, b):
# Your code here
return
Example: We compute 1 + 3 - 2
minus(plus(1, 3), 2)
Exercise: Use the functions implemented above to compute the following:
(((2 + 7) * 5) / 3) - 4
6 / 3 * 6 - 1
3 + 4 + 7 - 2 * 7 / 2
Level: Medium
# Your code here