From b9434ca31b97e322d5fbb4cb088ac9d7a38654f6 Mon Sep 17 00:00:00 2001 From: Sam Raker Date: Tue, 4 Aug 2015 10:57:50 -0400 Subject: [PATCH 1/4] add definition of 'dictionary' --- README.rst | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/README.rst b/README.rst index 2a452a6..9cdd8a4 100644 --- a/README.rst +++ b/README.rst @@ -112,6 +112,67 @@ Set will *not* necessarily remember what order they came in, and each value_ can only appear in the set once. +.. _dict: + +Dictionary + A structure that stores relationships between pairs of + values. Like a real dictionary, they are used to 'look + up' information about something by referencing its name + or another identifier. + + :: + + {'Mouser': 'Tabby', + 'Whiskers': 'Maine Coon'} + + The *keys* in this dictionary are ```'Mouser'``` and + ```'Whiskers'```. The *values* are ```'Tabby'``` and + ```'Maine Coon'```. Dictionaries are used to 'look + up' values by their keys: + + :: + + cats = {'Mouser': 'Tabby', + 'Whiskers': 'Maine Coon'} + cats['Mouser'] # Evaluates to 'Tabby' + + Keys and values can be added or changed: + + :: + + cats = {'Mouser', 'Tabby', + 'Whiskers': 'Maine Coon'} + cats['Fafnir'] = 'Sphinx' + cats['Mouser'] = 'Persian' + + # equivalent to + cats = {'Mouser': 'Persian', + 'Whiskers': 'Maine Coon', + 'Fafnir': 'Sphinx'} + + + The word 'dictionary' is frequently shortened + to 'dict'. ```dict``` can also be used to create + dictionaries in Python + + :: + + cats = dict(Mouser='Tabby', Whiskers='Maine Coon', + Fafnir='Sphinx') + + # equivalent to + cats = {'Mouser': 'Tabby', 'Whiskers': 'Maine Coon', + 'Fafnir': 'Sphinx'} + + Note that when making a dictionary with ```dict```, you use + ```=``` instead of ```:``` to separate keys and values, and + **do not** put quotes around keys. + + + Dictionaries are also known as 'hash tables', 'hash maps', + or just 'maps'. + + Functions --------- From 0cb6083b80e64d50983db7fae66ca748b55e7d1e Mon Sep 17 00:00:00 2001 From: Sam Raker Date: Tue, 4 Aug 2015 11:00:48 -0400 Subject: [PATCH 2/4] formatting --- README.rst | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index 9cdd8a4..fdbbb82 100644 --- a/README.rst +++ b/README.rst @@ -112,8 +112,6 @@ Set will *not* necessarily remember what order they came in, and each value_ can only appear in the set once. -.. _dict: - Dictionary A structure that stores relationships between pairs of values. Like a real dictionary, they are used to 'look @@ -125,10 +123,10 @@ Dictionary {'Mouser': 'Tabby', 'Whiskers': 'Maine Coon'} - The *keys* in this dictionary are ```'Mouser'``` and - ```'Whiskers'```. The *values* are ```'Tabby'``` and - ```'Maine Coon'```. Dictionaries are used to 'look - up' values by their keys: + The *keys* in this dictionary are ```'Mouser'``` and + ```'Whiskers'```. The *values* are ```'Tabby'``` and + ```'Maine Coon'```. Dictionaries are used to 'look + up' values by their keys: :: From aa8550df21e00d3a56edf1288281c7961b04c336 Mon Sep 17 00:00:00 2001 From: Sam Raker Date: Tue, 4 Aug 2015 11:02:59 -0400 Subject: [PATCH 3/4] another formatting fix --- README.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.rst b/README.rst index fdbbb82..2ccb383 100644 --- a/README.rst +++ b/README.rst @@ -123,9 +123,9 @@ Dictionary {'Mouser': 'Tabby', 'Whiskers': 'Maine Coon'} - The *keys* in this dictionary are ```'Mouser'``` and - ```'Whiskers'```. The *values* are ```'Tabby'``` and - ```'Maine Coon'```. Dictionaries are used to 'look + The *keys* in this dictionary are ``'Mouser'`` and + ``'Whiskers'``. The *values* are ``'Tabby'`` and + ``'Maine Coon'``. Dictionaries are used to 'look up' values by their keys: :: @@ -150,7 +150,7 @@ Dictionary The word 'dictionary' is frequently shortened - to 'dict'. ```dict``` can also be used to create + to 'dict'. ``dict`` can also be used to create dictionaries in Python :: @@ -162,8 +162,8 @@ Dictionary cats = {'Mouser': 'Tabby', 'Whiskers': 'Maine Coon', 'Fafnir': 'Sphinx'} - Note that when making a dictionary with ```dict```, you use - ```=``` instead of ```:``` to separate keys and values, and + Note that when making a dictionary with ``dict``, you use + ``=`` instead of ``:`` to separate keys and values, and **do not** put quotes around keys. From 62a9c40876987a1d23ab0b4f3b3a7993aff51817 Mon Sep 17 00:00:00 2001 From: Sam Raker Date: Tue, 4 Aug 2015 11:05:22 -0400 Subject: [PATCH 4/4] add more information about dictionary literal construction --- README.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 2ccb383..eff1d22 100644 --- a/README.rst +++ b/README.rst @@ -116,7 +116,9 @@ Dictionary A structure that stores relationships between pairs of values. Like a real dictionary, they are used to 'look up' information about something by referencing its name - or another identifier. + or another identifier. Dictionaries can be created by + listing keys and values separated by colons (``:``), + with everything wrapped in curly braces (``{``, ``}``): ::