Block 3 Part 2 - Python Code Flashcards
Create the following list as a variable:
a_list
items: 1, 10, 23, 2
a_list = [1, 10, 23, 2]
Check the length of the list.
len(a_list)
Add the number 5 to the end of the list.
a_list = a_list + [5]
Check the value of the last element in a_list.
a_list[len(a_list) - 1]
Display a_list on the screen
a_list
True or false?
Dictionaries – just like any other data items – are objects with an id, type and value.
True
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?
Item are surrounded by {} instead of brackets.
Word has apostrophes, e.g. ‘word’.
It has two key-value pairs:
train + definition
bicycle + definition
True or false?
Dictionaries have something in common with lists: they are both containers.
True
Can we create an empty dictionary?
Yes - it might look something like this:
train_types = {}
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_____
key
value
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?
keys
population_millions[‘USA’]
319 (As per my previous input)
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’.
False - they are case sensitive so be sure to write it correctly.
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.
overwrites
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_____.
error message
stop
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?
True
Random dictionary key selection
Two steps to this:
- turning the dictionary into a l____ of its
k____, then - picking a r______ item from that list.
a list of its keys
random
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.
True
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’?
from random import *
Selecting a random item from a list
Note that the function choice() takes a l_____ as its argument.
list
Getting user input
To get input from the user, we use the f___________:
input()
It has one argument, which is a s________.
function
string
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.
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.
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? ‘
variable
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.
instantiating
(In this case, instantiate means to fill in the pattern)
The interactive loop pattern
The interactive loop pattern can be found
in the course materials, namely in the ‘Problem solving and Python quick r________’.
reference
The interactive loop pattern
How would you set a variable called ‘exit’ to false?
a) exit = ‘false’
b) exit = False
b)
We do not need to use a string as Python has Boolean values True and False.
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?
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).
The echo chamber interactive loop