Block 3 Part 2 - Python Code Flashcards

1
Q

Create the following list as a variable:

a_list

items: 1, 10, 23, 2

A

a_list = [1, 10, 23, 2]

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

Check the length of the list.

A

len(a_list)

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

Add the number 5 to the end of the list.

A

a_list = a_list + [5]

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

Check the value of the last element in a_list.

A

a_list[len(a_list) - 1]

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

Display a_list on the screen

A

a_list

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

True or false?

Dictionaries – just like any other data items – are objects with an id, type and value.

A

True

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

Here is an example of a Python dictionary:

mini_dictionary = {‘train’: ‘A powered
railway vehicle’, ‘bicycle’:
‘A vehicle consisting of
two wheels’}

Look at the syntax.
What’s different in a dictionary to a list?

How many key-value pairs does this dictionary have?

A

Item are surrounded by {} instead of brackets.
Word has apostrophes, e.g. ‘word’.

It has two key-value pairs:
train + definition
bicycle + definition

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

True or false?

Dictionaries have something in common with lists: they are both containers.

A

True

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

Can we create an empty dictionary?

A

Yes - it might look something like this:

train_types = {}

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

Adding items to a Python dictionary

Once you have initialised the dictionary
e.g. population_millions = {}
Use the following code in the shell to add key-value pairs.

population_millions[k___] = v_____

A

key
value

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

Accessing dictionary values

For accessing the values of a dictionary, you can use the k_____, just like you can use an index to access an item in a list.

For instance, in order to access the item with the key key from dictionary population_millions, use:

population_millions[key]

Q: What would you type if you wanted to
access the input for the USA?

Q: What output would you get?

A

keys
population_millions[‘USA’]
319 (As per my previous input)

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

True or false?

Strings are not case-sensitive, so when accessing dictionary items it’s okay if you write, for example, ‘india’, rather than ‘India’.

A

False - they are case sensitive so be sure to write it correctly.

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

Amending a dictionary

To amend a dictionary entry, simply type it in again.

E.g. First entry
population_millions[‘France’] = 670

correct to 67, type again
population_millions[‘France’] = 67

It simply o_________ the previous value.

A

overwrites

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

Checking for a key

Sometimes it is useful to check whether a dictionary contains a particular key, before you try to access the value via that key. Why? If a key doesn’t exist, using the key results in an e______ message. If you are using the key in a program, the execution of the program will s_____.

A

error message
stop

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

Checking for a key

To check for a key in a dictionary, use the shell and type the following code:

key in population_millions

e.g. ‘USA’ in population_millions

Q: What output will we get if it is there?

A

True

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

Random dictionary key selection

Two steps to this:

  1. turning the dictionary into a l____ of its
    k____, then
  2. picking a r______ item from that list.
A

a list of its keys
random

15
Q

Converting a dictionary into a list

In Python, you can convert a dictionary into a list by using the list( ) constructor.

For example:
my_dict = {‘a’: 1, ‘b’: 2, ‘c’: 3}

Convert the dictionary keys to a list
keys_list = list(my_dict)

(In shell) keys_list
»> [‘a’, ‘b’, ‘c’]

True or false?
With this step we are only converting the keys to a list, not the values.

A

True

16
Q

Selecting a random item from a list

To select a random item from a list in Python, you can use the function choice() (after importing the random module).

What is the command to import the ‘random module’?

A

from random import *

17
Q

Selecting a random item from a list

Note that the function choice() takes a l_____ as its argument.

A

list

18
Q

Getting user input

To get input from the user, we use the f___________:

input()

It has one argument, which is a s________.

A

function
string

19
Q

Getting user input

Getting user input is a bit strange in Python.
Look at the following example:

user_input = input(“Enter something: “)

What will actually be stored as user_input will be their answer, not the prompt of ‘Enter something’.

Write an example of this is action.

A

user_input = input(“Enter something: “)

(once enter has been pressed, shell will pop up with statement,
Enter something:
I enter ‘hello’ in shell.
In shell I type: user_input
I then press enter and ‘hello’ is printed.

20
Q

Getting user input

We would write the algorithm to the following code as:

user_input = input(‘How shall I greet you? ‘)

Algorithm
Set the v_________ user_input to the response to ‘How shall I greet you? ‘

A

variable

21
Q

The interactive loop pattern

Suppose I wanted to write a program that keeps asking me for input and does something in response to my input.

This problem can be solved by i_____________ the interactive loop pattern.

A

instantiating

(In this case, instantiate means to fill in the pattern)

22
Q

The interactive loop pattern

The interactive loop pattern can be found
in the course materials, namely in the ‘Problem solving and Python quick r________’.

A

reference

23
Q

The interactive loop pattern

How would you set a variable called ‘exit’ to false?

a) exit = ‘false’
b) exit = False

A

b)
We do not need to use a string as Python has Boolean values True and False.

24
Q

The interactive loop pattern

This program will keep asking
for input until the user enters ‘quit’
“””
exit = False
while exit == False:
  user_input = input(‘Type your input here: ‘)
  if user_input == ‘quit’:
    exit = True

What is the preferred Pythonic way of stating this type of condition?

A

while not exit:

It works because not exit is a Boolean expression. It evaluates to True when exit evaluates to False, and to False when exit evaluates to True (and therefore can replace exit == False).

25
Q

The echo chamber interactive loop

A