Session 2 - Exercises Flashcards

1
Q

Explain this code - (7):

A

We prompt the user to input two numbers: lowerNumber and higherNumber. These will determine the range of numbers to count from and to.

We check if higherNumber is less than lowerNumber using the if statement. If it is, it means we need to count down from lowerNumber to higherNumber

Inside the if block, we use a for loop to iterate over the range of numbers from lowerNumber to higherNumber - 1 in reverse order (i.e., counting down). We specify -1 as the step size to decrement the numbers (-1 after highNumber-1)

Within the loop, we print each number.

If the condition in the if statement is not met, meaning higherNumber is greater than or equal to lowerNumber, we execute the else block.

Inside the else block, we use another for loop to iterate over the range of numbers from lowerNumber to higherNumber + 1 (inclusive). The default step size is 1, so we don’t need to specify it explicitly.

Within this loop, we also print each number.

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

Example output of this code:

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

Create a dictionary (use whichever variable name you like) which maps strings to integers as follows:

Print out the whole dictionary, then print out the list of keys (i.e. person names). Finally, print out just the entry for personC.

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

Use a for loop to print every number from 1 to 20 inclusive, but don’t print when the number is 13.

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

use a for loop to print numbers from 1 to 20 inclusive, but this time use a combination of formatting, if statements (think about how you can test if a number is evenly divisible by 5) and the end argument to print to print five numbers per line and line everything up neatly:

Your output should look like this:

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20 - (4)

A

for num in range(1, 21):: This line initializes a for loop that iterates over numbers from 1 to 20 (inclusive). The variable num takes on each of these values in sequence.

if num % 5 == 0:: This if statement checks whether the current value of num is divisible by 5. The % operator gives the remainder when num is divided by 5. If the remainder is 0, it means num is divisible by 5.

print(f’{num:2d}’): If num is divisible by 5 (i.e., if the condition in the if statement is true), this line prints the value of num with a minimum width of 2 characters. It adds leading spaces if necessary to ensure that the number occupies at least 2 characters. Additionally, it adds a newline character at the end, so the next print statement will start on a new line.

print(f’{num:2d}’, end=” “): If num is not divisible by 5 (i.e., the condition in the if statement is false), this line prints the value of num with a minimum width of 2 characters, similarly to the previous line. However, it does not add a newline character at the end. Instead, it ends the print statement with a space (“ “), meaning that the next thing to be printed will appear on the same line.

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

Set up a list containing the numbers 5, 10, 15, 20, 25. Print out the list, then randomise the order of the elements in the list (don’t forget to import the right routine). Print the list again.

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

Think of the data structures (list, dictionary or tuple) which would be most appropriate for storing the following information when coding in python. Explain your choices and, if using a dictionary, which item would be the key and which the value:

Words to be used as stimuli for an experiment

A

a.

A list of strings
# For example: [‘my’, ‘list’, ‘of’, ‘words’]

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

Think of the data structures (list, dictionary or tuple) which would be most appropriate for storing the following information when coding in python. Explain your choices and, if using a dictionary, which item would be the key and which the value:

The date of birth for each participant in an experiment (assume that participants have a unique identifier) - (3)

A

A dictionary with the participant id as the key and
# date of birth as the values
# For example: {‘P1’: ‘1980-01-01’, ‘P2’: ‘1975-12-22’}

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

Think of the data structures (list, dictionary or tuple) which would be most appropriate for storing the following information when coding in python. Explain your choices and, if using a dictionary, which item would be the key and which the value:

Percentage correct scores for an experiment - (2)

A

A list of scores
# For example: [77, 56, 75, 51, 62, 61]

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

Think of the data structures (list, dictionary or tuple) which would be most appropriate for storing the following information when coding in python. Explain your choices and, if using a dictionary, which item would be the key and which the value:

Advanced: For the next question you may wish to think about more complex options, for instance a list containing dictionaries or a dictionary in which the items are lists. Make sure you think about the difference between the two

A set of five scores for each participant in a study - (4)

A

A dictionary with the participant id as the key and
# a list of scores as the values
# For example: {‘P1’: [9, 8, 5, 5, 7],
# ‘P2’: [6, 1, 2, 5, 4]}

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