📄 27.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Developer's Handbook -> Built-In Data Types</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="22.html" class="navtitle">2. Language Review</a> > <span class="nonavtitle">Built-In Data Types</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="26.html" title="Programs"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=27" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="27.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="28.html" title="Operators"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A18%3A51+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162145081058105061222103253</font><a href="read4.asp?bookname=0672319942&snode=27&now=5%2F31%2F2002+4%3A18%3A51+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Built-In Data Types</h3>
<p>Built-in data types are types that are already built into the interpreter. They are divided into two groups:</p>
<H4>
Immutable Data Types</H4>
<P>These objects cannot have their values altered (for example, strings, numbers, and tuples).</P>
<h4>
Mutable Data Types</h4>
<p>These objects can have their values manipulated (for example, lists and dictionaries).</p>
<P>Sometimes, it becomes necessary to <A NAme="idx1073742160"></a>
<a NAME="idx1073742161"></a>
<a name="idx1073742162"></a>assign a <tt class="monofont">null</tt> value to a variable using the special data type known as <a namE="idx1073742163"></a>
<a Name="idx1073742164"></A>
<tt clAss="monofont">None:</tT>
</P>
<PRe>
>>> x = 1
>>> x
1
>>> print x
1
>>> x = None
>>> x
>>>
</pre>
<P>As you could see, nothing was returned. However, if you try to print this value, the <A NAme="idx1073742165"></a>
<a NAME="idx1073742166"></a>
<tt cLASS="monofont">print</tt> method of the object will specially handle the <tt class="monofont">None</tt> value by returning a <tt class="monofont">None</tt> result. This is shown in the following:</P>
<prE>
>>> print x
None
</pre>
<H4>
Numbers</h4>
<p>Python provides the following numeric data types: <a nAme="idx1073742167"></a>integer, <A NAMe="idx1073742168"></a>floating-point, hexadecimal (base 16), and octal (base 8). Some examples of these data types are <tt CLASs="monofont">43, 1.5, 0xB3,</tt> and <tT CLAss="monofont">045,</tt> respectively.</P>
<DIV class="note"><p class="notetitle"><b>Tip</b></p><p>
<p>
<a namE="idx1073742169"></a>Hexadecimal numbers must always be preceded by <tT claSs="monofont">0x,</tt> and <a Name="idx1073742170"></A>octal numbers must be preceded by <TT Class="monofont">0.</TT>
</P>
</P></div>
<bR>
<BR>
<P>Python can do a lot of things with numbers:</p>
<p>It can write <a nAME="idx1073742171"></A>equations:</p>
<pre>
>>> 3*(3.0/34)
0.264705882353
</pre>
<p>It can use <a name="idx1073742172"></a>functions:</p>
<pre>
>>> round(12.32,1)
12.3
</prE>
<p>It can make <a Name="idx1073742173"></A>comparisons:</p>
<pre>
>>> x = 2
>>> 0<x<5
1
</Pre>
<p>It can make <A NAMe="idx1073742174"></a>
<a nAME="idx1073742175"></A>binary operations, such as <a namE="idx1073742176"></A>
<TT clasS="monofont">shifting</TT> and <A name="idx1073742177"></a>
<tt class="monofont">masking:</tt>
</p>
<pre>
>>> 16<<2
64
>>> 40&0xab
40
>>> 2|1
3
>>> ~2
-3
>>> 3^4
7
</prE>
<p>A very important detail is the fact that Python <a Name="idx1073742178"></A>
<a namE="idx1073742179"></a>truncates <tt CLASs="monofont">integer</tt> divisions:</p>
<PRE>
>>> 3/2
1
</Pre>
<p>If you really want the <a NAME="idx1073742180"></a>decimals, you have two options. Either you pass a converted number to the division function, or you put a decimal point in your number, as illustrated here:</p>
<prE>
>>> x = 3
>>> float(x)/2
1.5
>>> x
3
>>> 3.0/2
1.5
</PRE>
<p>Python supports long integers梬ith unlimited size. To let Python know that it should <a name="idx1073742181"></a>
<a name="idx1073742182"></a>
<a name="idx1073742183"></a>handle an integer as a long integer, you need to put an <tT clAss="monofont">L</tT> at the end of the number:</p>
<pre>
>>> 2L**100
<I>1267650600228229401496703205376L</i>
</prE>
<P>Otherwise you get an <A Name="idx1073742184"></a>
<A NAMe="idx1073742185"></a>error message:</p>
<pRE>
>>> 2**100
Traceback (innermost last):
File "<stdin>", line 1, in ?
OverflowError: integer pow()
</PRe>
<p>
<a hREF="91.html">Chapter 4, "Exception Handling,"
</A> teaches you how to interpret this exception message.</p>
<p>Python also <a name="idx1073742186"></a>
<a name="idx1073742187"></a>handles complex numbers in the format (<tt clasS="monofont">real part + imaginary part</tt>):</P>
<pre>
>>> 2j**2
(-4+0j)
</Pre>
<h4>
Strings</h4>
<P>Python considers a string as a sequence of <a naME="idx1073742188"></A>
<A name="idx1073742189"></A>characters. Therefore, every time you use, for example, the string <TT Class="monofont">"Parrot",</TT> internally Python handles it as the sequence <TT clasS="monofont">["P", "a", "r", "r", "o", "t"].</TT> The first <A name="idx1073742190"></a>
<a name="idx1073742191"></a>indexer value is always the number zero. Hence, to have access to the letter <tt class="monofont">P,</tt> you need to say <Tt cLass="monofont">"Parrot"[0]</Tt> and to access the letter <tt cLass="monofont">a,</TT> you need to say <TT clasS="monofont">"Parrot"[1].</TT> Using the same concept, we can get access to all the other elements.</P>
<p>The following is an example of string operators:</p>
<prE>
>>> "dead parrot " + "sketch" # concatenation
"dead parrot sketch"
>>> "parrot " * 2 # repetition
"parrot parrot"
>>> "parrot"[1] # indexing
"a"
>>> "parrot"[-1] # indexing backward
"t"
>>> "parrot"[1:3] # slicing (*)
"ar"
</PRE>
<p>When <a naME="idx1073742192"></A>slicing, it isn't necessary to include both first and last elements. Whenever you omit one of the elements, it is assumed that you want everything in that direction. Note that the second argument is always a positional reference.<A name="idx1073742193"></a>
</p>
<pre>
>>> "parrot"[1:]
"arrot"
>>> "parrot"[:3]
"par"
</pre>
<p>Always remember that assigning <tt class="monofont">z = x</Tt> doesn't make a copy of the object <tT claSs="monofont">x.</tt> Instead, it creates a new reference for that object (as you already saw in the earlier <tT claSS="monofont">round</TT> example). If you have to create a <a namE="idx1073742194"></A>
<A Name="idx1073742195"></a>
<A NAMe="idx1073742196"></a>copy of a sequence named <tt CLASs="monofont">x,</tt> you need to type:</p>
<pre>
>>> z = x[:]
</pre>
<p>The variable <tt class="monofont">z</tt> will identify the middle of the variable <Tt cLass="monofont">x,</Tt> and it will be initialized with everything from the left direction plus everything from the right direction. Note that since Python 1.5, <tt cLass="monofont">id(s) == id(s[:])</TT> for strings because of string interning.</P>
<P>Strings cannot be modified after creation. It isn't possible to <a namE="idx1073742197"></A>
<A Name="idx1073742198"></a>
<A NAMe="idx1073742199"></a>assign a value to a substring because strings are <a nAME="idx1073742200"></A>
<a name="idx1073742201"></a>immutable. See the <a name="idx1073742202"></a>
<a name="idx1073742203"></a>error message in the next example:</p>
<pRe>
>>> t = "pxrrot"
>>> t[1:2] = "a"
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support slice assignment
</pRe>
<p>In cases like this, the usual solution is a little trick:</p>
<Pre>
s = s[:left_element] + new_substring + s[right_element:]
</prE>
<p>For example</p>
<pRE>
>>> t = "pxrrot"
>>> t = t[:1] + "a" + t[2:]
>>> t
"parrot"
</PRe>
<p>Let me show you other useful operations that you can do with strings:<a nAME="idx1073742204"></A>
</p>
<pre>
>>> len("parrot") # Get its length
6
>>> "parrot" < "sketch" # Compare one string against another.
1
>>> "t" in "parrot" # This logical test needs a char left operand
1
>>> "\n, \0, \x" # Use escape codes
"\012, \000, \\x"
</PRE>
<P>
<a hreF="27#5.html">Table 2.1</A> lists the escape codes supported by Python strings.</P>
<A name="5"></a><p><table border="1" celLspAcinG="0" cellPaddING="1" Width="100%">
<CAPTion><h5>Table
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -