mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 15:00:18 +00:00
started "native datatypes" chapter with text for intro, booleans, and None
This commit is contained in:
@@ -3,8 +3,8 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Case study: porting chardet to Python 3 - Dive into Python 3</title>
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="dip3.css">
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
@@ -14,7 +14,7 @@ body{counter-reset:h1 20}
|
||||
<body>
|
||||
<p class="skip"><a href="#divingin">skip to main content</a>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"> <input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p class="nav">You are here: <a href="/">Dive Into Python 3</a> <span>‣</span>
|
||||
<p class="nav">You are here: <a href="/">Home</a> <span>‣</span> <a href="table-of-contents.html">Dive Into Python 3</a> <span>‣</span>
|
||||
<h1>Case study: porting <code>chardet</code> to Python 3</h1>
|
||||
<blockquote class="q">
|
||||
<p><span>❝</span> Words, words. They’re all we have to go on. <span>❞</span><br>— <cite>Rosencrantz and Guildenstern are Dead</cite>
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -39,6 +39,7 @@ table.simple th{font-family:inherit !important}
|
||||
.fr{width:100%;border:1px dotted}
|
||||
.fr h4{margin-top:-1.2em;margin-left:-1em;width:8.5em;border:1px dotted;padding: 3px 3px 3px 13px;background:#fff;color:inherit;position:relative}
|
||||
.hover{background:#eee;color:inherit;cursor:default}
|
||||
i.baa{font-family:Baskerville,Constantia,Palatino,'Palatino Linotype','URW Palladio L','URW Bookman L',serif}/* "baa" = "best available ampersand" http://simplebits.com/notebook/2008/08/14/ampersands.html */
|
||||
body{counter-reset:h1}
|
||||
h1:before{content:"Chapter " counter(h1) ". "}
|
||||
h1{counter-reset:h2}
|
||||
|
||||
+13
-303
@@ -7,314 +7,24 @@
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
body{counter-reset:h1 -1}
|
||||
h1:before{counter-increment:h1}
|
||||
h2{margin-left:1.75em}
|
||||
h3{margin-left:3.5em}
|
||||
.appendix h1:before{content:""}
|
||||
p.first{clear:both;margin-top:0;padding-top:1.75em}
|
||||
ul{list-style:none}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"><input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p style="clear:both;margin-top:0;padding-top:1.75em"><cite>Dive Into Python 3</cite> will cover Python 3 and its differences from Python 2. Compared to the original <cite><a href="http://diveintopython.org/">Dive Into Python</a></cite>, it will be about 50% revised and 50% new material. I will publish drafts online as I go. The final version will be published on paper by Apress. The book will remain online under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a> license.
|
||||
<p>There is a <a href="http://hg.diveintopython3.org/">changelog</a>, a <a rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">feed</a>, and <a href="http://www.reddit.com/search?q=%22Dive+Into+Python+3%22">discussion on Reddit</a>. The final version will be downloadable as HTML and PDF. During development, the only way to download it is to clone the Mercurial repository:
|
||||
<p class="first"><cite>Dive Into Python 3</cite> will cover Python 3 and its differences from Python 2. Compared to the original <cite><a href="http://diveintopython.org/">Dive Into Python</a></cite>, it will be about 50% revised and 50% new material. I will publish drafts online as I go. The final version will be published on paper by Apress. The book will remain online under the <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a> license.
|
||||
<p>Here’s what I’ve written so far:</p>
|
||||
<ul>
|
||||
<li><a href="table-of-contents.html">Table of contents</a> (<strong>not finalized</strong>)
|
||||
<li><a href="your-first-python-program.html">Chapter 1. Your first Python program</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html">Chapter 20. Case study: porting <code>chardet</code> to Python 3</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html">Appendix A. Porting code to Python 3 with <code>2to3</code></a>
|
||||
</ul>
|
||||
|
||||
<p>There is a <a href="http://hg.diveintopython3.org/">changelog</a>, a <a rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">feed</a>, and <a href="http://www.reddit.com/search?q=%22Dive+Into+Python+3%22&sort=new">discussion on Reddit</a>. During development, you can download the book by cloning the Mercurial repository:
|
||||
<pre><samp class="prompt">you@localhost:~$ </samp><kbd>hg clone http://hg.diveintopython3.org/ diveintopython3</kbd></pre>
|
||||
<p>Below is the draft table of contents. It is <b>not finalized</b>. Only a few chapters have been written so far. The rest is just stubs and random notes to myself.
|
||||
<h1>Installing Python</h1>
|
||||
<h2>Python on Windows</h2>
|
||||
<h2>Python on Mac OS X</h2>
|
||||
<h2>Python on Linux</h2>
|
||||
<h2>Python from source</h2>
|
||||
<h2>The interactive shell</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1><a href="your-first-python-program.html">Your first Python program</a></h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>Declaring functions</h2>
|
||||
<h3>How Python's datatypes compare to other programming languages</h3>
|
||||
<h2>Writing readable code</h2>
|
||||
<h3>Why bother?</h3>
|
||||
<h3>Docstrings</h3>
|
||||
<h3>Function annotations</h3>
|
||||
<h3>Style conventions</h3>
|
||||
<h2>Everything is an object</h2>
|
||||
<h3>The import search path</h3>
|
||||
<h3>What's an object?</h3>
|
||||
<h2>Indenting code</h2>
|
||||
<h2>Testing modules</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>Native Python datatypes</h1>
|
||||
<!-- "Lists and tuples and sets, oh my!" -->
|
||||
<h2>Lists</h2>
|
||||
<h3>Differences from Python 2</h3>
|
||||
<h3>Creating new a list</h3>
|
||||
<h3>Modifying a list</h3>
|
||||
<h3>Searching a list</h3>
|
||||
<h3>Deleting elements from a list</h3>
|
||||
<h3>List operators</h3>
|
||||
<h3>Looping through a list (list comprehensions)</h3>
|
||||
<h3>Tuples</h3>
|
||||
<h2>Dictionaries</h2>
|
||||
<h3>Differences from Python 2</h3>
|
||||
<h3>Creating a new dictionary</h3>
|
||||
<h3>Modifying a dictionary</h3>
|
||||
<h3>Deleting items from a dictionary</h3>
|
||||
<h3>Looping through a dictionary (dictionary comprehensions)</h3>
|
||||
<h3>Dictionary views</h3>
|
||||
<h2>Sets</h2>
|
||||
<h3>Differences from Python 2</h3>
|
||||
<h3>Creating a new set</h3>
|
||||
<h3>Modifying a set</h3>
|
||||
<h3>Deleting elements from a set</h3>
|
||||
<h3>Common set operations: union, intersection, and difference</h3>
|
||||
<h3>Frozen sets</h3>
|
||||
<h2>Numbers</h2>
|
||||
<h3>Differences from Python 2</h3>
|
||||
<h3>Integers</h3>
|
||||
<h3>Long integers</h3>
|
||||
<h3>Floating point numbers</h3>
|
||||
<h3>Complex numbers</h3>
|
||||
<h3>Common numerical operations</h3>
|
||||
<h1></h1>
|
||||
<!-- "I read part of it all the way through." -->
|
||||
<h2>Iterators</h2>
|
||||
<h2>Generators</h2>
|
||||
<h2>Views</h2>
|
||||
<h2>...</h2>
|
||||
<h1>Strings</h1>
|
||||
<h2>There ain't no such thing as "plain text"</h2>
|
||||
<h3>A brief history of character encoding</h3>
|
||||
<h3>What's a character?</h3>
|
||||
<h3>How strings are stored in memory</h3>
|
||||
<h3>Converting between different character encodings</h3>
|
||||
<h2>Differences from Python 2</h2>
|
||||
<h2>Formatting strings</h2>
|
||||
<h2>What's my string?</h2>
|
||||
<h2>Lists and strings</h2>
|
||||
<h2>Historical note on the string module</h2>
|
||||
<h2>Byte streams</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>The power of introspection</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>Using optional and named arguments</h2>
|
||||
<h3>Keyword-only arguments</h3>
|
||||
<h2>Using type, str, dir, and other built-in functions</h2>
|
||||
<h3>The type function</h3>
|
||||
<h3>The str function</h3>
|
||||
<h3>Built-in functions</h3>
|
||||
<h2>Getting object references with getattr</h2>
|
||||
<h3>getattr with modules</h3>
|
||||
<h3>getattr as a dispatcher</h3>
|
||||
<h2>Filtering lists</h2>
|
||||
<h2>The peculiar nature of and and or</h2>
|
||||
<h3>Using the and-or trick</h3>
|
||||
<h2>Using lambda functions</h2>
|
||||
<h3>Real-world lambda functions</h3>
|
||||
<h2>Putting it all together</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>Objects and object-orientation</h1>
|
||||
<h2>...major changes afoot...</h2>
|
||||
<h2>...stuff about decorators...</h2>
|
||||
<h2>...stuff about importing modules...</h2>
|
||||
<h3>...mention why "from module import *" is only allowed at module level</h3>
|
||||
<h1>Exceptions</h1>
|
||||
<h2>...</h2>
|
||||
<h1>Files</h1>
|
||||
<h2>File objects</h2>
|
||||
<h2>Reading files</h2>
|
||||
<h2>Close your files... or don't</h2>
|
||||
<h2>Handling I/O errors</h2>
|
||||
<h2>Writing to files</h2>
|
||||
<h1>Regular expressions</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>Case study: street addresses</h2>
|
||||
<h2>Case study: Roman numerals</h2>
|
||||
<h3>Checking for thousands</h3>
|
||||
<h3>Checking for hundreds</h3>
|
||||
<h2>Using the {n,m} syntax</h2>
|
||||
<h3>Checking for tens and ones</h3>
|
||||
<h2>Verbose regular expressions</h2>
|
||||
<h2>Case study: parsing phone numbers</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>HTML processing</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>html5lib</h2>
|
||||
<h3>Installing html5lib</h3>
|
||||
<h3>Using html5lib</h3>
|
||||
<h2>Extracting data from HTML documents</h2>
|
||||
<h2>Building HTML documents</h2>
|
||||
<h2>Putting it all together</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>XML Processing</h1>
|
||||
<h2>...major changes afoot...</h2>
|
||||
<h1>HTTP web services</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>How not to fetch data over HTTP</h2>
|
||||
<h2>Features of HTTP</h2>
|
||||
<h3>User-Agent</h3>
|
||||
<h3>Redirects</h3>
|
||||
<h3>Last-Modified/If-Modified-Since</h3>
|
||||
<h3>ETag-If-None-Match</h3>
|
||||
<h3>Compression</h3>
|
||||
<h2>Differences from Python 2</h2>
|
||||
<h2>httplib2 (note: needs port)</h2>
|
||||
<h3>Installing httplib2</h3>
|
||||
<h3>Why httplib2 is better than http.client</h3>
|
||||
<h2>Debugging HTTP web services</h2>
|
||||
<h2>Setting the User-Agent</h2>
|
||||
<h2>Handling Last-Modified and ETag</h2>
|
||||
<h2>Handling redirects</h2>
|
||||
<h2>Handling compressed data</h2>
|
||||
<h2>Putting it all together</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>Unit testing</h1>
|
||||
<h2>Introduction to Roman numerals</h2>
|
||||
<h2>Diving in</h2>
|
||||
<h2>Introducing romantest.py</h2>
|
||||
<h2>Testing for success</h2>
|
||||
<h2>Testing for failure</h2>
|
||||
<h2>Testing for sanity</h2>
|
||||
<h1>Test-first programming</h1>
|
||||
<h2>roman.py, stage 1</h2>
|
||||
<h2>roman.py, stage 2</h2>
|
||||
<h2>roman.py, stage 3</h2>
|
||||
<h2>roman.py, stage 4</h2>
|
||||
<h2>roman.py, stage 5</h2>
|
||||
<h1>Refactoring your code</h1>
|
||||
<h2>Handling bugs</h2>
|
||||
<h2>Handling changing requirements</h2>
|
||||
<h2>The art of refactoring</h2>
|
||||
<h2>Postscript</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>Dynamic functions</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>plural.py, stage 1</h2>
|
||||
<h2>plural.py, stage 2</h2>
|
||||
<h2>plural.py, stage 3</h2>
|
||||
<h2>plural.py, stage 4</h2>
|
||||
<h2>plural.py, stage 5</h2>
|
||||
<h2>plural.py, stage 6</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1>Metaclasses</h1>
|
||||
<h2>...once I figure out WTF metaclasses are...</h2>
|
||||
<h1>Performance tuning</h1>
|
||||
<h2>Diving in</h2>
|
||||
<h2>Using the timeit module</h2>
|
||||
<h2>Optimizing regular expressions</h2>
|
||||
<h2>Optimizing dictionary lookups</h2>
|
||||
<h2>Optimizing list operations</h2>
|
||||
<h2>Optimizing string manipulation</h2>
|
||||
<h2>Summary</h2>
|
||||
<h1><a href="case-study-porting-chardet-to-python-3.html">Case study: porting <code>chardet</code> to Python 3</a></h1>
|
||||
<h2><a href="#faq">Introducing <code class="filename">chardet</code>: a mini-FAQ</a></h2>
|
||||
<h3><a href="#faq.what">What is character encoding auto-detection?</a></h3>
|
||||
<h3><a href="#faq.impossible">Isn't that impossible?</a></h3>
|
||||
<h3><a href="#faq.who">Who wrote this detection algorithm?</a></h3>
|
||||
<h3><a href="#faq.yippie">Yippie! Screw the standards, I'll just auto-detect everything!</a></h3>
|
||||
<h3><a href="#faq.why">Why bother with auto-detection if it's slow, inaccurate, and non-standard?</a></h3>
|
||||
<h2><a href="#divingin">Diving in</a></h2>
|
||||
<h3><a href="#how.bom"><code>UTF-n</code> with a <abbr title="Byte Order Mark">BOM</abbr></a></h3>
|
||||
<h3><a href="#how.esc">Escaped encodings</a></h3>
|
||||
<h3><a href="#how.mb">Multi-byte encodings</a></h3>
|
||||
<h3><a href="#how.sb">Single-byte encodings</a></h3>
|
||||
<h3><a href="#how.windows1252"><code>windows-1252</code></a></h3>
|
||||
<h2><a href="#running2to3">Running <code class="filename">2to3</code></a></h2>
|
||||
<h2><a href="#manual">Fixing what <code class="filename">2to3</code> can't</a></h2>
|
||||
<h3><a href="#falseisinvalidsyntax"><code>False</code> is invalid syntax</a></h3>
|
||||
<h3><a href="#nomodulenamedconstants">No module named <code class="filename">constants</code></a></h3>
|
||||
<h3><a href="#namefileisnotdefined">Name '<var>file</var>' is not defined</a></h3>
|
||||
<h3><a href="#cantuseastringpattern">Can't use a string pattern on a bytes-like object</a></h3>
|
||||
<h3><a href="#cantconvertbytesobject">Can't convert '<code>bytes</code>' object to <code>str</code> implicitly</a></h3>
|
||||
<h1>Packaging Python libraries</h1>
|
||||
<!-- http://pypi.python.org/pypi -->
|
||||
<h2>A brief history of packaging (and why it's harder than you think)</h2>
|
||||
<h2>setuptools</h2>
|
||||
<h2>distutils</h2>
|
||||
<h2>Eggs</h2>
|
||||
<h2>pip</h2>
|
||||
<h2>Platform-specific packaging</h2>
|
||||
<h3>Packaging by Linux distributions</h3>
|
||||
<h3>Py2exe</h3>
|
||||
<h1>Creating graphics with the Python Imaging Library</h1>
|
||||
<h2>...<a href="http://www.reddit.com/r/Python/comments/7sj39/dive_into_python_3/c07b3cq">will likely get ported in time</a>...</h2>
|
||||
<h1>Where to go from here</h1>
|
||||
<p>Tentative because most of these have not been ported to Python 3 yet.
|
||||
<h2>WSGI</h2>
|
||||
<h2>Django</h2>
|
||||
<h2>Pylons</h2>
|
||||
<h2>TurboGears</h2>
|
||||
<h2>AppEngine</h2>
|
||||
<h2>IronPython</h2>
|
||||
<h2>Jython</h2>
|
||||
<h2>PyPy</h2>
|
||||
<h2>Stackless Python</h2>
|
||||
<h1><del>Scripts and streams</del></h1>
|
||||
<h2>...will be folded into other chapters...</h2>
|
||||
<h1><del>Functional programming</del></h1>
|
||||
<h2>...bits and pieces will be folded into other chapters...</h2>
|
||||
<h1><del>SOAP web services</del></h1>
|
||||
<h2>...no one will miss you...</h2>
|
||||
<div class="appendix">
|
||||
<h1 class="appendix">Appendix A. <a href="porting-code-to-python-3-with-2to3.html">Porting code to Python 3 with <code class="filename">2to3</code></a></h1>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#divingin">Diving in</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#print"><code>print</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicodeliteral">Unicode string literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicode"><code>unicode()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#long"><code>long</code> data type</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#ne"><> comparison</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#has_key"><code>has_key()</code> dictionary method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dict">Dictionary methods that return lists</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#imports">Modules that have been renamed or reorganized</a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#http"><code>http</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#urllib"><code>urllib</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dbm"><code>dbm</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xmlrpc"><code>xmlrpc</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#othermodules">Other modules</a>
|
||||
</ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#import">Relative imports within a package</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#next"><code>next()</code> iterator method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#filter"><code>filter()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#map"><code>map()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#reduce"><code>reduce()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#apply"><code>apply()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#intern"><code>intern()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#exec"><code>exec</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#execfile"><code>execfile</code> statement</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#repr"><code>repr</code> literals (backticks)</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#exceptions">Exceptions</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#except"><code>try...except</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#raise"><code>raise</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#throw"><code>throw</code> method on generators</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xrange"><code>xrange()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#raw_input"><code>raw_input()</code> and <code>input()</code> global functions</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#funcattrs"><code>func_*</code> function attributes</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xreadlines"><code>xreadlines()</code> I/O method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#tuple_params"><code>lambda</code> functions with multiple parameters</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#methodattrs">Special method attributes</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#nonzero"><code>__nonzero__</code> special class attribute</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#numliterals">Octal literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#renames"><code>sys.maxint</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#callable"><code>callable()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#zip"><code>zip()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#standarderror"><code>StandardError()</code> exception</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#types"><code>types</code> module constants</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#isinstance"><code>isinstance()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#basestring"><code>basestring</code> datatype</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#itertools"><code>itertools</code> module</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#sys_exc"><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#paren">List comprehensions over tuples</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#getcwdu"><code>os.getcwdu()</code> function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#metaclass">Metaclasses</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#nitpick">Matters of style</a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#set_literal"><code>set()</code> literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#buffer"><code>buffer()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#wscomma">Whitespace around commas</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#idioms">Common idioms</a>
|
||||
</ol>
|
||||
</ol>
|
||||
</div>
|
||||
<p>The final version will be downloadable as HTML and PDF.
|
||||
<p class="c">This site is optimized for Lynx just because fuck you.<br>I’m told it also looks good in graphical browsers.
|
||||
<p class="c">© 2001-4, 2009 <span>ℳ</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
|
||||
<!--
|
||||
|
||||
@@ -0,0 +1,293 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Native datatypes - Dive into Python 3</title>
|
||||
<link rel="stylesheet" type="text/css" href="dip3.css">
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
body{counter-reset:h1 2}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<p class="skip"><a href="#divingin">skip to main content</a>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"> <input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p class="nav">You are here: <a href="/">Home</a> <span>‣</span> <a href="table-of-contents.html">Dive Into Python 3</a> <span>‣</span>
|
||||
<h1>Native datatypes</h1>
|
||||
<blockquote class="q">
|
||||
<p><span>❝</span> Wonder is the foundation of all philosophy, research its progress, ignorance its end. <span>❞</span><br>— <cite>Michel de Montaigne</cite>
|
||||
</blockquote>
|
||||
<ol>
|
||||
<li><a href="#divingin">Diving in</a>
|
||||
<li><a href="#booleans">Booleans</a>
|
||||
<li><a href="#numbers">Numbers</a>
|
||||
<ol>
|
||||
<li><a href="#integers">Integers</a>
|
||||
<li><a href="#floats">Floating point numbers</a>
|
||||
<li><a href="#fractions">Fractions</a>
|
||||
<li><a href="#complexnumbers">Complex numbers</a>
|
||||
<li><a href="#numberoperations">Common operations on numbers</a>
|
||||
<li><a href="#math">The <code>math</code> module</a>
|
||||
</ol>
|
||||
<li><a href="#lists">Lists</a>
|
||||
<li><a href="#sets">Sets</a>
|
||||
<li><a href="#dictionaries">Dictionaries
|
||||
<li><a href="#none"><code>None</code></a>
|
||||
</ol>
|
||||
<h2 id="divingin">Diving in</h2>
|
||||
<p class="fancy">A short digression is in order. Put aside <a href="your-first-python-program.html">your first Python program</a> for just a minute, and let's talk about Python's native datatypes. These types are the foundation on which you will build all your future Python programs.
|
||||
<p>Python has many native datatypes. Here are the important ones:
|
||||
<ol>
|
||||
<li><b>Booleans</b> are either <code>True</code> or <code>False</code>.
|
||||
<li><b>Numbers</b> can be integers (<code>1</code> and <code>2</code>), floats (<code>1.1</code> and <code>1.2</code>), fractions (<code>1/2</code> and <code>2/3</code>), or even complex numbers (<code><var>i</var></code>, the square root of <code>-1</code>).
|
||||
<li><b>Strings</b> are sequences of Unicode characters, <i>e.g.</i> an <abbr>HTML</abbr> document.
|
||||
<li><b>Bytes</b> and <b>byte arrays</b>, <i>e.g.</i> a <abbr>JPEG</abbr> image file.
|
||||
<li><b>Lists</b> are ordered sequences of values.
|
||||
<li><b>Sets</b> are unordered bags of values.
|
||||
<li><b>Dictionaries</b> are unordered bags of key-value pairs.
|
||||
</ol>
|
||||
<p>Of course, there are a lot more types than these seven. <a href="your-first-python-program.html#everythingisanobject">Everything is an object</a> in Python, so there are types like <i>module</i>, <i>function</i>, <i>class</i>, <i>method</i>, <i>file</i>, and even <i>compiled code</i>. You've already seen some of these: <a href="your-first-python-program.html#runningscripts">modules have names</a>, <a href="your-first-python-program.html#docstrings">functions have <code>docstrings</code></a>, <i class="baa">&</i>c. You'll learn about classes in [FIXME xref] and files in [FIXME xref].
|
||||
<p>Strings and bytes are important enough — and complicated enough — that they get their own chapter. Let's look at the others first.
|
||||
<h2 id="booleans">Booleans</h2>
|
||||
<p>Booleans are either true or false. Python has two constants, <code>True</code> and <code>False</code>, which can be used to assign boolean values directly. Expressions can also evaluate to a boolean value. In certain places (like <code>if</code> statements), Python expects an expression to evaluate to a boolean value. These places are called <i>boolean contexts</i>. You can use virtually any expression in a boolean context, and Python will try to determine its truth value. Different datatypes have different rules about which values are true or false in a boolean context. (This will make more sense once you see some concrete examples later in this chapter.)
|
||||
<p>For example, take this snippet from <a href="your-first-python-program.html#divingin"><code>humansize.py</code></a>:
|
||||
<pre><code>if size < 0:
|
||||
raise ValueError('number must be non-negative')</code></pre>
|
||||
<p><var>size</var> is an integer, <code>0</code> is an integer, and <code><</code> is a numerical operator. The result of the expression <code>size < 0</code> is always a boolean. You can test this yourself in the Python interactive shell:
|
||||
<pre class="screen">
|
||||
<samp class="prompt">>>> </samp><kbd>size = 1</kbd>
|
||||
<samp class="prompt">>>> </samp><kbd>size < 0</kbd>
|
||||
<samp>False</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>size = 0</kbd>
|
||||
<samp class="prompt">>>> </samp><kbd>size < 0</kbd>
|
||||
<samp>False</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>size = -1</kbd>
|
||||
<samp class="prompt">>>> </samp><kbd>size < 0</kbd>
|
||||
<samp>True</samp></pre>
|
||||
<h2 id="numbers">Numbers</h2>
|
||||
|
||||
<h3 id="integers">Integers</h3>
|
||||
|
||||
<h3 id="floats">Floating point numbers</h3>
|
||||
|
||||
<h3 id="fractions">Fractions</h3>
|
||||
|
||||
<h3 id="complexnumbers">Complex numbers</h3>
|
||||
|
||||
<h3 id="numberoperations">Common operations on numbers</h3>
|
||||
|
||||
<h3 id="math">The <code>math</code> module</h3>
|
||||
|
||||
<h2 id="none"><code>None</code></h2>
|
||||
<p><code>None</code> is a special constant in Python. It is a null value. <code>None</code> is not <code>False</code>; it is not <code>0</code>; it is not an empty string. Comparing <code>None</code> to anything other than <code>None</code> will always return <code>False</code>.
|
||||
<p><code>None</code> is the only null value. It has its own datatype (<code>NoneType</code>). You can assign <code>None</code> to any variable, but you can not create other <code>NoneType</code> objects. All variables whose value is <code>None</code> are equal to each other.
|
||||
<pre class="screen">
|
||||
<samp class="prompt">>>> </samp><kbd>type(None)</kbd>
|
||||
<samp><class 'NoneType'></samp>
|
||||
<samp class="prompt">>>> </samp><kbd>None == False</kbd>
|
||||
<samp>False</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>None == 0</kbd>
|
||||
<samp>False</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>None == ''</kbd>
|
||||
<samp>False</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>None == None</kbd>
|
||||
<samp>True</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>x = None</kbd>
|
||||
<samp class="prompt">>>> </samp><kbd>x == None</kbd>
|
||||
<samp>True</samp>
|
||||
<samp class="prompt">>>> </samp><kbd>y = None</kbd>
|
||||
<samp class="prompt">>>> </samp><kbd>x == y</kbd>
|
||||
<samp>True</samp>
|
||||
</pre>
|
||||
<!--
|
||||
<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>
|
||||
-->
|
||||
<p class="c">© 2001-4, 2009 <span>ℳ</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -3,8 +3,8 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Porting code to Python 3 with 2to3 - Dive into Python 3</title>
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="dip3.css">
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
@@ -16,7 +16,7 @@ h3:before{counter-increment:h3;content:"A." counter(h2) "." counter(h3) ". "}
|
||||
<body>
|
||||
<p class="skip"><a href="#divingin">skip to main content</a>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"> <input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p class="nav">You are here: <a href="/">Dive Into Python 3</a> <span>‣</span>
|
||||
<p class="nav">You are here: <a href="/">Home</a> <span>‣</span> <a href="table-of-contents.html">Dive Into Python 3</a> <span>‣</span>
|
||||
<h1>Porting code to Python 3 with <code>2to3</code></h1>
|
||||
<blockquote class="q">
|
||||
<p><span>❝</span> Life is pleasant. Death is peaceful. It’s the transition that’s troublesome. <span>❞</span><br>— Isaac Asimov (attributed)
|
||||
|
||||
@@ -0,0 +1,388 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Table of contents - Dive Into Python 3</title>
|
||||
<link rel="stylesheet" type="text/css" href="dip3.css">
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
h1:before{content:""}
|
||||
ol,ul{font-weight:bold}
|
||||
li ol{font-weight:normal}
|
||||
ul{list-style:none;margin:0;padding:0}
|
||||
ul li ol{margin:0;padding:0 0 0 2.5em}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"><input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p class="nav">You are here: <a href="/">Home</a> <span>‣</span> Dive Into Python 3 <span>‣</span>
|
||||
<h1>Table of contents</h1>
|
||||
<ol start="0">
|
||||
<li>Installing Python
|
||||
<ol>
|
||||
<li>Python on Windows
|
||||
<li>Python on Mac OS X
|
||||
<li>Python on Ubuntu Linux
|
||||
<li>Python from source
|
||||
<li>The interactive shell
|
||||
</ol>
|
||||
<li><a href="your-first-python-program.html">Your first Python program</a>
|
||||
<ol>
|
||||
<li><a href="your-first-python-program.html#divingin">Diving in</a>
|
||||
<li><a href="your-first-python-program.html#declaringfunctions">Declaring functions</a>
|
||||
<li><a href="your-first-python-program.html#readability">Writing readable code</a>
|
||||
<ol>
|
||||
<li><a href="your-first-python-program.html#docstrings">Docstrings</a>
|
||||
<li><a href="your-first-python-program.html#functionannotations">Function annotations</a>
|
||||
<li><a href="your-first-python-program.html#styleconventions">Style conventions</a>
|
||||
</ol>
|
||||
<li><a href="your-first-python-program.html#everythingisanobject">Everything is an object</a>
|
||||
<ol>
|
||||
<li><a href="your-first-python-program.html#importsearchpath">The <code>import</code> search path</a>
|
||||
<li><a href="your-first-python-program.html#whatsanobject">What's an object?</a>
|
||||
</ol>
|
||||
<li><a href="your-first-python-program.html#indentingcode">Indenting code</a>
|
||||
<li><a href="your-first-python-program.html#runningscripts">Running scripts</a>
|
||||
</ol>
|
||||
<li><a href="native-datatypes.html">Native Python datatypes</a>
|
||||
<ol>
|
||||
<li><a href="native-datatypes.html#divingin">Diving in</a>
|
||||
<li><a href="native-datatypes.html#booleans">Booleans</a>
|
||||
<li>Numbers
|
||||
<ol>
|
||||
<li>Integers
|
||||
<li>Floating point numbers
|
||||
<li>Fractions
|
||||
<li>Complex numbers
|
||||
<li>Common operations on numbers
|
||||
<li>The <code>math</code> module
|
||||
</ol>
|
||||
<li>Lists
|
||||
<ol>
|
||||
<li>Creating new a list
|
||||
<li>Modifying a list
|
||||
<li>Searching a list
|
||||
<li>Deleting elements from a list
|
||||
<li>Common operations on lists
|
||||
</ol>
|
||||
<li>Sets
|
||||
<ol>
|
||||
<li>Creating a new set
|
||||
<li>Modifying a set
|
||||
<li>Deleting elements from a set
|
||||
<li>Common operations on sets <!-- union, intersection, and difference -->
|
||||
<li>Frozen sets
|
||||
</ol>
|
||||
<li>Dictionaries
|
||||
<ol>
|
||||
<li>Creating a new dictionary
|
||||
<li>Modifying a dictionary
|
||||
<li>Deleting items from a dictionary
|
||||
<li>Common operations on dictionaries
|
||||
</ol>
|
||||
<li><a href="native-datatypes.html#none"><code>None</code></a>
|
||||
</ol>
|
||||
<li>Strings
|
||||
<ol>
|
||||
<li>There ain't no such thing as "plain text"
|
||||
<ol>
|
||||
<li>A brief history of character encoding
|
||||
<li>What's a character?
|
||||
<li>How strings are stored in memory
|
||||
<li>Converting between different character encodings
|
||||
</ol>
|
||||
<li>Formatting strings
|
||||
<li>What's my string?
|
||||
<li>Lists and strings
|
||||
<li>Historical note on the string module
|
||||
<li>Byte streams
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>The power of introspection
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>Using optional and named arguments
|
||||
<ol>
|
||||
<li>Keyword-only arguments
|
||||
</ol>
|
||||
<li>Using type, str, dir, and other built-in functions
|
||||
<ol>
|
||||
<li>The type function
|
||||
<li>The str function
|
||||
<li>Built-in functions
|
||||
</ol>
|
||||
<li>Getting object references with getattr
|
||||
<ol>
|
||||
<li>getattr with modules
|
||||
<li>getattr as a dispatcher
|
||||
</ol>
|
||||
<li>Filtering lists
|
||||
<li>Using lambda functions
|
||||
<li>Putting it all together
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>Objects and object-orientation
|
||||
<ol>
|
||||
<li>...major changes afoot...
|
||||
<li>...stuff about decorators...
|
||||
<li>...stuff about importing modules...
|
||||
<ol>
|
||||
<li>...mention why "from module import *" is only allowed at module level
|
||||
</ol>
|
||||
</ol>
|
||||
<li>Exceptions
|
||||
<ol>
|
||||
<li>...
|
||||
</ol>
|
||||
<li>Files
|
||||
<ol>
|
||||
<li>File objects
|
||||
<li>Reading files
|
||||
<li>Close your files... or don't
|
||||
<li>Handling I/O errors
|
||||
<li>Writing to files
|
||||
</ol>
|
||||
<li>Regular expressions
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>Case study: street addresses
|
||||
<li>Case study: Roman numerals
|
||||
<ol>
|
||||
<li>Checking for thousands
|
||||
<li>Checking for hundreds
|
||||
</ol>
|
||||
<li>Using the {n,m} syntax
|
||||
<ol>
|
||||
<li>Checking for tens and ones
|
||||
</ol>
|
||||
<li>Verbose regular expressions
|
||||
<li>Case study: parsing phone numbers
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>HTML processing
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>html5lib
|
||||
<ol>
|
||||
<li>Installing html5lib
|
||||
<li>Using html5lib
|
||||
</ol>
|
||||
<li>Extracting data from HTML documents
|
||||
<li>Building HTML documents
|
||||
<li>Putting it all together
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>XML Processing
|
||||
<ol>
|
||||
<li>...major changes afoot...
|
||||
</ol>
|
||||
<li>HTTP web services
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>How not to fetch data over HTTP
|
||||
<li>Features of HTTP
|
||||
<ol>
|
||||
<li>User-Agent
|
||||
<li>Redirects
|
||||
<li>Last-Modified/If-Modified-Since
|
||||
<li>ETag-If-None-Match
|
||||
<li>Compression
|
||||
</ol>
|
||||
<li>Differences from Python 2
|
||||
<li>httplib2 (note: needs port)
|
||||
<ol>
|
||||
<li>Installing httplib2
|
||||
<li>Why httplib2 is better than http.client
|
||||
</ol>
|
||||
<li>Debugging HTTP web services
|
||||
<li>Setting the User-Agent
|
||||
<li>Handling Last-Modified and ETag
|
||||
<li>Handling redirects
|
||||
<li>Handling compressed data
|
||||
<li>Putting it all together
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>Unit testing
|
||||
<ol>
|
||||
<li>Introduction to Roman numerals
|
||||
<li>Diving in
|
||||
<li>Introducing romantest.py
|
||||
<li>Testing for success
|
||||
<li>Testing for failure
|
||||
<li>Testing for sanity
|
||||
</ol>
|
||||
<li>Test-first programming
|
||||
<ol>
|
||||
<li>roman.py, stage 1
|
||||
<li>roman.py, stage 2
|
||||
<li>roman.py, stage 3
|
||||
<li>roman.py, stage 4
|
||||
<li>roman.py, stage 5
|
||||
</ol>
|
||||
<li>Refactoring your code
|
||||
<ol>
|
||||
<li>Handling bugs
|
||||
<li>Handling changing requirements
|
||||
<li>The art of refactoring
|
||||
<li>Postscript
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>Dynamic functions
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>plural.py, stage 1
|
||||
<li>plural.py, stage 2
|
||||
<li>plural.py, stage 3
|
||||
<li>plural.py, stage 4
|
||||
<li>plural.py, stage 5
|
||||
<li>plural.py, stage 6
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li>Metaclasses
|
||||
<ol>
|
||||
<li>...once I figure out WTF metaclasses are...
|
||||
</ol>
|
||||
<li>Performance tuning
|
||||
<ol>
|
||||
<li>Diving in
|
||||
<li>Using the timeit module
|
||||
<li>Optimizing regular expressions
|
||||
<li>Optimizing dictionary lookups
|
||||
<li>Optimizing list operations
|
||||
<li>Optimizing string manipulation
|
||||
<li>Summary
|
||||
</ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html">Case study: porting <code>chardet</code> to Python 3</a>
|
||||
<ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#divingin">Introducing <code class="filename">chardet</code>: a mini-FAQ</a>
|
||||
<ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#faq.what">What is character encoding auto-detection?</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#faq.impossible">Isn't that impossible?</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#faq.who">Who wrote this detection algorithm?</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#faq.yippie">Yippie! Screw the standards, I'll just auto-detect everything!</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#faq.why">Why bother with auto-detection if it's slow, inaccurate, and non-standard?</a>
|
||||
</ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#divingin2">Diving in</a>
|
||||
<ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#how.bom"><code>UTF-n</code> with a <abbr title="Byte Order Mark">BOM</abbr></a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#how.esc">Escaped encodings</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#how.mb">Multi-byte encodings</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#how.sb">Single-byte encodings</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#how.windows1252"><code>windows-1252</code></a>
|
||||
</ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#running2to3">Running <code class="filename">2to3</code></a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#manual">Fixing what <code class="filename">2to3</code> can't</a>
|
||||
<ol>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#falseisinvalidsyntax"><code>False</code> is invalid syntax</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#nomodulenamedconstants">No module named <code class="filename">constants</code></a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#namefileisnotdefined">Name '<var>file</var>' is not defined</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#cantuseastringpattern">Can't use a string pattern on a bytes-like object</a>
|
||||
<li><a href="case-study-porting-chardet-to-python-3.html#cantconvertbytesobject">Can't convert '<code>bytes</code>' object to <code>str</code> implicitly</a>
|
||||
</ol>
|
||||
</ol>
|
||||
<li>Packaging Python libraries
|
||||
<!-- http://pypi.python.org/pypi -->
|
||||
<ol>
|
||||
<li>A brief history of packaging (and why it's harder than you think)
|
||||
<li>setuptools
|
||||
<li>distutils
|
||||
<li>Eggs
|
||||
<li>pip
|
||||
<li>Platform-specific packaging
|
||||
<ol>
|
||||
<li>Packaging by Linux distributions
|
||||
<li>Py2exe
|
||||
</ol>
|
||||
</ol>
|
||||
<li>Creating graphics with the Python Imaging Library
|
||||
<ol>
|
||||
<li>...<a href="http://www.reddit.com/r/Python/comments/7sj39/dive_into_python_3/c07b3cq">will likely get ported in time</a>...
|
||||
</ol>
|
||||
<li>Where to go from here (tentative because most of these have not been ported to Python 3 yet)
|
||||
<ol>
|
||||
<li>WSGI
|
||||
<li>Django
|
||||
<li>Pylons
|
||||
<li>TurboGears
|
||||
<li>AppEngine
|
||||
<li>IronPython
|
||||
<li>Jython
|
||||
<li>PyPy
|
||||
<li>Stackless Python
|
||||
</ol>
|
||||
</ol>
|
||||
<ul>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html">Appendix A. Porting code to Python 3 with <code class="filename">2to3</code></a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#divingin">Diving in</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#print"><code>print</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicodeliteral">Unicode string literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#unicode"><code>unicode()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#long"><code>long</code> data type</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#ne"><> comparison</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#has_key"><code>has_key()</code> dictionary method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dict">Dictionary methods that return lists</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#imports">Modules that have been renamed or reorganized</a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#http"><code>http</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#urllib"><code>urllib</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#dbm"><code>dbm</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xmlrpc"><code>xmlrpc</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#othermodules">Other modules</a>
|
||||
</ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#import">Relative imports within a package</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#next"><code>next()</code> iterator method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#filter"><code>filter()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#map"><code>map()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#reduce"><code>reduce()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#apply"><code>apply()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#intern"><code>intern()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#exec"><code>exec</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#execfile"><code>execfile</code> statement</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#repr"><code>repr</code> literals (backticks)</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#except"><code>try...except</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#raise"><code>raise</code> statement</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#throw"><code>throw</code> method on generators</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xrange"><code>xrange()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#raw_input"><code>raw_input()</code> and <code>input()</code> global functions</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#funcattrs"><code>func_*</code> function attributes</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#xreadlines"><code>xreadlines()</code> I/O method</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#tuple_params"><code>lambda</code> functions with multiple parameters</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#methodattrs">Special method attributes</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#nonzero"><code>__nonzero__</code> special class attribute</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#numliterals">Octal literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#renames"><code>sys.maxint</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#callable"><code>callable()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#zip"><code>zip()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#standarderror"><code>StandardError()</code> exception</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#types"><code>types</code> module constants</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#isinstance"><code>isinstance()</code> global function</a> (3.1+)
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#basestring"><code>basestring</code> datatype</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#itertools"><code>itertools</code> module</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#sys_exc"><code>sys.exc_type</code>, <code>sys.exc_value</code>, <code>sys.exc_traceback</code></a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#paren">List comprehensions over tuples</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#getcwdu"><code>os.getcwdu()</code> function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#metaclass">Metaclasses</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#nitpick">Matters of style</a>
|
||||
<ol>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#set_literal"><code>set()</code> literals</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#buffer"><code>buffer()</code> global function</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#wscomma">Whitespace around commas</a>
|
||||
<li><a href="porting-code-to-python-3-with-2to3.html#idioms">Common idioms</a>
|
||||
</ol>
|
||||
</ol>
|
||||
</ul>
|
||||
<p>Orphans (not sure where these belong yet):
|
||||
<ul>
|
||||
<li>Tuples
|
||||
<li>Iterators
|
||||
<li>Generators
|
||||
<li>List comprehensions
|
||||
<li>Set comprehensions
|
||||
<li>Dictionary comprehensions
|
||||
<li>Views (several dictionary methods return them, they're dynamic, update when the dictionary changes, etc.)
|
||||
</ul>
|
||||
<p class="c">© 2001-4, 2009 <span>ℳ</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
|
||||
</body>
|
||||
</html>
|
||||
@@ -3,8 +3,8 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Your first Python program - Dive into Python 3</title>
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="dip3.css">
|
||||
<script type="text/javascript" src="dip3.packed.js"></script>
|
||||
<link rel="shortcut icon" href="data:image/ico,">
|
||||
<link rel="alternate" type="application/atom+xml" href="http://hg.diveintopython3.org/atom-log">
|
||||
<style type="text/css">
|
||||
@@ -14,7 +14,7 @@ body{counter-reset:h1 1}
|
||||
<body>
|
||||
<p class="skip"><a href="#divingin">skip to main content</a>
|
||||
<form action="http://www.google.com/cse" id="search"><div><input type="hidden" name="cx" value="014021643941856155761:l5eihuescdw"><input type="hidden" name="ie" value="UTF-8"> <input name="q" size="31"> <input type="submit" name="sa" value="Search"></div></form>
|
||||
<p class="nav">You are here: <a href="/">Dive Into Python 3</a> <span>‣</span>
|
||||
<p class="nav">You are here: <a href="/">Home</a> <span>‣</span> <a href="table-of-contents.html">Dive Into Python 3</a> <span>‣</span>
|
||||
<h1>Your first Python program</h1>
|
||||
<blockquote class="q">
|
||||
<p><span>❝</span> Don’t bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate. <span>❞</span><br>— <cite>Ven. Henepola Gunararatana</cite>
|
||||
|
||||
Reference in New Issue
Block a user