Files
dive-into-python3/native-datatypes.html
T
2009-02-07 14:46:24 -05:00

188 lines
14 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">
<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">&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>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
<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 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>FIXME
<h2 id="lists">Lists</h2>
<p>FIXME
<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>
<p class="c">&copy; 2001-4, 2009 <span>&#x2133;</span>ark Pilgrim, <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">CC-BY-3.0</a>
</body>
</html>