{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to programming for Geoscientists (through Python)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Lecture 2: Solutions to exercises\n",
"## Gerard J. Gorman (g.gorman@imperial.ac.uk) http://www.imperial.ac.uk/people/g.gorman"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2.1: Values of boolean expressions"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"C = 41\n",
"\n",
"# The value of C must be equal to 40 for this condition to be True. Otherwise,\n",
"# this condition is False. Of course, 41 is not equal to 40, so this particular\n",
"# condition is False.\n",
"C == 40\n",
"\n",
"# Both conditions (C != 40 *and* C < 41) must be True for the whole statement\n",
"# to be True. Although the first condition is True, the second condition is\n",
"# False since C is not less than 41, so the whole statement is False.\n",
"C != 40 and C < 41\n",
"\n",
"# Here, only one of the two conditions needs to be True in order for the whole\n",
"# statement to be True. Since C != 40 is True, this particular statement is True.\n",
"C != 40 or C < 41\n",
"\n",
"# The *not* keyword just negates whatever condition follows it. In this case,\n",
"# C == 40 is False, so 'not C == 40' will return the opposite (True).\n",
"not C == 40\n",
"\n",
"# C > 40 is True, so 'not C > 40' is False\n",
"not C > 40\n",
"\n",
"# For this condition to be True, C must either be less than 40 *or* equal to 40.\n",
"# If neither of these are True, then the condition is False.\n",
"C <= 41\n",
"\n",
"# The *not* keyword will negate the result, so 'not False' is 'True'.\n",
"not False\n",
"\n",
"# With the *and* logical operator, both conditions must be True for the whole \n",
"# statement to be True. In this particular case, the statement is False.\n",
"True and False\n",
"\n",
"# With the *or* logical operator, this whole statement is True since at least\n",
"# one of the conditions is True.\n",
"False or True\n",
"\n",
"# None of the conditions are True, so the whole statement is False.\n",
"False or False or False\n",
"\n",
"# At least one of the conditions is False, so the whole statement is False.\n",
"True and True and False\n",
"\n",
"# The integer value of False is 0, so this statement is True\n",
"False == 0\n",
"\n",
"# The integer value of True is 1, so this statement is False\n",
"True == 0\n",
"\n",
"# This statement is True (see above).\n",
"True == 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2.2: Make a Fahrenheit-Celsius conversion table\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 -17.77777777777778\n",
"10 -12.222222222222223\n",
"20 -6.666666666666667\n",
"30 -1.1111111111111112\n",
"40 4.444444444444445\n",
"50 10.0\n",
"60 15.555555555555557\n",
"70 21.11111111111111\n",
"80 26.666666666666668\n",
"90 32.22222222222222\n",
"100 37.77777777777778\n"
]
}
],
"source": [
"# Make a Fahrenheit-Celsius conversion table\n",
"F = 0 # Initialise F\n",
"dF = 10 # Increment for F within the loop\n",
"while F <= 100: # Loop heading with condition\n",
" C = (F - 32)*(5.0/9.0) # 1st statement inside loop\n",
" print(F, C) # 2nd statement inside loop\n",
" F = F + dF # Increment F for the next iteration of the loop."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2.3: Write an approximate Fahrenheit-Celsius conversion table\n",
"Many people use an approximate formula for quickly converting Fahrenheit ($F$) to Celsius ($C$) degrees:\n",
"$C \\approx \\hat{C} = \\frac{F − 30}{2}$\n",
"Modify the program from the previous exercise so that it prints three columns: $F$, $C$, and the approximate value $\\hat{C}$."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 -17.77777777777778 -15.0\n",
"10 -12.222222222222223 -10.0\n",
"20 -6.666666666666667 -5.0\n",
"30 -1.1111111111111112 0.0\n",
"40 4.444444444444445 5.0\n",
"50 10.0 10.0\n",
"60 15.555555555555557 15.0\n",
"70 21.11111111111111 20.0\n",
"80 26.666666666666668 25.0\n",
"90 32.22222222222222 30.0\n",
"100 37.77777777777778 35.0\n"
]
}
],
"source": [
"# Write an approximate Fahrenheit-Celsius conversion table\n",
"F = 0 # Initialise F\n",
"dF = 10 # Increment for F within the loop\n",
"while F <= 100: # Loop heading with condition\n",
" C = (F - 32)*(5.0/9.0) # 1st statement inside loop\n",
" C_hat = (F - 30)/2.0 # Compute the approximate value of C.\n",
" print(F, C, C_hat) # 2nd statement inside loop\n",
" F = F + dF # Increment F for the next iteration of the loop."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2.4: Store odd numbers in a list\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15]\n"
]
}
],
"source": [
"# Store odd numbers in a list\n",
"n = 16\n",
"\n",
"i = 1\n",
"odd_numbers = []\n",
"while i <= n:\n",
" odd_numbers.append(i) # Append the odd number 'i' to the list called 'odd_numbers'\n",
" i = i + 2\n",
"print(\"The odd numbers list = \", odd_numbers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise 2.5: Repeat the previous exercise using: a for loop, list comprehension and the range function"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15]\n",
"The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15]\n"
]
}
],
"source": [
"# Generate odd numbers by a list comprehension\n",
"n = 16\n",
"\n",
"# Use list comprehension to generate the list of odd numbers.\n",
"# Remember to choose the step size to be 2 here.\n",
"odd_numbers = [i for i in range(1, n+1, 2)]\n",
"print(\"The odd numbers list = \", odd_numbers)\n",
"\n",
"# You could also do (using the default 'start' and 'step' values in the range function)\n",
"odd_numbers = [2*i + 1 for i in range(int((n + 1)/2))]\n",
"print(\"The odd numbers list = \", odd_numbers)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}