Chapter 5 continued: Nested Sequences and Dictionaries
- When to use a tuple instead of a list.
- Tuples are faster than lists. Because tuples cannot be changed, the computer can store them in a different way.
- Use tuples when you have constants.
- Lists are flexible: because of this, use lists most of the time.
- Nested Sequences
- Sequences inside other sequences
- Enclose entire list in brackets, each element should be contained in parenthesis if a tuple or brackets if it is a list. Do this only if the element contains more than one term.
- Example:
- nested=[“first”, (“second”, “third”), [“fourth”, “fifth”, “sixth”]]
- Test in idle: The term above ; then print nested.
- scores=[(“Moe”, 1000), (“Larry”, 1500), (“Curly”, 3000)]
- scores is a list that contains 3 tuples.
- Accessing Nested Elements
- You access element in a nested sequence through indexing.
Examples- Type coding into Idle- Label output next to print statement:
scores=[(“Moe”, 1000), (“Larry”, 1500), (“Curly”, 3000)]
print(scores[0])
print(scores[1])
print(scores[2])
a_score=scores[2]
print(a_score)
print (a_score[0])
print(scores[2][0])
- Run High Scores2
Questions:
1. What is choice?
2. When one is chosen, the loop
for entry in scores:
score, name=entry
print(name,”\t”, score)
What is the value of entry?
What is scores?
What gets stored in score,name?
What gets printed?
- Adding a score by appending a nested tuple
- When choice is 2-
- How does the name and score get entered into the tuple?
- What does scores.sort(reverse=True) do?
- What does scores=scores[:5]
Practice with nested Sequences.
Create a nested list – that contains the name of a bowler and their three game totals. Have the list contain the information for 3 bowlers. The Bowler’s name and scores can not be changed ( use a tuple)
Information: John: Scores, 200, 170, 225
Sue: Scores, 290, 220, 190
Pete: Scores, 190, 210, 215
Print out each bowlers name and their three scores.
Find the average of each bowlers games.
Print out the highest score.
Challenge- try to use nested for loops to find the average. Store the averages in a list.
III. Creating Dictionaries.
- Stores data in pairs just like a regular dictionary.
- A key word is associated with a value
- A dictionary can not contain multiple items with the same key.
- A key is immutable. It can be a string, number or tuple.
- Values do not have to be unique.
- Run Geek Translator Program.
- Questions for Program.
- Choose 1: Type in Googling- What happened?
Find line of coding that found term then showed definition.
- Choose 2: Enter “IT” for term then definition of “Information Technology”.
Find the code that entered in the term to the dictionary.
What would happen if you entered a term that was already in the dictionary?
Find the coding that handled this.
- Choose 3- enter “IT”, then enter Computer System Technology. What is different between the coding in for Choice 2 and Choice 3?
- Choose #4- enter “IT”. What coding deletes a term?
- How does the program end?
H.
Selected Dictionary MethodsMethod / Description
get(key,[default]) / Returns the value of key. If key doesn’t exist then the optional default is returned. If default isn’t specified “none” appears
keys() / Returns a view of all keys in dicionary
values() / Returns a view of all values in dictionary
items() / Returns a view of all items in dictionary. Each item is a two-element tuple, where the first element is the key and the second element is the key’s value.
I. Create a Dictionary of at least 5 words of your choice. Display the keys, then the values , then the items using the above methods. Allow the user to look up a definition, change a definition and delete an entry.
IV. Hangman game
Play and analyze Hangman game.
Programming Assignment
- Create a two dimensional list that holds a person’s name and phone number. The list should hold at least 5 different people’s information should be in the list. The program should allow the user to display the phone book and add new members to the phone book. A menu should be displayed initially with the options.
- Write a “Who’s Your Daddy “ program. The program les the user enter the name of a male and produces the name of his father. ( You can use celebrities, fictional characters or even historical figures) Allow the user to add, replace and delete son-father pairs. This program uses a dictionary.