Session 1 - Exercises Flashcards

1
Q

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

A

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 = f
b
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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”.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

A

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])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Exercise 5
Set up a tuple containing three floating point numbers: 2.0, 4.0, 6.0. Print out the tuple.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Exercise 6
Print every number from 0 to 30 inclusive.

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Exercise 7
Use a loop to print every odd number between 1 to 30 inclusive.

A

for num in range(1, 31, 2):
print(num)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

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()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

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’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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 = f
b
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

a = 8
b = 24
c = 0.5
d = 100
e = 3.2
f = 1.4142135623730951
g = 4096.000000000006
h = 3.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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}”)

A

Area: 10.0
Width 5.0
Height 2.0
Area: 10.000, Width: 5.000, Height: 2.000

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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])

A

1
4
4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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)

A

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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])

A

(2.0, 4.0, 6.0)
4.0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the output?

# 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)

A

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

17
Q

What is the output?

for x in range(0,31,2): #starts from 0 to 30 and steps of 2
print(x)

A

0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30

18
Q

What is the output?

for num in range(1, 31, 2):
print(num)

A

1
3
5
7
9

29

19
Q

What is the output?
number = 3

Print the multiplication
for mult in range(1, 13):
print(number,” x “ , mult , “ = “ , (number * mult))

A

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36

20
Q

What is the output?
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()
A

Multiplication table for 1

1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
1 x 11 = 11
1 x 12 = 12

Multiplication table for 2

2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24

Multiplication table for 3

3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
3 x 11 = 33
3 x 12 = 36

Multiplication table for 4

4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48

Multiplication table for 5

5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
5 x 11 = 55
5 x 12 = 60

Multiplication table for 6

6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
6 x 10 = 60
6 x 11 = 66
6 x 12 = 72

Multiplication table for 7

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84

Multiplication table for 8

8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
8 x 11 = 88
8 x 12 = 96

Multiplication table for 9

9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
9 x 11 = 99
9 x 12 = 108

Multiplication table for 10

10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100
10 x 11 = 110
10 x 12 = 120

Multiplication table for 11

11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110
11 x 11 = 121
11 x 12 = 132

Multiplication table for 12

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
12 x 11 = 132
12 x 12 = 144

21
Q

What is the output?

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’)

A

-10 degrees C = 14.0 degrees F
-5 degrees C = 23.0 degrees F
0 degrees C = 32.0 degrees F
5 degrees C = 41.0 degrees F
10 degrees C = 50.0 degrees F
15 degrees C = 59.0 degrees F
20 degrees C = 68.0 degrees F
25 degrees C = 77.0 degrees F
30 degrees C = 86.0 degrees F
35 degrees C = 95.0 degrees F
40 degrees C = 104.0 degrees F
45 degrees C = 113.0 degrees F
50 degrees C = 122.0 degrees F

22
Q
A