Session 1 - Exercises Flashcards
Exercise 1
Write a single script which prints out the results of the following sums. Place the values into variables before printing them rather than just passing the results to the print function (you will need this for questions g and h).
a. 1+7 b. 3∗8 c. 12 d. 102 e. 3+13+2 f. 212 g. fb h. a∗ba2
Answer for Session 1 Exercise 1
a = 1 + 7
b = 3 * 8
c = 1 / 2
d = 102
e = 3 + (1 / (3 + 2))
f = 2 ** (1 / 2)
g = fb
h = (a * b) / (a ** 2)
# Use format strings to neatly print out our data
print(“a = “,a)
print(“b = “,b)
print(“c = “,c)
print(“d = “,d)
print(“e = “,e)
print(“f = “,f)
print(“g = “,g)
print(“h = “,h)
Exercise 2
Write some code which calculates the area of a rectangle using the formula area = width * height
Do this by setting up a variable for width and height, then calculate the area and store it in the area variable. Then print out “Area: x, Height: y, Width: z”.
Note: In Session 2 we will learn a much nicer way of printing things. Here is a taster:
Answer for Session 1 Exercise 2
Set up our width and height
width = 5.0
height = 2.0
Calculate our area
area = width * height
Output our area, width and height
print(“Area: “, area)
print(“Width”, width)
print(“Height”,height)
print(f”Area: {area:.3f}, Width: {width:.3f}, Height: {height:.3f}”)
formating area, width and height to floating point number with 3DP
Exercise 3
Set up a list called my_list containing the numbers 1 to 4 inclusive. Print the first and last entries in the list. Try and find two different ways to print the last entry in the list
Answer for Session 1 Exercise 3
Set up our list
my_list = [1, 2, 3, 4]
Or we could use my_list = list(arange(1, 5))
Print out the first entry
print(my_list[0])
Print out the last entry
print(my_list[3])
Another way of printing out the last entry
print(my_list[-1])
Exercise 4
Set up an empty list. Loop around the numbers between 0 and 10 and, on each loop iteration, add the number squared to the end of the list. Print the final list.
Answer for Session 1 Exercise 4
Set up an empty list
my_list = []
Append the square of each number from 0 to 10 to the list
# We use the ‘range’ function which generates ranges of numbers using the start,stop,step format.
# So ‘range(1,100,2) is all the odd numbers between 1 and 99 inclusive
for x in range(0, 11):
my_list.append(x ** 2)
Output the list
print(my_list)
Exercise 5
Set up a tuple containing three floating point numbers: 2.0, 4.0, 6.0. Print out the tuple.
Answer for Session 1 Exercise 5
Set up a tuple - remember these are just like lists but you cannot change them.
my_tuple = (2.0, 4.0, 6.0)
Output the tuple
print(my_tuple)
Exercise 6
Print every number from 0 to 30 inclusive.
Answer for Session 1 Exercise 6
Loop over all numbers between 0 and 30
# Use range again - remember the last number in the range is excluded
for num in range(0, 31):
print(num)
Exercise 7
Use a loop to print every odd number between 1 to 30 inclusive.
for num in range(1, 31, 2):
print(num)
Exercise 8
Write python code to create and output a times table (from 1 to 12) of a number which you define as a variable. Your code should start like this:
Number for which to produce the times table
number = 3
An example output might start:
3 x 1 = 3
3 x 2 = 6
…
Answer for Session 1 Exercise 8
Number for which to produce the times table
number = 3
Print the multiplication
for mult in range(1, 13):
print(number,” x “ , mult , “ = “ , (number * mult))
# Again, this is fine.. but using commas to string together strings is a bit clumsy. Here is a fancy way that we will practise more in session 2: # print(f"{number:2d} x {mult:2d} = {number * mult:3d}") # It lets you control the number of decimal places more precisely
:2d - number formatted as deciaml integer with min width of 2 characters
Exercise 9
Take your previous example and use another loop to make it produce multiplication tables for numbers from 1 to 12. Put a title before each table.
Your output should start something like:
Multiplication table for 1
1 x 1 = 1
1 x 2 = 2
and end
12 x 11 = 132
12 x 12 = 144
Answer for Session 1 Exercise 9
for number in range(1, 13):
# Output a header and a blank line
print(f”Multiplication table for {number}”)
print()
# Print the multiplication table for mult in range(1, 13): print(number," x " , mult , " = " , (number * mult)) # Put a blank line after each table for neatness sake print()
Exercise 10
Write a program to output a list of temperatures in Celsius and Fahrenheit running from -10C to 50C.
The equation for calculating Fahrenheit from Celsius is: f=C∗95+32 . italicised text Print the table neatly.
Double-click (or enter) to edit
Answer for Session 1 Exercise 10
Set the range to work between
c_min = -10
c_max = 50
for c in range(c_min, c_max + 1,5):
# Convert degrees C into F
f = c * (9 / 5) + 32
print(c,” degrees C = “, f, “ degrees F”)
# or with the fancy ‘f’ notation…. print(f’{c:6.1f} C = {f:5.1f} F’)
What is output of this code?
a = 1 + 7
b = 3 * 8
c = 1 / 2
d = 102
e = 3 + (1 / (3 + 2))
f = 2 ** (1 / 2)
g = fb
h = (a * b) / (a ** 2)
# Use format strings to neatly print out our data
print(“a = “,a)
print(“b = “,b)
print(“c = “,c)
print(“d = “,d)
print(“e = “,e)
print(“f = “,f)
print(“g = “,g)
print(“h = “,h)
a = 8
b = 24
c = 0.5
d = 100
e = 3.2
f = 1.4142135623730951
g = 4096.000000000006
h = 3.0
Note: In Session 2 we will learn a much nicer way of printing things. Here is a taster:
What is output of this code?
Answer for Session 1 Exercise 2
Set up our width and height
width = 5.0
height = 2.0
Calculate our area
area = width * height
Output our area, width and height
print(“Area: “, area)
print(“Width”, width)
print(“Height”,height)
print(f”Area: {area:.3f}, Width: {width:.3f}, Height: {height:.3f}”)
Area: 10.0
Width 5.0
Height 2.0
Area: 10.000, Width: 5.000, Height: 2.000
What is output of this code?
Answer for Session 1 Exercise 3
Set up our list
my_list = [1, 2, 3, 4]
Or we could use my_list = list(arange(1, 5))
Print out the first entry
print(my_list[0])
Print out the last entry
print(my_list[3])
Another way of printing out the last entry
print(my_list[-1])
1
4
4
We use the ‘range’ function which generates ranges of numbers using the start,stop,step format.
What is the output?
Set up an empty list
my_list = []
for x in range(0, 11):
my_list.append(x ** 2)
Output the list
print(my_list)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
What is the output?
Set up a tuple - remember these are just like lists but you cannot change them.
my_tuple = (2.0, 4.0, 6.0)
Output the tuple
print(my_tuple)
print(my_tuple[1])
(2.0, 4.0, 6.0)
4.0