Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python: Dictionaries Nicola Licata Charis Kao Dan Green.

Similar presentations


Presentation on theme: "Python: Dictionaries Nicola Licata Charis Kao Dan Green."— Presentation transcript:

1

2 Python: Dictionaries Nicola Licata Charis Kao Dan Green

3 What Are Dictionaries? Similar to lists Separates the scrubs from the pros Indexed by keys that the programmer sets instead of numbers

4 Syntax #examplecode.exe loading….. #important thing to note, use {} instead of [] charizard = {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’} league = {'name': 'Zed', 'scaling': ‘AD’} print(charizard[‘type’]) print(league[‘scaling’]) >>>fire >>>AD

5

6

7 Adding Parts to a Dictionary You can easily add parts to a list by using the following code charizard = {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’} charizard[‘weakness’] = ‘water’ print(charizard) >>>{‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’, ‘weakness’: water}

8 Deleting Parts of Your List Use del to delete individual parts of your list charizard = {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’} del charizard[‘trainer’] print(charizard) >>>{‘name’: ‘Charizard’, ‘type’: ‘fire’}

9 Printing The Keys You can print just the keys of the list by using the following code charizard = {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’} print(list(charizard.keys())) >>>[‘name’, ‘type’, ‘trainer’]

10 Dictionaryception You can have dictionaries within dictionaries charizard = {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’} raichu = {‘name’: ‘Raichu’, ‘type’: ‘fire’, ‘trainer’: ‘Gary’} pokemon = {‘charizard’: charizard, ‘raichu’: raichu} print(pokemon) >>> {‘charizard’: {‘name’: ‘Charizard’, ‘type’: ‘fire’, ‘trainer’: ‘Ash’}, ‘raichu’: {‘name’: ‘Raichu’, ‘type’: ‘fire’, ‘trainer’: ‘Gary’}}

11 Recap Video

12 Programming Challenge Write a code that sorts all items alphabetically in a dictionary without using any form of the sort command.

13 Just kidding, we’re reasonable.

14 Warm-Up Challenge Write a program that has multiple sub-dictionaries within one “master dictionary” print the “master dictionary” and then print each separate part of the master dictionary on its own separate line *Pro-Tip*: use other things you have learned in this class

15 Answer dict1 = {‘example’ = 1} dict2 = {‘example’ = 2} masterdict = {‘dict1’: dict1, ‘dict2’: dict2} masterlist = [dict1, dict2] print(masterdict) for x in range(len(masterlistprint)): print(masterlistprint[x])

16 Test to Separate The Men From The Boys Create a database that stores people’s names and the towns they live in Allow the user to add/remove entries Also allow the user to show specific entries


Download ppt "Python: Dictionaries Nicola Licata Charis Kao Dan Green."

Similar presentations


Ads by Google