<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>2.1.4 Numeric Types </TITLE>
<META NAME="description" CONTENT="2.1.4 Numeric Types ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="typesseq.html">
<LINK REL="previous" HREF="comparisons.html">
<LINK REL="up" HREF="types.html">
<LINK REL="next" HREF="bitstring-ops.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="bitstring-ops.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="types.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="comparisons.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="bitstring-ops.html">2.1.4.1 Bit-string Operations on</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="types.html">2.1 Built-in Types</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="comparisons.html">2.1.3 Comparisons</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION004140000000000000000"></A>
<BR>
2.1.4 Numeric Types 
</H2>

<P>
There are four numeric types: <i class=dfn>plain integers</i>, <i class=dfn>long integers</i>, 
<i class=dfn>floating point numbers</i>, and <i class=dfn>complex numbers</i>.
Plain integers (also just called <i class=dfn>integers</i>)
are implemented using <tt class=ctype>long</tt> in C, which gives them at least 32
bits of precision.  Long integers have unlimited precision.  Floating
point numbers are implemented using <tt class=ctype>double</tt> in C.  All bets on
their precision are off unless you happen to know the machine you are
working with.
<a name="l2h-39"></a>
<a name="l2h-40"></a>
<a name="l2h-41"></a>
<a name="l2h-42"></a>
<a name="l2h-43"></a>
<a name="l2h-44"></a>
<a name="l2h-45"></a>

<P>
Complex numbers have a real and imaginary part, which are both
implemented using <tt class=ctype>double</tt> in C.  To extract these parts from
a complex number <var>z</var>, use <tt><var>z</var>.real</tt> and <tt><var>z</var>.imag</tt>.  

<P>
Numbers are created by numeric literals or as the result of built-in
functions and operators.  Unadorned integer literals (including hex
and octal numbers) yield plain integers.  Integer literals with an "<tt class=samp>L</tt>"or "<tt class=samp>l</tt>" suffix yield long integers
("<tt class=samp>L</tt>" is preferred because "<tt class=samp>1l</tt>" looks too much like eleven!).
Numeric literals containing a decimal point or an exponent sign yield
floating point numbers.  Appending "<tt class=samp>j</tt>" or "<tt class=samp>J</tt>" to a numeric
literal yields a complex number.
<a name="l2h-46"></a>
<a name="l2h-47"></a>
<a name="l2h-48"></a>
<a name="l2h-49"></a>
<a name="l2h-50"></a>
<a name="l2h-51"></a>
<a name="l2h-52"></a>

<P>
Python fully supports mixed arithmetic: when a binary arithmetic
operator has operands of different numeric types, the operand with the
``smaller'' type is converted to that of the other, where plain
integer is smaller than long integer is smaller than floating point is
smaller than complex.
Comparisons between numbers of mixed type use the same rule.<A NAME="tex2html2"
 HREF="#foot1128"><SUP>2.2</SUP></A>The functions <tt class=function>int()</tt>, <tt class=function>long()</tt>, <tt class=function>float()</tt>,
and <tt class=function>complex()</tt> can be used
to coerce numbers to a specific type.
<a name="l2h-53"></a>
<a name="l2h-54"></a>
<a name="l2h-55"></a>
<a name="l2h-56"></a>
<a name="l2h-57"></a>

<P>
All numeric types support the following operations, sorted by
ascending priority (operations in the same box have the same
priority; all numeric operations have a higher priority than
comparison operations):

<P>
<table border align=center>
  <thead>
    <tr bgcolor="#99CCFF">
      <th><b>Operation</b></th>
      <th align=left><b>Result</b></th>
      <th><b>Notes</b></th>
    </thead>
  <tbody valign=baseline>
    <tr><td align=center valign=baseline><code><var>x</var> + <var>y</var></code></td>
        <td>sum of <var>x</var> and <var>y</var></td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code><var>x</var> - <var>y</var></code></td>
        <td>difference of <var>x</var> and <var>y</var></td>
        <td align=center>&nbsp;</td>
  
    <tr><td align=center valign=baseline><code><var>x</var> * <var>y</var></code></td>
        <td>product of <var>x</var> and <var>y</var></td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code><var>x</var> / <var>y</var></code></td>
        <td>quotient of <var>x</var> and <var>y</var></td>
        <td align=center>(1)&nbsp;</td>
    <tr><td align=center valign=baseline><code><var>x</var> % <var>y</var></code></td>
        <td>remainder of <tt><var>x</var> / <var>y</var></tt></td>
        <td align=center>&nbsp;</td>
  
    <tr><td align=center valign=baseline><code>-<var>x</var></code></td>
        <td><var>x</var> negated</td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code>+<var>x</var></code></td>
        <td><var>x</var> unchanged</td>
        <td align=center>&nbsp;</td>
  
    <tr><td align=center valign=baseline><code>abs(<var>x</var>)</code></td>
        <td>absolute value or magnitude of <var>x</var></td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code>int(<var>x</var>)</code></td>
        <td><var>x</var> converted to integer</td>
        <td align=center>(2)&nbsp;</td>
    <tr><td align=center valign=baseline><code>long(<var>x</var>)</code></td>
        <td><var>x</var> converted to long integer</td>
        <td align=center>(2)&nbsp;</td>
    <tr><td align=center valign=baseline><code>float(<var>x</var>)</code></td>
        <td><var>x</var> converted to floating point</td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code>complex(<var>re</var>,<var>im</var>)</code></td>
        <td>a complex number with real part <var>re</var>, imaginary part <var>im</var>.  <var>im</var> defaults to zero.</td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code><var>c</var>.conjugate()</code></td>
        <td>conjugate of the complex number <var>c</var></td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code>divmod(<var>x</var>, <var>y</var>)</code></td>
        <td>the pair <tt>(<var>x</var> / <var>y</var>, <var>x</var> % <var>y</var>)</tt></td>
        <td align=center>(3)&nbsp;</td>
    <tr><td align=center valign=baseline><code>pow(<var>x</var>, <var>y</var>)</code></td>
        <td><var>x</var> to the power <var>y</var></td>
        <td align=center>&nbsp;</td>
    <tr><td align=center valign=baseline><code><var>x</var> ** <var>y</var></code></td>
        <td><var>x</var> to the power <var>y</var></td>
        <td align=center>&nbsp;</td></tbody>
</table>
<a name="l2h-58"></a>
<a name="l2h-35"></a>
<P>
Notes:
<DL>
<DD><P>
<DT><STRONG>(1)</STRONG>
<DD>For (plain or long) integer division, the result is an integer.
The result is always rounded towards minus infinity: 1/2 is 0, 
(-1)/2 is -1, 1/(-2) is -1, and (-1)/(-2) is 0.
<a name="l2h-59"></a>
<a name="l2h-60"></a>

<P>
<DT><STRONG>(2)</STRONG>
<DD>Conversion from floating point to (long or plain) integer may round or
truncate as in C; see functions <tt class=function>floor()</tt> and <tt class=function>ceil()</tt> in
module <tt class=module>math</tt> for well-defined conversions.
<a name="l2h-37"></a><a name="l2h-38"></a><a name="l2h-61"></a>
<a name="l2h-62"></a>
<a name="l2h-63"></a>

<P>
<DT><STRONG>(3)</STRONG>
<DD>See the section on built-in functions for an exact definition.

<P>
</DL>

<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot1128">... rule.</A><A NAME="foot1128"
 HREF="typesnumeric.html#tex2html2"><SUP>2.2</SUP></A>
<DD>
	As a consequence, the list <tt>[1, 2]</tt> is considered equal
        to <tt>[1.0, 2.0]</tt>, and similar for tuples.

</DL>
<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html688"
 HREF="bitstring-ops.html">2.1.4.1 Bit-string Operations on Integer Types </A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="bitstring-ops.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="types.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="comparisons.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="bitstring-ops.html">2.1.4.1 Bit-string Operations on</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="types.html">2.1 Built-in Types</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="comparisons.html">2.1.3 Comparisons</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
