<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"ref.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"ref.html" title='Python Reference Manual'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"shifting.html" />
<link rel=
"prev" href=
"unary.html" />
<link rel=
"parent" href=
"expressions.html" />
<link rel=
"next" href=
"shifting.html" />
<meta name='aesop' content='information'
/>
<title>5.6 Binary arithmetic operations
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"5.5 Unary arithmetic operations"
href=
"unary.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"5. Expressions"
href=
"expressions.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"5.7 Shifting operations"
href=
"shifting.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Reference Manual
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"unary.html">5.5 Unary arithmetic operations
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"expressions.html">5. Expressions
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"shifting.html">5.7 Shifting operations
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION007600000000000000000"></A><A NAME=
"binary"></A>
5.6 Binary arithmetic operations
<a id='l2h-
414' xml:id='l2h-
414'
></a>
The binary arithmetic operations have the conventional priority
levels. Note that some of these operations also apply to certain
non-numeric types. Apart from the power operator, there are only two
levels, one for multiplicative operators and one for additive
<div class=
"productions">
<td><a id='tok-m_expr' xml:id='tok-m_expr'
>m_expr
</a></td>
<td><a class='grammartoken'
href=
"unary.html#tok-u_expr">u_expr
</a> |
<a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a> "*" <a class='grammartoken'
href=
"unary.html#tok-u_expr">u_expr
</a>
|
<a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a> "//" <a class='grammartoken'
href=
"unary.html#tok-u_expr">u_expr
</a>
|
<a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a> "/" <a class='grammartoken'
href=
"unary.html#tok-u_expr">u_expr
</a></td></tr>
<td><code>|
<a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a> "%" <a class='grammartoken'
href=
"unary.html#tok-u_expr">u_expr
</a></code></td></tr>
<td><a id='tok-a_expr' xml:id='tok-a_expr'
>a_expr
</a></td>
<td><a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a> |
<a class='grammartoken'
href=
"binary.html#tok-a_expr">a_expr
</a> "+" <a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a>
|
<a class='grammartoken'
href=
"binary.html#tok-a_expr">a_expr
</a> "-" <a class='grammartoken'
href=
"binary.html#tok-m_expr">m_expr
</a></td></tr>
<a class=
"grammar-footer"
href=
"grammar.txt" type=
"text/plain"
>Download entire grammar as text.
</a>
The
<code>*
</code> (multiplication) operator yields the product of its
arguments. The arguments must either both be numbers, or one argument
must be an integer (plain or long) and the other must be a sequence.
In the former case, the numbers are converted to a common type and
then multiplied together. In the latter case, sequence repetition is
performed; a negative repetition factor yields an empty sequence.
<a id='l2h-
416' xml:id='l2h-
416'
></a>
The
<code>/
</code> (division) and
<code>//
</code> (floor division) operators yield
the quotient of their arguments. The numeric arguments are first
converted to a common type. Plain or long integer division yields an
integer of the same type; the result is that of mathematical division
with the `floor' function applied to the result. Division by zero
<tt class=
"exception">ZeroDivisionError
</tt> exception.
<a id='l2h-
415' xml:id='l2h-
415'
></a><a id='l2h-
417' xml:id='l2h-
417'
></a>
The
<code>%
</code> (modulo) operator yields the remainder from the
division of the first argument by the second. The numeric arguments
are first converted to a common type. A zero right argument raises
the
<tt class=
"exception">ZeroDivisionError
</tt> exception. The arguments may be floating
point numbers, e.g.,
<code>3.14%
0.7</code> equals
<code>0.34</code> (since
<code>3.14</code> equals
<code>4*
0.7 +
0.34</code>.) The modulo operator always
yields a result with the same sign as its second operand (or zero);
the absolute value of the result is strictly smaller than the absolute
value of the second operand
<A NAME=
"tex2html5"
HREF=
"#foot4570"><SUP>5.2</SUP></A>.
<a id='l2h-
418' xml:id='l2h-
418'
></a>
The integer division and modulo operators are connected by the
following identity:
<code>x == (x/y)*y + (x%y)
</code>. Integer division and
modulo are also connected with the built-in function
<tt class=
"function">divmod()
</tt>:
<code>divmod(x, y) == (x/y, x%y)
</code>. These identities don't hold for
floating point numbers; there similar identities hold
approximately where
<code>x/y
</code> is replaced by
<code>floor(x/y)
</code> or
<code>floor(x/y) -
1</code><A NAME=
"tex2html6"
HREF=
"#foot4636"><SUP>5.3</SUP></A>.
<div class=
"versionnote"><b>Deprecated since release
2.3.
</b>
The floor division operator, the modulo operator,
and the
<tt class=
"function">divmod()
</tt> function are no longer defined for complex
numbers. Instead, convert to a floating point number using the
<tt class=
"function">abs()
</tt> function if appropriate.
</div><p></p>
The
<code>+
</code> (addition) operator yields the sum of its arguments.
The arguments must either both be numbers or both sequences of the
same type. In the former case, the numbers are converted to a common
type and then added together. In the latter case, the sequences are
<a id='l2h-
419' xml:id='l2h-
419'
></a>
The
<code>-
</code> (subtraction) operator yields the difference of its
arguments. The numeric arguments are first converted to a common
<a id='l2h-
420' xml:id='l2h-
420'
></a>
<BR><HR><H4>Footnotes
</H4>
<DT><A NAME=
"foot4570">... operand
</A><A
href=
"binary.html#tex2html5"><SUP>5.2</SUP></A></DT>
While
<code>abs(x%y)
< abs(y)
</code> is true mathematically, for
floats it may not be true numerically due to roundoff. For
example, and assuming a platform on which a Python float is an
IEEE
754 double-precision number, in order that
<code>-
1e-100 %
1e100
</code>
have the same sign as
<code>1e100
</code>, the computed result is
<code>-
1e-100 +
1e100
</code>, which is numerically exactly equal
to
<code>1e100
</code>. Function
<tt class=
"function">fmod()
</tt> in the
<tt class=
"module">math
</tt>
module returns a result whose sign matches the sign of the
first argument instead, and so returns
<code>-
1e-100</code> in this case.
Which approach is more appropriate depends on the application.
<DT><A NAME=
"foot4636">...
1</A><A
href=
"binary.html#tex2html6"><SUP>5.3</SUP></A></DT>
If x is very close to an exact integer multiple of y, it's
possible for
<code>floor(x/y)
</code> to be one larger than
<code>(x-x%y)/y
</code> due to rounding. In such cases, Python returns
the latter result, in order to preserve that
<code>divmod(x,y)[
0]
* y + x % y
</code> be very close to
<code>x
</code>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"5.5 Unary arithmetic operations"
href=
"unary.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"5. Expressions"
href=
"expressions.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"5.7 Shifting operations"
href=
"shifting.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Reference Manual
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"unary.html">5.5 Unary arithmetic operations
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"expressions.html">5. Expressions
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"shifting.html">5.7 Shifting operations
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.