πŸ“– Dictionaries

Learning Targets

Do Now

In repl:

Run the following code:

my_dictionary = {
'cat': 'a domestic feline',
'dog': 'a domestic canine',
'chair': 'furniture piece for sitting',
'car': 'automobile'
}
print(my_dictionary)
print(my_dictionary['dog'])
print(my_dictionary.get('dog'))
print('cat' in my_dictionary)
print('monkey' in my_dictionary)

In comments: 1. What prints?
2. What type1 is my_dictionary? 3. Add a line of code that will print the definition of chair, then run the code again. 4. What happens if you use my_dictionary[β€˜kittens’]? What do you think that error means?


✍ Dictionary = { } ← note curly brackets

Like a list, but has a key instead of an index. Dictionaries pair the key with a value.

password = {'SK':12345}

The key & value can be any string or number.

Dictionaries look like:

passwords = {'SK': 12345, 'Lentino': 67890, 'Perez': 54321}
print(passwords['SK'])

Dictionaries are great for things like address books (pairing a name with a phone number), login pages (pairing an e-mail address with a username), etc.

Dictionary Lab

In repl: Dictionary_Lab

1⃣ Create a social network profile for yourself using a dictionary. I’ve suggested some keys, but you should feel free to add your own.

user_profile = {
	# name
	# school
	# birthday
	# email
	# username(s)
	# where are you from?
	# number of siblings
	# skills
	# about you
	# favorite quote
	# favorite movie
	# favorite musician
	# favorite TV show
	# favorite book
}

2⃣ Test your profile by printing the value for each key.

3⃣ Create a function that lets someone look through your profile. When called, it should ask something like β€œwhat would you like to know about me?” and they can respond with an aspect like email and get back your email. If the key is not in your profile, let them know that they can’t have that information.

4⃣ Add a while loop to your function that lets someone keep searching until they type β€œexit.”

#️ Remember: I only grade commented code.

  1. Hint: try print(type(my_dictionary))