Lists and dictionaries organize data in structures for programs.
In [3]:
cat = {'name' : 'Zophie', 'age': 7, 'color':'gray'}
A list of Dictionaries isa Data Structure.
In [4]:
allCats = []
allCats.append({'name' : 'Zophie', 'age': 7, 'color':'gray'})
allCats.append({'name' : 'Fooka', 'age': 5, 'color':'black'})
allCats.append({'name' : 'Fat-tail', 'age': 5, 'color':'gray'})
allCats.append({'name' : '???', 'age': -1, 'color':'orange'})
print(allCats)
In [7]:
from IPython.display import Image
Image(url='https://automatetheboringstuff.com/images/000003.png')
Out[7]:
We will use data structures to create a representation of this board. Use string values to represent the nine spaces. The dictionary values will hold the X's and O's, and the position strings ('low-R') will be the keys.
In [29]:
import pprint
theBoard = {
'top-L':' ',
'top-M':' ',
'top-R':' ',
'mid-L':' ',
'mid-M':' ',
'mid-R':' ',
'low-L':' ',
'low-M':' ',
'low-R':' ',
}
pprint.pprint(theBoard)
The keys are arbitrary; they are just used to store and change values.
Machine version:
In [30]:
theBoard['mid-M'] = 'X'
pprint.pprint(theBoard)
Human version:
In [32]:
from IPython.display import Image
Image(url='https://automatetheboringstuff.com/images/000008.png')
Out[32]:
How would we draw this board and include win conditions? Defining functions.
In [34]:
def printBoard(board):
print(board['top-L'] + ' | ' + board['top-M'] + ' | ' + board['top-R'])
print('----------')
print(board['mid-L'] + ' | ' + board['mid-M'] + ' | ' + board['mid-R'])
print('----------')
print(board['low-L'] + ' | ' + board['low-M'] + ' | ' + board['low-R'])
printBoard(theBoard)
Dictionaries, lists, and strings can be used in combination to simulate real world things, translating machine responses to human responses.
If confused about what type of data you're dealing with, use the type() function.
In [43]:
print('type(42)')
print(type(42))
print('type(hello)')
print(type('hello'))
print('type(3.14)')
print(type(3.14))
print('type(theBoard)')
print(type(theBoard))
print('type(theBoard[top-R])')
print(type(theBoard['top-R']))