<jupyter_start><jupyter_text>Introduction to programming for Geoscientists (through Python)  Lecture 2: Solutions to exercises <NAME> (<EMAIL>) http://www.imperial.ac.uk/people/g.gorman  Exercise 2.1: Values of boolean expressions<jupyter_code>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<jupyter_output><empty_output><jupyter_text>Exercise 2.2: Make a Fahrenheit-Celsius conversion tableWrite 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.<jupyter_code># 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.<jupyter_output>0 -17.77777777777778
10 -12.222222222222223
20 -6.666666666666667
30 -1.1111111111111112
40 4.444444444444445
50 10.0
60 15.555555555555557
70 21.11111111111111
80 26.666666666666668
90 32.22222222222222
100 37.77777777777778<jupyter_text>Exercise 2.3: Write an approximate Fahrenheit-Celsius conversion tableMany 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}$.<jupyter_code># 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.<jupyter_output>0 -17.77777777777778 -15.0
10 -12.222222222222223 -10.0
20 -6.666666666666667 -5.0
30 -1.1111111111111112 0.0
40 4.444444444444445 5.0
50 10.0 10.0
60 15.555555555555557 15.0
70 21.11111111111111 20.0
80 26.666666666666668 25.0
90 32.22222222222222 30.0
100 37.77777777777778 35.0<jupyter_text>Exercise 2.4: Store odd numbers in a listModify 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.<jupyter_code># 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)<jupyter_output>The odd numbers list =  [1, 3, 5, 7, 9, 11, 13, 15]<jupyter_text>Exercise 2.5: Repeat the previous exercise using: a for loop, list comprehension and the range function<jupyter_code># 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)<jupyter_output>The odd numbers list =  [1, 3, 5, 7, 9, 11, 13, 15]
The odd numbers list =  [1, 3, 5, 7, 9, 11, 13, 15]
{'fraction_non_alphanumeric': 0.08275733233700729, 'fraction_numerical': 0.10424061274051934, 'mean_word_length': 3.4065843621399177, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 20, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '530961', 'license': 'CC-BY-3.0', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1645, 'n_words': 793, 'path': 'notebook/Lecture-2-Introduction-to-programming-for-geoscientists-Solutions.ipynb', 'repo_name': 'navjotk/test', 'size': '5379'}