#!/usr/bin/env python # coding: utf-8 # # Introduction to programming for Geoscientists (through Python) # # Lecture 2: Solutions to exercises # ## Gerard J. Gorman (g.gorman@imperial.ac.uk) http://www.imperial.ac.uk/people/g.gorman # ## Exercise 2.1: Values of boolean expressions # In[1]: C = 41 # The value of C must be equal to 40 for this condition to be True. Otherwise, # this condition is False. Of course, 41 is not equal to 40, so this particular # condition is False. C == 40 # Both conditions (C != 40 *and* C < 41) must be True for the whole statement # to be True. Although the first condition is True, the second condition is # False since C is not less than 41, so the whole statement is False. C != 40 and C < 41 # Here, only one of the two conditions needs to be True in order for the whole # statement to be True. Since C != 40 is True, this particular statement is True. C != 40 or C < 41 # The *not* keyword just negates whatever condition follows it. In this case, # C == 40 is False, so 'not C == 40' will return the opposite (True). not C == 40 # C > 40 is True, so 'not C > 40' is False not C > 40 # For this condition to be True, C must either be less than 40 *or* equal to 40. # If neither of these are True, then the condition is False. C <= 41 # The *not* keyword will negate the result, so 'not False' is 'True'. not False # With the *and* logical operator, both conditions must be True for the whole # statement to be True. In this particular case, the statement is False. True and False # With the *or* logical operator, this whole statement is True since at least # one of the conditions is True. False or True # None of the conditions are True, so the whole statement is False. False or False or False # At least one of the conditions is False, so the whole statement is False. True and True and False # The integer value of False is 0, so this statement is True False == 0 # The integer value of True is 1, so this statement is False True == 0 # This statement is True (see above). True == 1 # ## Exercise 2.2: Make a Fahrenheit-Celsius conversion table # Write a program that prints out a table with Fahrenheit degrees 0, 10, 20, ..., 100 in the first column and the corresponding Celsius degrees in the second column. # In[2]: # Make a Fahrenheit-Celsius conversion table F = 0 # Initialise F dF = 10 # Increment for F within the loop while F <= 100: # Loop heading with condition C = (F - 32)*(5.0/9.0) # 1st statement inside loop print(F, C) # 2nd statement inside loop F = F + dF # Increment F for the next iteration of the loop. # ## Exercise 2.3: Write an approximate Fahrenheit-Celsius conversion table # Many people use an approximate formula for quickly converting Fahrenheit ($F$) to Celsius ($C$) degrees:

# $C \approx \hat{C} = \frac{F − 30}{2}$

# Modify the program from the previous exercise so that it prints three columns: $F$, $C$, and the approximate value $\hat{C}$. # In[3]: # Write an approximate Fahrenheit-Celsius conversion table F = 0 # Initialise F dF = 10 # Increment for F within the loop while F <= 100: # Loop heading with condition C = (F - 32)*(5.0/9.0) # 1st statement inside loop C_hat = (F - 30)/2.0 # Compute the approximate value of C. print(F, C, C_hat) # 2nd statement inside loop F = F + dF # Increment F for the next iteration of the loop. # ## Exercise 2.4: Store odd numbers in a list # Modify the program from the previous exercise to store the generated odd numbers in a list. Start with an empty list and use a while loop where you in each pass of the loop append a new element to the list. Finally, print the list elements to the screen. # In[4]: # Store odd numbers in a list n = 16 i = 1 odd_numbers = [] while i <= n: odd_numbers.append(i) # Append the odd number 'i' to the list called 'odd_numbers' i = i + 2 print("The odd numbers list = ", odd_numbers) # ## Exercise 2.5: Repeat the previous exercise using: a for loop, list comprehension and the range function # In[5]: # Generate odd numbers by a list comprehension n = 16 # Use list comprehension to generate the list of odd numbers. # Remember to choose the step size to be 2 here. odd_numbers = [i for i in range(1, n+1, 2)] print("The odd numbers list = ", odd_numbers) # You could also do (using the default 'start' and 'step' values in the range function) odd_numbers = [2*i + 1 for i in range(int((n + 1)/2))] print("The odd numbers list = ", odd_numbers)