variables, references and data structures
TRANSCRIPT
COMP 1001: Introduction to Computers for Arts and Social Sciences
Making Stories Interactive
Variables, References and Data Structures
COMP 1001: Introduction to Computers for Arts and Social Sciences
Learning Objectives
• Learn how information is stored on a computer.
• Learn basic programming concepts (variables, if statements, loops, and
functions) and write simple programs using these concepts.
COMP 1001: Introduction to Computers for Arts and Social Sciences
COMP 1001: Introduction to Computers for Arts and Social Sciences
What is a Story?
COMP 1001: Introduction to Computers for Arts and Social Sciences
ACTORS perform actions
THE WORLD changes
EVENTS relate to each other
COMP 1001: Introduction to Computers for Arts and Social Sciences
About people, not things STORIES:
conflict
COMP 1001: Introduction to Computers for Arts and Social Sciences
Interactivity
COMP 1001: Introduction to Computers for Arts and Social Sciences
Two or more agents
Understand each other
Respond properly
COMP 1001: Introduction to Computers for Arts and Social Sciences
Tools
COMP 1001: Introduction to Computers for Arts and Social Sciences
http://twinery.org/
COMP 1001: Introduction to Computers for Arts and Social Sciences
http://www.inklestudios.com/inklewriter/
COMP 1001: Introduction to Computers for Arts and Social Sciences
http://www.renpy.org/
COMP 1001: Introduction to Computers for Arts and Social Sciences
From the Beginning…
COMP 1001: Introduction to Computers for Arts and Social Sciences
We will examine how to do a simple text-based interactive story.
COMP 1001: Introduction to Computers for Arts and Social Sciences
User Input
We want to ask the user a question.
print("Choose from the following options:")
print("1: go left")
print("2: go right")
input("> ")
COMP 1001: Introduction to Computers for Arts and Social Sciences
User Input
We want to ask the user a question.
print("Choose from the following options:")
print("1: go left")
print("2: go right")
input("> ") This function returns an answer. Where should we put it?
COMP 1001: Introduction to Computers for Arts and Social Sciences
Variables
A variable is like a box we can put information in.
COMP 1001: Introduction to Computers for Arts and Social Sciences
answer = input("> ")
answer
2
Variables
The user types 2 and presses enter.
COMP 1001: Introduction to Computers for Arts and Social Sciences
answer = input("> ")
answer
Variables
The value is stored in the box.
2
COMP 1001: Introduction to Computers for Arts and Social Sciences
if answer == 2:
print("You chose to go left, ")
print(" a most dangerous path.")
answer
2
Variables
COMP 1001: Introduction to Computers for Arts and Social Sciences
if answer == 2:
print("You chose to go left, ")
print(" a most dangerous path.")
answer
2
Variables
Look at what's inside the box and
do something with it
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
0
Variables
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
0
Variables
Put zero in a box labelled x.
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
0
Variables
Peek in the box to get the value…
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
0
Variables
…and compare it to 2
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
0
Variables
Peek in the box to get the value…
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
1
Variables
…add 1 to it…
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
1
Variables
…and put the result back into the box
COMP 1001: Introduction to Computers for Arts and Social Sciences
x = 0
while x < 2:
x = x + 1
x
Variables
Now the box contains the value 1.
1
COMP 1001: Introduction to Computers for Arts and Social Sciences
What is the value of y after the execution of this code? x = 37
y = x + 2
x = 20
Text 37607 1052115: 39 1052116: 22 1052117: 59 1052118: 20 1052119: 57
COMP 1001: Introduction to Computers for Arts and Social Sciences
Data Types
We know there are different ways of representing data in memory using binary
numbers.
COMP 1001: Introduction to Computers for Arts and Social Sciences
Data Types
Name Type
int plain integer – no decimal point
float floating point number – has a decimal point
complex real + imaginary parts
string text between quotes
COMP 1001: Introduction to Computers for Arts and Social Sciences
Data Types
We know there are different ways of representing data in memory using binary
numbers.
How does our variable know what type of data it is storing?
COMP 1001: Introduction to Computers for Arts and Social Sciences
Data Types
Answer: Context.
Variable Data Type
a = 3 integer
a = 3.4 float
a = "3.4" string
a = 0b11 integer
a = "0b11" string
COMP 1001: Introduction to Computers for Arts and Social Sciences
Stories as Data
COMP 1001: Introduction to Computers for Arts and Social Sciences
Storing Story Data We'll represent a short story based on the classic nursery
rhyme Little Miss Muffet:
Little Miss Muffet sat on her tuffet Eating her curds and whey When along came a spider And sat down beside her
And scared Miss Muffet away
http://crown-heart.deviantart.com/art/Little-Miss-Muffet-162099928
COMP 1001: Introduction to Computers for Arts and Social Sciences
1. The sun shone brightly when Miss Muffet finally arrived at her favorite tuffet. Though it seemed odd to others that she’d decide to store any furniture outdoors, Miss Muffet loved sitting on the footstool under her favorite tree.
2. Just like every morning, Miss Muffet was famished. Luckily, just like every morning, she had brought her curds and whey to enjoy for breakfast.
3. As Miss Muffet brought the first spoonful to her mouth, a very long-legged but otherwise friendly spider noticed her sitting below. The spider headed downward, released his thread above him, so he could visit the curious creature now seated under his tree.
4. As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider.
5. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.
COMP 1001: Introduction to Computers for Arts and Social Sciences
1. The sun shone brightly when Miss Muffet finally arrived at her favorite tuffet. Though it seemed odd to others that she’d decide to store any furniture outdoors, Miss Muffet loved sitting on the footstool under her favorite tree.
2. Just like every morning, Miss Muffet was famished. Luckily, just like every morning, she had brought her curds and whey to enjoy for breakfast.
3. As Miss Muffet brought the first spoonful to her mouth, a very long-legged but otherwise friendly spider noticed her sitting below. The spider headed downward, released his thread above him, so he could visit the curious creature now seated under his tree.
4. As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider.
5. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.
What data type do we need, and how will we store these values?
COMP 1001: Introduction to Computers for Arts and Social Sciences
Data Structures
Used to organize information on a computer so it can be
used efficiently for the task at hand
COMP 1001: Introduction to Computers for Arts and Social Sciences
The List Data Structure
COMP 1001: Introduction to Computers for Arts and Social Sciences
The List Data Structure
These numbers are called indexes
COMP 1001: Introduction to Computers for Arts and Social Sciences
The List Data Structure
Computer scientists count from zero
COMP 1001: Introduction to Computers for Arts and Social Sciences
Index Scene
0
The sun shone brightly when Miss Muffet finally arrived at her
favorite tuffet. Though it seemed odd to others that she’d decide
to store any furniture outdoors, Miss Muffet loved sitting on the
footstool under her favorite tree.
1
Just like every morning, Miss Muffet was famished. Luckily, just
like every morning, she had brought her curds and whey to enjoy
for breakfast.
2
As Miss Muffet brought the first spoonful to her mouth, a very
long-legged but otherwise friendly spider noticed her sitting
below. The spider headed downward, released his thread above
him, so he could visit the curious creature now seated under his
tree.
3
As the spider landed on Miss Muffet’s spoon, the girl sensed
something was wrong. As soon as she saw the insect, she
screamed before she could even consider that she was, in fact,
facing a friendly spider.
4
Miss Muffet was gone before the spider could even say hello. The
spider felt terrible for scaring her away, but soon forgot as he
investigated the curds and whey left behind.
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Creating a list:
[5, 3, -2, 10]
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Creating a list:
[1, -9.5555, "hello", [1, 2, 3]]
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Creating a list:
[1, -9.5555, "hello", [1, 2, 3]]
Lists can contain multiple data types – even other
lists!
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Assigning a list to a variable:
myList = [1, -9.5555, "hello", [1, 2, 3]]
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Accessing items in a list:
myList = [1, -9.5555, "hello", [1, 2, 3]]
print(myList[2])
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Accessing items in a list:
myList = [1, -9.5555, "hello", [1, 2, 3]]
print(myList[2])
Prints "hello"
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Getting the length of a list:
len(myList)
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Getting the length of a list:
print(myList[len(myList)-1])
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Getting the length of a list:
print(myList[len(myList)-1])
Prints the last item in the list (remember, starts at
zero!)
COMP 1001: Introduction to Computers for Arts and Social Sciences
Lists in Python
Getting the length of a list:
print(myList[-1])
Shortcut for accessing last item in the list
COMP 1001: Introduction to Computers for Arts and Social Sciences
missMuffetScenes = [
"The sun shone brightly…",
"Just like every morning…",
"As Miss Muffet brought…",
"As the spider landed on…",
"Miss Muffet was gone…"
]
Scene text abbreviated for space…
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
How can we print out our story scenes?
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
print(missMuffetScenes[0])
print(missMuffetScenes[1])
print(missMuffetScenes[2])
print(missMuffetScenes[3])
print(missMuffetScenes[4])
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
print(missMuffetScenes[0])
print(missMuffetScenes[1])
print(missMuffetScenes[2])
print(missMuffetScenes[3])
print(missMuffetScenes[4])
Is this really going to work for longer stories?
COMP 1001: Introduction to Computers for Arts and Social Sciences
While Loop
Drive the track while the race is not over
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
We want the first index to give us the first scene
in the list
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
Drive laps so long as we haven't gone past the
last item
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
Should use len(missMuffetScenes)-1
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
Drive the lap is printing the scene…
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
currentIndex = 0
while currentIndex <= 4:
print(missMuffetScenes[currentIndex])
currentIndex = currentIndex + 1
…and updating the index to get ready for the next lap
COMP 1001: Introduction to Computers for Arts and Social Sciences
For Loop
Drive the track one lap for every item in a list
COMP 1001: Introduction to Computers for Arts and Social Sciences
For Loop
for <variableName> in <listName>:
<code to run a number of times
equal to the size of the list>
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
for scene in missMuffetScenes:
print(scene)
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
for scene in missMuffetScenes:
print(scene)
The list that we will be looping over
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
for scene in missMuffetScenes:
print(scene)
This variable will get a new value from the list every lap
COMP 1001: Introduction to Computers for Arts and Social Sciences
Printing List Items
for scene in missMuffetScenes:
print(scene)
Driving the lap is just printing this time – everything else is
updated automatically
COMP 1001: Introduction to Computers for Arts and Social Sciences
Things to Do With Scene Lists
Useful in video games, when players can explore the world in
between scenes.
Allows us to rearrange the ordering of the scenes.
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
Lists can be rearranged with a series of swaps.
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
Swapping the first and last items…
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
Swapping the first and last items…
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
!!!
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
Get the contents of the first box…
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
…and put them into the last box
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = missMuffetScenes[-1]
Now we're just copying scene1 back into the first box
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
Swapping the first and last items…
temp = missMuffetScenes[-1]
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = temp
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
temp = missMuffetScenes[-1]
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = temp
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
temp = missMuffetScenes[-1]
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = temp
First put scene3 into the temporary box
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
temp = missMuffetScenes[-1]
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = temp
COMP 1001: Introduction to Computers for Arts and Social Sciences
Rearranging List Items
temp = missMuffetScenes[-1]
missMuffetScenes[-1] = missMuffetScenes[0]
missMuffetScenes[0] = temp
Then we can copy scene1 to the last box before copying scene3 into the first box (now
shown)
COMP 1001: Introduction to Computers for Arts and Social Sciences
What is the value of a after the execution of this code? a = [2, 4, 6, 8]
a.remove(4) #item
a.pop(2) #index
Text 37607 622977: [2,4] 622997: [6,8] 622998: [2,6] 622999: [2,8] 623000: Nothing / error
COMP 1001: Introduction to Computers for Arts and Social Sciences
What is printed by this code? lst = [3, 6, 9]
sum = 0
counter = 0
while counter < len(lst):
sum += counter
counter += 2
print(sum)
Text 37607 634005: 18 634007: 6 634014: 2 634015: 9 634021: Nothing / error
COMP 1001: Introduction to Computers for Arts and Social Sciences
Branching Stories
COMP 1001: Introduction to Computers for Arts and Social Sciences
COMP 1001: Introduction to Computers for Arts and Social Sciences
This is a tree data structure
COMP 1001: Introduction to Computers for Arts and Social Sciences
As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. As soon as she saw the insect, she screamed before she could even consider that she was, in fact, facing a friendly spider. Miss Muffet was gone before the spider could even say hello. The spider felt terrible for scaring her away, but soon forgot as he investigated the curds and whey left behind.
The End
COMP 1001: Introduction to Computers for Arts and Social Sciences
As the spider landed on Miss Muffet’s spoon, the girl sensed something was wrong. Miss Muffet should run away as soon as she sees the spider. Miss Muffet should stay to greet the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Miss Muffet held out her spoon as the wide-eyed spider grinned and lunged for the delicious snack. From that day forward, the spider joined Miss Muffet every morning for breakfast with his new friend.
The end
COMP 1001: Introduction to Computers for Arts and Social Sciences
When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Offer the spider some curds and whey. Don’t share with the spider.
COMP 1001: Introduction to Computers for Arts and Social Sciences
When she saw the spider, curiosity got the best of her. “Why hello, there, little friend!” she said. Although Miss Muffet tried to make small talk with the spider, he was a little put off by her unwillingness to share. The spider decided he’d try another tree to see if he could find someone a little more generous with their breakfast. Miss Muffet never saw him again.
The end
COMP 1001: Introduction to Computers for Arts and Social Sciences
COMP 1001: Introduction to Computers for Arts and Social Sciences
How can we represent a scene as data?
COMP 1001: Introduction to Computers for Arts and Social Sciences
How can we represent a scene as data?
We need: - The text of a scene - Scenes that either offer a choice or don't - For scenes that do offer a choice, the text
for the options - What scene to go to next (automatically or
based on choice)
COMP 1001: Introduction to Computers for Arts and Social Sciences
[False,
"Just like every morning, Miss Muffet was
famished. Luckily, just like every morning, she
had brought her curds and whey to enjoy for
breakfast.",
"SpoonMouth"]
COMP 1001: Introduction to Computers for Arts and Social Sciences
[False,
"Just like every morning, Miss Muffet was
famished. Luckily, just like every morning, she
had brought her curds and whey to enjoy for
breakfast.",
"SpoonMouth"]
There are no choices in this scene.
COMP 1001: Introduction to Computers for Arts and Social Sciences
[False,
"Just like every morning, Miss Muffet was
famished. Luckily, just like every morning, she
had brought her curds and whey to enjoy for
breakfast.",
"SpoonMouth"] All scenes have their scene text to print
COMP 1001: Introduction to Computers for Arts and Social Sciences
[False,
"Just like every morning, Miss Muffet was
famished. Luckily, just like every morning, she
had brought her curds and whey to enjoy for
breakfast.",
"SpoonMouth"] This is the next scene to visit
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
This scene does have a choice
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
Scene text
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
Option 1
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
Next scene if option 1 is chosen
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"]
Option 2
COMP 1001: Introduction to Computers for Arts and Social Sciences
[True,
" As the spider landed on Miss Muffet’s spoon,
the girl sensed something was wrong.",
"Miss Muffet should run away as soon as she sees
the spider.",
"RunAway",
"Miss Muffet should stay to greet the spider.",
"Stay"] Next scene if
option 2 is chosen
COMP 1001: Introduction to Computers for Arts and Social Sciences
Now we have scene data – how can we associate the
scene name with the data?
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
The dictionary data structure associates one piece of data with
another.
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary = {"one" : 1, "two" : 2, "three" : 3}
Creating a new dictionary:
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary = {"one" : 1, "two" : 2, "three" : 3}
Creating a new dictionary:
Keys
(similar to words in a dictionary to look up)
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary = {"one" : 1, "two" : 2, "three" : 3}
Creating a new dictionary:
Values
(similar to the definition found when looking up the word)
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary = {"one" : 1, "two" : 2, "three" : 3}
Creating a new dictionary:
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
print(myDictionary["two"])
Accessing values in a dictionary:
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
print(myDictionary["two"])
Accessing values in a dictionary:
Prints 2
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary["two"] = 22
Replacing values in a dictionary:
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
myDictionary["ten"] = 10
Adding new values to a dictionary:
COMP 1001: Introduction to Computers for Arts and Social Sciences
Dictionaries
for key in myDictionary.keys():
print(myDictionary[key])
Printing values in a dictionary:
for value in myDictionary.values():
print(value)
print(list(myDictionary.keys()))
print(list(myDictionary.values()))
COMP 1001: Introduction to Computers for Arts and Social Sciences
What is the value of d after this code runs? d = {3:4, 4:5, 5:6}
d[3] = 8
d[6] = 7
Text 37607 729146: {3:4, 4:5, 5:8} 729147: {3:8, 4:5, 5:6} 729151: {3:4, 4:5, 5:8, 6:7} 729152: {3:8, 4:5, 5:6, 6:7} 729153: Nothing / error
COMP 1001: Introduction to Computers for Arts and Social Sciences
References
COMP 1001: Introduction to Computers for Arts and Social Sciences
A Look at Our Data's Structure
COMP 1001: Introduction to Computers for Arts and Social Sciences
A Look at Our Data's Structure