<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>5.6 Binary arithmetic operations</title> <META NAME="description" CONTENT="5.6 Binary arithmetic operations"> <META NAME="keywords" CONTENT="ref"> <META NAME="resource-type" CONTENT="document"> <META NAME="distribution" CONTENT="global"> <meta http-equiv="Content-Type" content="text/html; charset="> <link rel="STYLESHEET" href="ref.css"> <link rel="first" href="ref.html"> <link rel="contents" href="contents.html" title="Contents"> <link rel="index" href="genindex.html" title="Index"> <LINK REL="next" href="shifting.html"> <LINK REL="previous" href="unary.html"> <LINK REL="up" href="expressions.html"> <LINK REL="next" href="shifting.html"> </head> <body> <DIV CLASS="navigation"> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="unary.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="expressions.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="shifting.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Reference Manual</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="unary.html">5.5 Unary arithmetic operations</A> <b class="navlabel">Up:</b> <a class="sectref" href="expressions.html">5. Expressions</A> <b class="navlabel">Next:</b> <a class="sectref" href="shifting.html">5.7 Shifting operations</A> <br><hr> </DIV> <!--End of Navigation Panel--> <H1><A NAME="SECTION007600000000000000000"> </A> <BR> 5.6 Binary arithmetic operations </H1> <a name="l2h-272"> </a> <P> 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 operators: <P> <dl><dd class="grammar"> <div class="productions"> <table cellpadding="2"> <tr valign="baseline"> <td><code><a name="tok-m_expr">m_expr</a></code></td> <td> ::= </td> <td><code><a href="unary.html#tok-u_expr">u_expr</a> | <a href="node57.html#tok-m_expr">m_expr</a> "*" <a href="unary.html#tok-u_expr">u_expr</a> | <a href="node57.html#tok-m_expr">m_expr</a> "/" <a href="unary.html#tok-u_expr">u_expr</a> | <a href="node57.html#tok-m_expr">m_expr</a> "%" <a href="unary.html#tok-u_expr">u_expr</a></code></td></tr> <tr valign="baseline"> <td><code><a name="tok-a_expr">a_expr</a></code></td> <td> ::= </td> <td><code><a href="binary.html#tok-m_expr">m_expr</a> | <a href="">aexpr</a> "+" <a href="binary.html#tok-m_expr">m_expr</a> <a href="">aexpr</a> "-" <a href="binary.html#tok-m_expr">m_expr</a></code></td></tr> </table> </div> <a class="grammar-footer" href="grammar.txt" type="text/plain" >Download entire grammar as text.</a> </dd></dl> <P> 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 name="l2h-273"> </a> <P> The <code>/</code> (division) operator yields the quotient of its 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 raises the <tt class="exception">ZeroDivisionError</tt> exception. <a name="l2h-274"> </a> <P> 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 second operand. <a name="l2h-275"> </a> <P> 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 and complex 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> (for floats),<A NAME="tex2html4" HREF="#foot3686"><SUP>5.2</SUP></A> or <code>floor((x/y).real)</code> (for complex). <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 concatenated. <a name="l2h-276"> </a> <P> The <code>-</code> (subtraction) operator yields the difference of its arguments. The numeric arguments are first converted to a common type. <a name="l2h-277"> </a> <P> <BR><HR><H4>Footnotes</H4> <DL> <DT><A NAME="foot3686">... floats),</A><A NAME="foot3686" href="binary.html#tex2html4"><SUP>5.2</SUP></A> <DD> 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>. </DL> <DIV CLASS="navigation"> <p><hr> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td><A href="unary.html"><img src="../icons/previous.gif" border="0" height="32" alt="Previous Page" width="32"></A></td> <td><A href="expressions.html"><img src="../icons/up.gif" border="0" height="32" alt="Up One Level" width="32"></A></td> <td><A href="shifting.html"><img src="../icons/next.gif" border="0" height="32" alt="Next Page" width="32"></A></td> <td align="center" width="100%">Python Reference Manual</td> <td><A href="contents.html"><img src="../icons/contents.gif" border="0" height="32" alt="Contents" width="32"></A></td> <td><img src="../icons/blank.gif" border="0" height="32" alt="" width="32"></td> <td><A href="genindex.html"><img src="../icons/index.gif" border="0" height="32" alt="Index" width="32"></A></td> </tr></table> <b class="navlabel">Previous:</b> <a class="sectref" href="unary.html">5.5 Unary arithmetic operations</A> <b class="navlabel">Up:</b> <a class="sectref" href="expressions.html">5. Expressions</A> <b class="navlabel">Next:</b> <a class="sectref" href="shifting.html">5.7 Shifting operations</A> <hr> <span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>