mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
started "native datatypes" chapter with text for intro, booleans, and None
This commit is contained in:
@@ -812,194 +812,6 @@ them into a larger program.<table id="tip.mac.runasmain" class="tip" border="0"
|
||||
</ul>
|
||||
<div class="chapter">
|
||||
<h2 id="datatypes">Chapter 3. Native Datatypes</h2>
|
||||
<p>You'll get back to your first Python program in just a minute. But first, a short digression is in order, because you need to know about dictionaries, tuples,
|
||||
and lists (oh my!). If you're a Perl hacker, you can probably skim the bits about dictionaries and lists, but you should still pay attention to tuples.
|
||||
<h2 id="odbchelper.dict">3.1. Introducing Dictionaries</h2>
|
||||
<p>One of Python's built-in datatypes is the dictionary, which defines one-to-one relationships between keys and values.<table id="compare.dict.perl" class="note" border="0" summary="">
|
||||
<tr>
|
||||
<td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" align="left" valign="top" width="99%">A dictionary in Python is like a hash in Perl. In Perl, variables that store hashes always start with a <code>%</code> character. In Python, variables can be named anything, and Python keeps track of the datatype internally.
|
||||
</td>
|
||||
</tr>
|
||||
</table><table id="compare.dict.java" class="note" border="0" summary="">
|
||||
<tr>
|
||||
<td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" align="left" valign="top" width="99%">A dictionary in Python is like an instance of the <code>Hashtable</code> class in Java.
|
||||
</td>
|
||||
</tr>
|
||||
</table><table id="compare.dict.vb" class="note" border="0" summary="">
|
||||
<tr>
|
||||
<td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" align="left" valign="top" width="99%">A dictionary in Python is like an instance of the <code>Scripting.Dictionary</code> object in Visual Basic.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>3.1.1. Defining Dictionaries</h3>
|
||||
<div class="example"><h3 id="odbchelper.dict.define">Example 3.1. Defining a Dictionary</h3><pre class="screen"><samp class="prompt">>>> </samp>d = {"server":"mpilgrim", "database":"master"} <img id="odbchelper.dict.1.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'database': 'master'}
|
||||
<samp class="prompt">>>> </samp>d["server"]<img id="odbchelper.dict.1.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12">
|
||||
'mpilgrim'
|
||||
<samp class="prompt">>>> </samp>d["database"] <img id="odbchelper.dict.1.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12">
|
||||
'master'
|
||||
<samp class="prompt">>>> </samp>d["mpilgrim"] <img id="odbchelper.dict.1.4" src="images/callouts/4.png" alt="4" border="0" width="12" height="12">
|
||||
<samp class="traceback">Traceback (innermost last):
|
||||
File "<interactive input>", line 1, in ?
|
||||
KeyError: mpilgrim</span></pre><div class="calloutlist">
|
||||
<table border="0" summary="Callout list">
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.1.1"><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">First, you create a new dictionary with two elements and assign it to the variable <var>d</var>. Each element is a key-value pair, and the whole set of elements is enclosed in curly braces.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.1.2"><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left"><code>'server'</code> is a key, and its associated value, referenced by <code>d["server"]</code>, is <code>'mpilgrim'</code>.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.1.3"><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left"><code>'database'</code> is a key, and its associated value, referenced by <code>d["database"]</code>, is <code>'master'</code>.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.1.4"><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">You can get values by key, but you can't get keys by value. So <code>d["server"]</code> is <code>'mpilgrim'</code>, but <code>d["mpilgrim"]</code> raises an exception, because <code>'mpilgrim'</code> is not a key.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>3.1.2. Modifying Dictionaries</h3>
|
||||
<div class="example"><h3 id="odbchelper.dict.modify">Example 3.2. Modifying a Dictionary</h3><pre class="screen"><samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'database': 'master'}
|
||||
<samp class="prompt">>>> </samp>d["database"] = "pubs" <img id="odbchelper.dict.2.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'database': 'pubs'}
|
||||
<samp class="prompt">>>> </samp>d["uid"] = "sa" <img id="odbchelper.dict.2.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}</pre><div class="calloutlist">
|
||||
<table border="0" summary="Callout list">
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.2.1"><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">You can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.2.2"><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">You can add new key-value pairs at any time. This syntax is identical to modifying existing values. (Yes, this will annoy
|
||||
you someday when you think you are adding new values but are actually just modifying the same value over and over because
|
||||
your key isn't changing the way you think it is.)
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>Note that the new element (key <code>'uid'</code>, value <code>'sa'</code>) appears to be in the middle. In fact, it was just a coincidence that the elements appeared to be in order in the first
|
||||
example; it is just as much a coincidence that they appear to be out of order now.<table id="tip.dictorder" class="note" border="0" summary="">
|
||||
<tr>
|
||||
<td rowspan="2" align="center" valign="top" width="1%"><img src="images/note.png" alt="Note" title="" width="24" height="24"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="2" align="left" valign="top" width="99%">Dictionaries have no concept of order among elements. It is incorrect to say that the elements are “out of order”; they are simply unordered. This is an important distinction that will annoy you when you want to access the elements of
|
||||
a dictionary in a specific, repeatable order (like alphabetical order by key). There are ways of doing this, but they're
|
||||
not built into the dictionary.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>When working with dictionaries, you need to be aware that dictionary keys are case-sensitive.
|
||||
<div class="example"><h3 id="odbchelper.dict.case">Example 3.3. Dictionary Keys Are Case-Sensitive</h3><pre class="screen">
|
||||
<samp class="prompt">>>> </samp>d = {}
|
||||
<samp class="prompt">>>> </samp>d["key"] = "value"
|
||||
<samp class="prompt">>>> </samp>d["key"] = "other value" <img id="odbchelper.dict.5.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'key': 'other value'}
|
||||
<samp class="prompt">>>> </samp>d["Key"] = "third value" <img id="odbchelper.dict.5.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'Key': 'third value', 'key': 'other value'}
|
||||
</pre><div class="calloutlist">
|
||||
<table border="0" summary="Callout list">
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.5.1"><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">Assigning a value to an existing dictionary key simply replaces the old value with a new one.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.5.2"><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">This is not assigning a value to an existing dictionary key, because strings in Python are case-sensitive, so <code>'key'</code> is not the same as <code>'Key'</code>. This creates a new key/value pair in the dictionary; it may look similar to you, but as far as Python is concerned, it's completely different.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="example"><h3 id="odbchelper.dictionarytypes">Example 3.4. Mixing Datatypes in a Dictionary</h3><pre class="screen"><samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'pubs'}
|
||||
<samp class="prompt">>>> </samp>d["retrycount"] = 3 <img id="odbchelper.dict.3.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
|
||||
<samp class="prompt">>>> </samp>d[42] = "douglas" <img id="odbchelper.dict.3.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
<samp class="computeroutput">{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
|
||||
42: 'douglas', 'retrycount': 3}</span></pre><div class="calloutlist">
|
||||
<table border="0" summary="Callout list">
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.3.1"><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">Dictionaries aren't just for strings. Dictionary values can be any datatype, including strings, integers, objects, or even
|
||||
other dictionaries. And within a single dictionary, the values don't all need to be the same type; you can mix and match
|
||||
as needed.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.3.2"><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left">Dictionary keys are more restricted, but they can be strings, integers, and a few other types. You can also mix and match
|
||||
key datatypes within a dictionary.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>3.1.3. Deleting Items From Dictionaries</h3>
|
||||
<div class="example"><h3 id="odbchelper.dict.del">Example 3.5. Deleting Items from a Dictionary</h3><pre class="screen"><samp class="prompt">>>> </samp>d
|
||||
<samp class="computeroutput">{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master',
|
||||
42: 'douglas', 'retrycount': 3}</samp>
|
||||
<samp class="prompt">>>> </samp>del d[42] <img id="odbchelper.dict.4.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{'server': 'mpilgrim', 'uid': 'sa', 'database': 'master', 'retrycount': 3}
|
||||
<samp class="prompt">>>> </samp>d.clear() <img id="odbchelper.dict.4.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12">
|
||||
<samp class="prompt">>>> </samp>d
|
||||
{}</pre><div class="calloutlist">
|
||||
<table border="0" summary="Callout list">
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.4.1"><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left"><code>del</code> lets you delete individual items from a dictionary by key.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td width="12" valign="top" align="left"><a href="#odbchelper.dict.4.2"><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
|
||||
</td>
|
||||
<td valign="top" align="left"><code>clear</code> deletes all items from a dictionary. Note that the set of empty curly braces signifies a dictionary without any items.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="itemizedlist">
|
||||
<h3>Further Reading on Dictionaries</h3>
|
||||
<ul>
|
||||
<li><a href="http://www.ibiblio.org/obp/thinkCSpy/" title="Python book for computer science majors"><i class="citetitle">How to Think Like a Computer Scientist</i></a> teaches about dictionaries and shows how to <a href="http://www.ibiblio.org/obp/thinkCSpy/chap10.htm">use dictionaries to model sparse matrices</a>.
|
||||
|
||||
<li><a href="http://www.faqts.com/knowledge-base/index.phtml/fid/199/">Python Knowledge Base</a> has a lot of <a href="http://www.faqts.com/knowledge-base/index.phtml/fid/541">example code using dictionaries</a>.
|
||||
|
||||
<li><a href="http://www.activestate.com/ASPN/Python/Cookbook/" title="growing archive of annotated code samples">Python Cookbook</a> discusses <a href="http://www.activestate.com/ASPN/Python/Cookbook/Recipe/52306">how to sort the values of a dictionary by key</a>.
|
||||
|
||||
<li><a href="http://www.python.org/doc/current/lib/"><i class="citetitle">Python Library Reference</i></a> summarizes <a href="http://www.python.org/doc/current/lib/typesmapping.html">all the dictionary methods</a>.
|
||||
|
||||
</ul>
|
||||
<h2 id="odbchelper.list">3.2. Introducing Lists</h2>
|
||||
<p>Lists are Python's workhorse datatype. If your only experience with lists is arrays in Visual Basic or (God forbid) the datastore in Powerbuilder, brace yourself for Python lists.<table id="compare.list.perl" class="note" border="0" summary="">
|
||||
<tr>
|
||||
|
||||
Reference in New Issue
Block a user