Lesson 17:

Data Structures

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)


[{'color': 'gray', 'name': 'Zophie', 'age': 7}, {'color': 'black', 'name': 'Fooka', 'age': 5}, {'color': 'gray', 'name': 'Fat-tail', 'age': 5}, {'color': 'orange', 'name': '???', 'age': -1}]

The Tic-Tac-Toe Game Program

We can use data structures to represent values in Python that can be understood. For example, using key-value pairs and strings to represent regions of a tic-tac-toe board.


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)


{'low-L': ' ',
 'low-M': ' ',
 'low-R': ' ',
 'mid-L': ' ',
 'mid-M': ' ',
 'mid-R': ' ',
 'top-L': ' ',
 'top-M': ' ',
 'top-R': ' '}

The keys are arbitrary; they are just used to store and change values.

Machine version:


In [30]:
theBoard['mid-M'] = 'X'

pprint.pprint(theBoard)


{'low-L': ' ',
 'low-M': ' ',
 'low-R': ' ',
 'mid-L': ' ',
 'mid-M': 'X',
 'mid-R': ' ',
 'top-L': ' ',
 'top-M': ' ',
 'top-R': ' '}

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)


  |   |  
----------
  | X |  
----------
  |   |  

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


type(42)
<class 'int'>
type(hello)
<class 'str'>
type(3.14)
<class 'float'>
type(theBoard)
<class 'dict'>
type(theBoard[top-R])
<class 'str'>