Files
dive-into-python3/native-datatypes.html
T

658 lines
47 KiB
HTML

<!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>
<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>
<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>&nbsp;<input name=q size=31>&nbsp;<input type=submit name=root value=Search></div></form>
<p class=nav>You are here: <a href=/>Home</a> <span>&#8227;</span> <a href=table-of-contents.html>Dive Into Python 3</a> <span>&#8227;</span>
<h1>Native datatypes</h1>
<blockquote class=q>
<p><span>&#x275D;</span> Wonder is the foundation of all philosophy, research its progress, ignorance its end. <span>&#x275E;</span><br>&mdash; <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>
<ol>
<li><a href=#creatinglists>Creating lists</a>
<li><a href=#slicinglists>Slicing lists</a>
</ol>
<!--
<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><a href=#sets>Sets</a>
<!--
<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><a href=#dictionaries>Dictionaries</a>
<li><a href=#none><code>None</code></a>
<li><a href=#furtherreading>Further reading</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 datatypes. <a href=your-first-python-program.html#datatypes>Every variable has a datatype</a>, even though you don't declare it explicitly. Based on each variable's original assignment, Python figures out what type it is and keeps tracks of that internally.
<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>&amp;</i>c. You'll learn about classes in [FIXME xref] and files in [FIXME xref].
<p>Strings and bytes are important enough &mdash; and complicated enough &mdash; 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 &lt; 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>&lt;</code> is a numerical operator. The result of the expression <code>size &lt; 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 &lt; 0</kbd>
<samp>False</samp>
<samp class=prompt>>>> </samp><kbd>size = 0</kbd>
<samp class=prompt>>>> </samp><kbd>size &lt; 0</kbd>
<samp>False</samp>
<samp class=prompt>>>> </samp><kbd>size = -1</kbd>
<samp class=prompt>>>> </samp><kbd>size &lt; 0</kbd>
<samp>True</samp></pre>
<h2 id=numbers>Numbers</h2>
<p>Numbers are awesome. There are so many to choose from. Python supports both integers and floating point numbers. There's no type declaration to distinguish them; Python tells them apart by the presence or absence of a decimal point.
<pre class=screen>
<a><samp class=prompt>>>> </samp><kbd>type(1)</kbd> <span>&#x2460;</span></a>
<samp>&lt;class 'int'></samp>
<a><samp class=prompt>>>> </samp><kbd>1 + 1</kbd> <span>&#x2461;</span></a>
<samp>2</samp>
<a><samp class=prompt>>>> </samp><kbd>1 + 1.0</kbd> <span>&#x2462;</span></a>
<samp>2.0</samp>
<samp class=prompt>>>> </samp><kbd>type(2.0)</kbd>
<samp>&lt;class 'float'></samp></pre>
<ol>
<li>You can use the <code>type()</code> function to check the type of any value or variable. As you might expect, <code>1</code> is an <code>int</code>.
<li>Adding an <code>int</code> to an <code>int</code> yields an <code>int</code>.
<li>Adding an <code>int</code> to a <code>float</code> yields a <code>float</code>. Python coerces the <code>int</code> into a <code>float</code> to perform the addition, then returns a <code>float</code> as the result.
</ol>
<p>As you just saw, some operators (like addition) will coerce integers to floating point numbers as needed. You can also coerce them by yourself.
<pre class=screen>
<a><samp class=prompt>>>> </samp><kbd>float(2)</kbd> <span>&#x2460;</span></a>
<samp>2.0</samp>
<a><samp class=prompt>>>> </samp><kbd>int(2.0)</kbd> <span>&#x2461;</span></a>
<samp>2</samp>
<a><samp class=prompt>>>> </samp><kbd>int(2.5)</kbd> <span>&#x2462;</span></a>
<samp>2</samp>
<a><samp class=prompt>>>> </samp><kbd>int(-2.5)</kbd> <span>&#x2463;</span></a>
<samp>-2</samp>
<a><samp class=prompt>>>> </samp><kbd>1.12345678901234567890</kbd> <span>&#x2464;</span></a>
<samp>1.1234567890123457</samp>
<a><samp class=prompt>>>> </samp><kbd>type(1000000000000000)</kbd> <span>&#x2465;</span></a>
<samp>&lt;class 'int'></samp></pre>
<ol>
<li>You can explicitly coerce an <code>int</code> to a <code>float</code> by calling the <code>float()</code> function.
<li>Unsurprisingly, you can also coerce a <code>float</code> to an <code>int</code> by calling <code>int()</code>.
<li>The <code>int()</code> function will truncate, not round.
<li>The <code>int()</code> function truncates negative numbers towards <code>0</code>. It's a true truncate function, not a a floor function.
<li>Floating point numbers are accurate to 15 decimal places.
<li>Integers can be arbitrarily large.
</ol>
<blockquote class="note compare python2">
<p><span>&#x261E;</span>Python 2 had separate types for <code>int</code> and <code>long</code>. The <code>int</code> datatype was limited by <code>sys.maxint</code>, which varied by platform but was usually <code>2<sup>32</sup>-1</code>. Python 3 has just one integer type, which behaves mostly like the old <code>long</code> type from Python 2. See <a href=http://www.python.org/dev/peps/pep-0237>PEP 237</a> for details.
</blockquote>
<p>You can do all kinds of things with numbers.
<pre class=screen>
<a><samp class=prompt>>>> </samp><kbd>11 / 2</kbd> <span>&#x2460;</span></a>
<samp>5.5</samp>
<a><samp class=prompt>>>> </samp><kbd>11 // 2</kbd> <span>&#x2461;</span></a>
<samp>5</samp>
<a><samp class=prompt>>>> </samp><kbd>&minus;11 // 2</kbd> <span>&#x2462;</span></a>
<samp>&minus;6</samp>
<a><samp class=prompt>>>> </samp><kbd>11.0 // 2</kbd> <span>&#x2463;</span></a>
<samp>5.0</samp>
<a><samp class=prompt>>>> </samp><kbd>11 ** 2</kbd> <span>&#x2464;</span></a>
<samp>121</samp>
<a><samp class=prompt>>>> </samp><kbd>11 % 2</kbd> <span>&#x2465;</span></a>
<samp>1</samp>
</pre>
<ol>
<li>The <code>/</code> operator performs floating point division. It returns a <code>float</code> even if both the numerator and denominator are <code>int</code>s.
<li>The <code>//</code> operator performs a quirky kind of integer division. When the result is positive, you can think of it as truncating (not rounding) to <code>0</code> decimal places, but be careful with that.
<li>When integer-dividing negative numbers, the <code>//</code> operator rounds &#8220;up&#8221; to the nearest integer. Mathematically speaking, it's rounding &#8220;down&#8221; since <code>&minus;6</code> is less than <code>&minus;5</code>, but it could trip you up if you expecting it to truncate to <code>&minus;5</code>.
<li>The <code>//</code> operator doesn't always return an integer. If either the numerator or denominator is a <code>float</code>, it will still round to the nearest integer, but the actual return value will be a <code>float</code>.
<li>The <code>**</code> operator means &#8220;raised to the power of.&#8221; <code>11<sup>2</sup></code> is <code>121</code>.
<li>The <code>%</code> operator gives the remainder after performing integer division. <code>11</code> divided by <code>2</code> is <code>5</code> with a remainder of <code>1</code>, so the result here is <code>1</code>.
</ol>
<blockquote class="note compare python2">
<p><span>&#x261E;</span>In Python 2, the <code>/</code> operator usually meant integer division, but you could make it behave like floating point division by including a special directive in your code. In Python 3, the <code>/</code> operator always means floating point division. See <a href=http://www.python.org/dev/peps/pep-0238/>PEP 238</a> for details.
</blockquote>
<p>FIXME fractions, math module, numbers in a boolean context
<h2 id=lists>Lists</h2>
<p>Lists are Python's workhorse datatype. When I say "list," you might be thinking "array whose size I have to declare in advance, that can only contain items of the same type, <i class=baa>&amp;</i>c." Don't think that. Lists are much cooler than that.
<blockquote class="note compare perl5">
<p><span>&#x261E;</span>A list in Python is like an array in Perl 5. In Perl 5, variables that store arrays always start with the <code>@</code> character; in Python, variables can be named anything, and Python keeps track of the datatype internally.
</blockquote>
<blockquote class="note compare java">
<p><span>&#x261E;</span>A list in Python is much more than an array in Java (although it can be used as one if that's really all you want out of life). A better analogy would be to the <code>ArrayList</code> class, which can hold arbitrary objects and can expand dynamically as new items are added.
</blockquote>
<h3 id=creatinglists>Creating lists</h3>
<p>Creating a list is easy: use square brackets to wrap a comma-separated list of values.
<pre class=screen>
<a><samp class=prompt>>>> </samp><kbd>a_list = ["a", "b", "mpilgrim", "z", "example"]</kbd> <span>&#x2460;</span></a>
<samp class=prompt>>>> </samp><kbd>a_list</kbd>
['a', 'b', 'mpilgrim', 'z', 'example']
<a><samp class=prompt>>>> </samp><kbd>a_list[0]</kbd> <span>&#x2461;</span></a>
<samp>'a'</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[4]</kbd> <span>&#x2462;</span></a>
<samp>'example'</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[-1]</kbd> <span>&#x2463;</span></a>
<samp>'example'</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[-3]</kbd> <span>&#x2464;</span></a>
<samp>'mpilgrim'</samp></pre>
<ol>
<li>First, you define a list of five items. Note that they retain their original order. This is not an accident. A list is an ordered set of items.
<li>A list can be used like a zero-based array. The first element of any non-empty list is always <code>a_list[0]</code>.
<li>The last element of this five-element list is <code>a_list[4]</code>, because lists are always zero-based.
<li>A negative index accesses elements from the end of the list counting backwards. The last element of any non-empty list is always <code>a_list[-1]</code>.
<li>If the negative index is confusing to you, think of it this way: <code>a_list[-<var>n</var>] == a_list[len(a_list) - <var>n</var>]</code>. So in this list, <code>a_list[-3] == a_list[5 - 3] == a_list[2]</code>.
</ol>
<h3 id=slicinglists>Slicing lists</h3>
<p>Once you've defined a list, you can get any part of it as a new list. This is called <i>slicing</i> the list.
<pre class=screen>
<samp class=prompt>>>> </samp><kbd>a_list</kbd>
<samp>['a', 'b', 'mpilgrim', 'z', 'example']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[1:3]</kbd> <span>&#x2460;</span></a>
<samp>['b', 'mpilgrim']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[1:-1]</kbd> <span>&#x2461;</span></a>
<samp>['b', 'mpilgrim', 'z']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[0:3]</kbd> <span>&#x2462;</span></a>
<samp>['a', 'b', 'mpilgrim']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[:3]</kbd> <span>&#x2463;</span></a>
<samp>['a', 'b', 'mpilgrim']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[3:]</kbd> <span>&#x2464;</span></a>
<samp>['z', 'example']</samp>
<a><samp class=prompt>>>> </samp><kbd>a_list[:]</kbd> <span>&#x2465;</span></a>
['a', 'b', 'mpilgrim', 'z', 'example']</pre>
<ol>
<li>You can get a part of a list, called a &#8220;slice&#8221;, by specifying two indices. The return value is a new list containing all the elements of the list, in order, starting with the first slice index (in this case <code>a_list[1]</code>), up to but not including the second slice index (in this case <code>a_list[3]</code>).
<li>Slicing works if one or both of the slice indices is negative. If it helps, you can think of it this way: reading the list from left to right, the first slice index specifies the first element you want, and the second slice index specifies the first element you don't want. The return value is everything in between.
<li>Lists are zero-based, so <code>a_list[0:3]</code> returns the first three elements of the list, starting at <code>a_list[0]</code>, up to but not including <code>a_list[3]</code>.
<li>If the left slice index is <code>0</code>, you can leave it out, and <code>0</code> is implied. So <code>a_list[:3]</code> is the same as <code>a_list[0:3]</code>, because the starting <code>0</code> is implied.
<li>Similarly, if the right slice index is the length of the list, you can leave it out. So <code>a_list[3:]</code> is the same as <code>a_list[3:5]</code>, because this list has five elements. There is a pleasing symmetry here. In this five-element list, <code>a_list[:3]</code> returns the first 3 elements, and <code>a_list[3:]</code> returns the last two elements. In fact, <code>a_list[:<var>n</var>]</code> will always return the first <var>n</var> elements, and <code>a_list[<var>n</var>:]</code> will return the rest, regardless of the length of the list.
<li>If both slice indices are left out, all elements of the list are included. But this is not the same as the original <var>a_list</var> variable. It is a new list that happens to have all the same elements. <code>a_list[:]</code> is shorthand for making a complete copy of a list.
</ol>
<!--
<h3>3.2.2. Adding Elements to Lists</h3>
<div class="example"><h3>Example 3.10. Adding Elements to a List</h3><pre class=screen><samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'z', 'example']
<samp class=prompt>>>> </samp>li.append("new") <img id=odbchelper.list.5.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
<samp class=prompt>>>> </samp>li.insert(2, "new") <img id=odbchelper.list.5.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
<samp class=prompt>>>> </samp>li.extend(["two", "elements"]) <img id=odbchelper.list.5.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.1><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>append</code> adds a single element to the end of the list.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.2><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>insert</code> inserts a single element into a list. The numeric argument is the index of the first element that gets bumped out of position.
Note that list elements do not need to be unique; there are now two separate elements with the value <code>'new'</code>, <code>a_list[2]</code> and <code>a_list[6]</code>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>extend</code> concatenates lists. Note that you do not call <code>extend</code> with multiple arguments; you call it with one argument, a list. In this case, that list has two elements.
</td>
</tr>
</table>
<div class="example"><h3 id=odbchelper.list.append.vs.extend>Example 3.11. The Difference between <code>extend</code> and <code>append</code></h3><pre class=screen>
<samp class=prompt>>>> </samp>a_list = ['a', 'b', 'c']
<samp class=prompt>>>> </samp>li.extend(['d', 'e', 'f']) <img id=odbchelper.list.5.4" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'c', 'd', 'e', 'f']
<samp class=prompt>>>> </samp>len(li) <img id=odbchelper.list.5.5" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
6
<samp class=prompt>>>> </samp>a_list[-1]
'f'
<samp class=prompt>>>> </samp>a_list = ['a', 'b', 'c']
<samp class=prompt>>>> </samp>li.append(['d', 'e', 'f']) <img id=odbchelper.list.5.6" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'c', ['d', 'e', 'f']]
<samp class=prompt>>>> </samp>len(li) <img id=odbchelper.list.5.7" src="images/callouts/4.png" alt="4" border="0" width="12" height="12>
4
<samp class=prompt>>>> </samp>a_list[-1]
['d', 'e', 'f']
</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.4><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Lists have two methods, <code>extend</code> and <code>append</code>, that look like they do the same thing, but are in fact completely different. <code>extend</code> takes a single argument, which is always a list, and adds each of the elements of that list to the original list.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.5><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Here you started with a list of three elements (<code>'a'</code>, <code>'b'</code>, and <code>'c'</code>), and you extended the list with a list of another three elements (<code>'d'</code>, <code>'e'</code>, and <code>'f'</code>), so you now have a list of six elements.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.6><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">On the other hand, <code>append</code> takes one argument, which can be any data type, and simply adds it to the end of the list. Here, you're calling the <code>append</code> method with a single argument, which is a list of three elements.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.5.7><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Now the original list, which started as a list of three elements, contains four elements. Why four? Because the last element
that you just appended <em>is itself a list</em>. Lists can contain any type of data, including other lists. That may be what you want, or maybe not. Don't use <code>append</code> if you mean <code>extend</code>.
</td>
</tr>
</table>
<h3>3.2.3. Searching Lists</h3>
<div class="example"><h3 id=odbchelper.list.search>Example 3.12. Searching a List</h3><pre class=screen><samp class=prompt>>>> </samp>li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
<samp class=prompt>>>> </samp>li.index("example") <img id=odbchelper.list.6.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
5
<samp class=prompt>>>> </samp>li.index("new") <img id=odbchelper.list.6.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
2
<samp class=prompt>>>> </samp>li.index("c") <img id=odbchelper.list.6.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class="traceback">Traceback (innermost last):
File "&lt;interactive input>", line 1, in ?
ValueError: list.index(x): x not in list</samp>
<samp class=prompt>>>> </samp>"c" in a_list <img id=odbchelper.list.6.4" src="images/callouts/4.png" alt="4" border="0" width="12" height="12>
False</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.6.1><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>index</code> finds the first occurrence of a value in the list and returns the index.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.6.2><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>index</code> finds the <em>first</em> occurrence of a value in the list. In this case, <code>'new'</code> occurs twice in the list, in <code>a_list[2]</code> and <code>a_list[6]</code>, but <code>index</code> will return only the first index, <code>2</code>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.6.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">If the value is not found in the list, Python raises an exception. This is notably different from most languages, which will return some invalid index. While this may
seem annoying, it is a good thing, because it means your program will crash at the source of the problem, rather than later
on when you try to use the invalid index.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.6.4><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">To test whether a value is in the list, use <code>in</code>, which returns <code>True</code> if the value is found or <code>False</code> if it is not.
</td>
</tr>
</table>
</div><table id=tip.boolean" class="note" border="0" summary=">
<tr>
<td colspan="2" align="left" valign="top" width="99%">Before version 2.2.1, Python had no separate boolean datatype. To compensate for this, Python accepted almost anything in a boolean context (like an <code>if</code> statement), according to the following rules:
<div class="itemizedlist">
<ul>
<li><code>0</code> is false; all other numbers are true.
<li>An empty string (<code>""</code>) is false, all other strings are true.
<li>An empty list (<code>[]</code>) is false; all other lists are true.
<li>An empty tuple (<code>()</code>) is false; all other tuples are true.
<li>An empty dictionary (<code>{}</code>) is false; all other dictionaries are true.
</ul>
</div>These rules still apply in Python 2.2.1 and beyond, but now you can also use an actual boolean, which has a value of <code>True</code> or <code>False</code>. Note the capitalization; these values, like everything else in Python, are case-sensitive.
</td>
</tr>
</table>
<h3>3.2.4. Deleting List Elements</h3>
<div class="example"><h3 id=odbchelper.list.removingelements>Example 3.13. Removing Elements from a List</h3><pre class=screen><samp class=prompt>>>> </samp>li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
<samp class=prompt>>>> </samp>li.remove("z") <img id=odbchelper.list.7.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'new', 'mpilgrim', 'example', 'new', 'two', 'elements']
<samp class=prompt>>>> </samp>li.remove("new") <img id=odbchelper.list.7.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'example', 'new', 'two', 'elements']
<samp class=prompt>>>> </samp>li.remove("c") <img id=odbchelper.list.7.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class="traceback">Traceback (innermost last):
File "&lt;interactive input>", line 1, in ?
ValueError: list.remove(x): x not in list</samp>
<samp class=prompt>>>> </samp>li.pop() <img id=odbchelper.list.7.4" src="images/callouts/4.png" alt="4" border="0" width="12" height="12>
'elements'
<samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.7.1><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>remove</code> removes the first occurrence of a value from a list.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.7.2><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>remove</code> removes <em>only</em> the first occurrence of a value. In this case, <code>'new'</code> appeared twice in the list, but <code>li.remove("new")</code> removed only the first occurrence.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.7.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">If the value is not found in the list, Python raises an exception. This mirrors the behavior of the <code>index</code> method.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.7.4><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left"><code>pop</code> is an interesting beast. It does two things: it removes the last element of the list, and it returns the value that it removed.
Note that this is different from <code>a_list[-1]</code>, which returns a value but does not change the list, and different from <code>li.remove(<var>value</var>)</code>, which changes the list but does not return a value.
</td>
</tr>
</table>
<h3>3.2.5. Using List Operators</h3>
<div class="example"><h3 id=odbchelper.list.operators>Example 3.14. List Operators</h3><pre class=screen><samp class=prompt>>>> </samp>a_list = ['a', 'b', 'mpilgrim']
<samp class=prompt>>>> </samp>a_list = a_list + ['example', 'new'] <img id=odbchelper.list.8.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'example', 'new']
<samp class=prompt>>>> </samp>a_list += ['two'] <img id=odbchelper.list.8.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
['a', 'b', 'mpilgrim', 'example', 'new', 'two']
<samp class=prompt>>>> </samp>a_list = [1, 2] * 3 <img id=odbchelper.list.8.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>li
[1, 2, 1, 2, 1, 2]</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.8.1><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Lists can also be concatenated with the <code>+</code> operator. <code><var>list</var> = <var>list</var> + <var>otherlist</var></code> has the same result as <code><var>list</var>.extend(<var>otherlist</var>)</code>. But the <code>+</code> operator returns a new (concatenated) list as a value, whereas <code>extend</code> only alters an existing list. This means that <code>extend</code> is faster, especially for large lists.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.8.2><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Python supports the <code>+=</code> operator. <code>a_list += ['two']</code> is equivalent to <code>li.extend(['two'])</code>. The <code>+=</code> operator works for lists, strings, and integers, and it can be overloaded to work for user-defined classes as well. (More
on classes in <a href=#fileinfo>Chapter 5</a>.)
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.list.8.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The <code>*</code> operator works on lists as a repeater. <code>a_list = [1, 2] * 3</code> is equivalent to <code>a_list = [1, 2] + [1, 2] + [1, 2]</code>, which concatenates the three lists into one.
</td>
</tr>
</table>
<div class="itemizedlist">
<h3>Further Reading on Lists</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 lists and makes an important point about <a href=http://www.ibiblio.org/obp/thinkCSpy/chap08.htm>passing lists as function arguments</a>.
<li><a href=http://www.python.org/doc/current/tut/tut.html><i class="citetitle">Python Tutorial</i></a> shows how to <a href=http://www.python.org/doc/current/tut/node7.html#SECTION007110000000000000000>use lists as stacks and queues</a>.
<li><a href=http://www.faqts.com/knowledge-base/index.phtml/fid/199/>Python Knowledge Base</a> answers <a href=http://www.faqts.com/knowledge-base/index.phtml/fid/534>common questions about lists</a> and has a lot of <a href=http://www.faqts.com/knowledge-base/index.phtml/fid/540>example code using lists</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/typesseq-mutable.html>all the list methods</a>.
</ul>
<h2 id=odbchelper.tuple>3.3. Introducing Tuples</h2>
<p>A tuple is an immutable list. A tuple can not be changed in any way once it is created.
<div class="example"><h3>Example 3.15. Defining a tuple</h3><pre class=screen><samp class=prompt>>>> </samp>t = ("a", "b", "mpilgrim", "z", "example") <img id=odbchelper.tuple.1.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class=prompt>>>> </samp>t
('a', 'b', 'mpilgrim', 'z', 'example')
<samp class=prompt>>>> </samp>t[0] <img id=odbchelper.tuple.1.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
'a'
<samp class=prompt>>>> </samp>t[-1] <img id=odbchelper.tuple.1.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
'example'
<samp class=prompt>>>> </samp>t[1:3] <img id=odbchelper.tuple.1.4" src="images/callouts/4.png" alt="4" border="0" width="12" height="12>
('b', 'mpilgrim')</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.1.1><img src="images/callouts/1.png" alt="1" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">A tuple is defined in the same way as a list, except that the whole set of elements is enclosed in parentheses instead of
square brackets.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.1.2><img src="images/callouts/2.png" alt="2" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">The elements of a tuple have a defined order, just like a list. Tuples indices are zero-based, just like a list, so the first
element of a non-empty tuple is always <code>t[0]</code>.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.1.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Negative indices count from the end of the tuple, just as with a list.</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.1.4><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">Slicing works too, just like a list. Note that when you slice a list, you get a new list; when you slice a tuple, you get
a new tuple.
</td>
</tr>
</table>
<div class="example"><h3 id=odbchelper.tuplemethods>Example 3.16. Tuples Have No Methods</h3><pre class=screen><samp class=prompt>>>> </samp>t
('a', 'b', 'mpilgrim', 'z', 'example')
<samp class=prompt>>>> </samp>t.append("new") <img id=odbchelper.tuple.2.1" src="images/callouts/1.png" alt="1" border="0" width="12" height="12>
<samp class="traceback">Traceback (innermost last):
File "&lt;interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'append'</samp>
<samp class=prompt>>>> </samp>t.remove("z") <img id=odbchelper.tuple.2.2" src="images/callouts/2.png" alt="2" border="0" width="12" height="12>
<samp class="traceback">Traceback (innermost last):
File "&lt;interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'remove'</samp>
<samp class=prompt>>>> </samp>t.index("example") <img id=odbchelper.tuple.2.3" src="images/callouts/3.png" alt="3" border="0" width="12" height="12>
<samp class="traceback">Traceback (innermost last):
File "&lt;interactive input>", line 1, in ?
AttributeError: 'tuple' object has no attribute 'index'</samp>
<samp class=prompt>>>> </samp>"z" in t <img id=odbchelper.tuple.2.4" src="images/callouts/4.png" alt="4" border="0" width="12" height="12>
True</pre><div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.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't add elements to a tuple. Tuples have no <code>append</code> or <code>extend</code> method.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.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't remove elements from a tuple. Tuples have no <code>remove</code> or <code>pop</code> method.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.2.3><img src="images/callouts/3.png" alt="3" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">You can't find elements in a tuple. Tuples have no <code>index</code> method.
</td>
</tr>
<tr>
<td width="12" valign="top" align="left"><a href=#odbchelper.tuple.2.4><img src="images/callouts/4.png" alt="4" border="0" width="12" height="12"></a>
</td>
<td valign="top" align="left">You can, however, use <code>in</code> to see if an element exists in the tuple.
</td>
</tr>
</table>
<p>So what are tuples good for?
<div class="itemizedlist">
<ul>
<li>Tuples are faster than lists. If you're defining a constant set of values and all you're ever going to do with it is iterate
through it, use a tuple instead of a list.
<li>It makes your code safer if you &#8220;write-protect&#8221; data that does not need to be changed. Using a tuple instead of a list is like having an implied <code>assert</code> statement that shows this data is constant, and that special thought (and a specific function) is required to override that.
<li>Remember that I said that <a href=#odbchelper.dictionarytypes" title="Example 3.4. Mixing Datatypes in a Dictionary>dictionary keys</a> can be integers, strings, and &#8220;a few other types&#8221;? Tuples are one of those types. Tuples can be used as keys in a dictionary, but lists can't be used this way.Actually, it's more complicated than that. Dictionary keys must be immutable. Tuples themselves are immutable, but if you
have a tuple of lists, that counts as mutable and isn't safe to use as a dictionary key. Only tuples of strings, numbers,
or other dictionary-safe tuples can be used as dictionary keys.
<li>Tuples are used in string formatting, as you'll see shortly.
</ul>
</div><table id=tip.tuple" class="note" border="0" summary=">
<tr>
<td colspan="2" align="left" valign="top" width="99%">Tuples can be converted into lists, and vice-versa. The built-in <code>tuple</code> function takes a list and returns a tuple with the same elements, and the <code>list</code> function takes a tuple and returns a list. In effect, <code>tuple</code> freezes a list, and <code>list</code> thaws a tuple.
</td>
</tr>
</table>
<div class="itemizedlist">
<h3>Further Reading on Tuples</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 tuples and shows how to <a href=http://www.ibiblio.org/obp/thinkCSpy/chap10.htm>concatenate tuples</a>.
<li><a href=http://www.faqts.com/knowledge-base/index.phtml/fid/199/>Python Knowledge Base</a> shows how to <a href=http://www.faqts.com/knowledge-base/view.phtml/aid/4553/fid/587>sort a tuple</a>.
<li><a href=http://www.python.org/doc/current/tut/tut.html><i class="citetitle">Python Tutorial</i></a> shows how to <a href=http://www.python.org/doc/current/tut/node7.html#SECTION007300000000000000000>define a tuple with one element</a>.
</ul>
-->
<h2 id=sets>Sets</h2>
<p>FIXME
<h2 id=dictionaries>Dictionaries</h2>
<p>One of Python's most important datatypes is the dictionary, which defines one-to-one relationships between keys and values.
<blockquote class="note compare perl5">
<p><span>&#x261E;</span>A dictionary in Python is like a hash in Perl 5. In Perl 5, 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.
</blockquote>
<p>Creating a dictionary is easy. The syntax is similar to <a href=#sets>sets</a>, but instead of values, you have key-value pairs. Once you have a dictionary, you can look up values by their key.
<pre class=screen>
<a><samp class=prompt>>>> </samp><kbd>a_dict = {"server":"db.diveintopython3.org", "database":"mysql"}</kbd> <span>&#x2460;</span></a>
<samp class=prompt>>>> </samp><kbd>a_dict</kbd>
<samp>{'server': 'db.diveintopython3.org', 'database': 'mysql'}</samp>
<a><samp class=prompt>>>> </samp><kbd>a_dict["server"]</kbd> <span>&#x2461;</span></a>
'db.diveintopython3.org'
<a><samp class=prompt>>>> </samp><kbd>a_dict["database"]</kbd> <span>&#x2462;</span></a>
'mysql'
<a><samp class=prompt>>>> </samp><kbd>a_dict["db.diveintopython3.org"]</kbd> <span>&#x2463;</span></a>
<samp class=traceback>Traceback (most recent call last):
File "&lt;stdin>", line 1, in &lt;module>
KeyError: 'db.diveintopython3.org'</samp></pre>
<ol>
<li>First, you create a new dictionary with two elements and assign it to the variable <var>a_dict</var>. Each element is a key-value pair, and the whole set of elements is enclosed in curly braces.
<li><code>'server'</code> is a key, and its associated value, referenced by <code>a_dict["server"]</code>, is <code>'db.diveintopython3.org'</code>.
<li><code>'database'</code> is a key, and its associated value, referenced by <code>a_dict["database"]</code>, is <code>'mysql'</code>.
<li>You can get values by key, but you can't get keys by value. So <code>a_dict["server"]</code> is <code>'db.diveintopython3.org'</code>, but <code>a_dict["db.diveintopython3.org"]</code> raises an exception, because <code>'db.diveintopython3.org'</code> is not a key.
</ol>
<p>Dictionaries do not have any predefined size limit. You can add new key-value pairs to a dictionary at any time, or you can modify the value of an existing key. Continuing from the previous example:
<pre class=screen>
<samp class=prompt>>>> </samp><kbd>a_dict</kbd>
<samp>{'server': 'db.diveintopython3.org', 'database': 'mysql'}</samp>
<a><samp class=prompt>>>> </samp><kbd>a_dict["database"] = "blog"</kbd> <span>&#x2460;</span></a>
<samp class=prompt>>>> </samp><kbd>a_dict</kbd>
<samp>{'server': 'db.diveintopython3.org', 'database': 'blog'}</samp>
<a><samp class=prompt>>>> </samp><kbd>a_dict["user"] = "mark"</kbd> <span>&#x2461;</span></a>
<a><samp class=prompt>>>> </samp><kbd>a_dict</kbd> <span>&#x2462;</span></a>
<samp>{'server': 'db.diveintopython3.org', 'user': 'mark', 'database': 'blog'}</samp>
<a><samp class=prompt>>>> </samp><kbd>a_dict["user"] = "dora"</kbd> <span>&#x2463;</span></a>
<samp class=prompt>>>> </samp><kbd>a_dict</kbd>
<samp>{'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}</samp>
<a><samp class=prompt>>>> </samp><kbd>a_dict["User"] = "mark"</kbd> <span>&#x2464;</span></a>
<samp class=prompt>>>> </samp><kbd>a_dict</kbd>
<samp>{'User': 'mark', 'server': 'db.diveintopython3.org', 'user': 'dora', 'database': 'blog'}</samp></pre>
<ol>
<li>You can not have duplicate keys in a dictionary. Assigning a value to an existing key will wipe out the old value.
<li>You can add new key-value pairs at any time. This syntax is identical to modifying existing values.
<li>The new dictionary item (key <code>'user'</code>, value <code>'mark'</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.
<li>Assigning a value to an existing dictionary key simply replaces the old value with the new one.
<li>Will this change the value of the <code>user</code> key back to "mark"? No! Look at the key closely &mdash; that's a capital <kbd>U</kbd> in <kbd>"User"</kbd>. Dictionary keys are case-sensitive, so this statement is creating a new key-value pair, not overwriting an existing one. It may look similar to you, but as far as Python is concerned, it's completely different.
</ol>
<p>Dictionaries aren't just for strings. Dictionary values can be any datatype, including integers, booleans, arbitrary 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. 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.
<p>In fact, you've already seen a dictionary with non-string keys and values, in <a href=your-first-python-program.html#divingin>your first Python program</a>.
<pre><code>SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),
1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}</code></pre>
<p>Let's tear that apart in the interactive shell.
<pre class=screen>
<samp class=prompt>>>> </samp><kbd>SUFFIXES = {1000: ('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'),</kbd>
<samp class=prompt>... </samp><kbd> 1024: ('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')}</kbd>
<a><samp class=prompt>>>> </samp><kbd>len(SUFFIXES)</kbd> <span>&#x2460;</span></a>
<samp>2</samp>
<a><samp class=prompt>>>> </samp><kbd>SUFFIXES[1000]</kbd> <span>&#x2461;</span></a>
<samp>('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')</samp>
<a><samp class=prompt>>>> </samp><kbd>SUFFIXES[1024]</kbd> <span>&#x2462;</span></a>
<samp>('KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')</samp>
<a><samp class=prompt>>>> </samp><kbd>SUFFIXES[1000][3]</kbd> <span>&#x2463;</span></a>
<samp>'TB'</samp></pre>
<ol>
<li>As with <a href=#lists>lists</a> and <a href=#sets>sets</a>, the <code>len()</code> function gives you the number of items in a dictionary.
<li><code>1000</code> is a key in the <code>SUFFIXES</code> dictionary; its value is a tuple of eight items (eight strings, to be precise).
<li>Similarly, <code>1024</code> is a key in the <code>SUFFIXES</code> dictionary; its value is also a tuple of eight items.
<li>Since <code>SUFFIXES[1000]</code> is a tuple, you can address individual items in the tuple by their 0-based index.
</ol>
<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>&lt;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>
<h3 id=furtherreading>Further reading</h3>
<ul>
<li>fractions
<li>math module
<li>PEP 237
<li>PEP 238
<li>links to appendix
<li>...etc...
</ul>
<p class=c>&copy; 2001&ndash;4, 2009 <span>&#x2133;</span>ark Pilgrim, <a href=http://creativecommons.org/licenses/by-sa/3.0/ rel=license>CC-BY-SA-3.0</a>
<script type=text/javascript src=jquery.js></script>
<script type=text/javascript src=dip3.js></script>