Dictionaries (Python Basics 5)

Introduction

  • dictionaries are super powerful
  • they can seem a little weird
  • they more or less make up their own namespace
  • object-oriented programming in Python is closely related to dictionaries
    • each object has its own built-in dictionary

What is a dictionary?

  • a set of key/value pairs
  • you use the keys to look up the values like looking-up a word in the dictionary to find its definition (key=word, value=definition)
    • the value is what you find when you look-up the key in the dictionary
  • example:
    • mydict = {'a':'hello', 'b':'ryan'}
      • what is mydict['a']?

Keys

  • keys can be almost anything
  • strings and integers make good choices, but you can also use instances of objects

Values

  • values can be pretty much anything

Example File

dictionaries.py

(from python_basics_1 repository)