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
True
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.
# 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.
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
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}$.
# 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.
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
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.
# 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)
The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15]
# 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)
The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15] The odd numbers list = [1, 3, 5, 7, 9, 11, 13, 15]