Chapter 5 continued: Nested Sequences and Dictionaries

  1. When to use a tuple instead of a list.
  2. Tuples are faster than lists. Because tuples cannot be changed, the computer can store them in a different way.
  3. Use tuples when you have constants.
  4. Lists are flexible: because of this, use lists most of the time.
  1. Nested Sequences
  2. Sequences inside other sequences
  3. 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.
  4. Example:
  5. nested=[“first”, (“second”, “third”), [“fourth”, “fifth”, “sixth”]]
  6. Test in idle: The term above ; then print nested.
  7. scores=[(“Moe”, 1000), (“Larry”, 1500), (“Curly”, 3000)]
  8. scores is a list that contains 3 tuples.
  9. Accessing Nested Elements
  10. 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])

  1. 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?

  1. Adding a score by appending a nested tuple
  2. When choice is 2-
  3. How does the name and score get entered into the tuple?
  4. What does scores.sort(reverse=True) do?
  5. 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.

  1. Stores data in pairs just like a regular dictionary.
  2. A key word is associated with a value
  3. A dictionary can not contain multiple items with the same key.
  4. A key is immutable. It can be a string, number or tuple.
  5. Values do not have to be unique.
  6. Run Geek Translator Program.
  7. Questions for Program.
  8. Choose 1: Type in Googling- What happened?

Find line of coding that found term then showed definition.

  1. 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.

  1. Choose 3- enter “IT”, then enter Computer System Technology. What is different between the coding in for Choice 2 and Choice 3?
  1. Choose #4- enter “IT”. What coding deletes a term?
  1. How does the program end?

H.

Selected Dictionary Methods
Method / 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

  1. 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.
  2. 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.