{article Examples__Python 3.4 }{title} {text} {/article}

Math Order of operations


PEMDAS

  1. Parentheses first

  2. Exponents (ie Powers and Square Roots, etc.)

  3. Multiplication and Division (left-to-right)

  4. Addition and Subtraction (left-to-right)


Divide and Multiply rank equally (and go left to right).

Add and Subtract rank equally (and go left to right)

This means that you should do what is possible within parentheses first, then exponents, then multiplication and division (from left to right), and then addition and subtraction (from left to right). If parentheses are enclosed within other parentheses, work from the inside out.

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.

  • >>> # Example One
  • >>> 7 + 3 * 5
    22 # Answer
    >>> 3 * 5 # Multiplication and Division (left-to-right)
    15
    >>> 15 + 7 # Addition and Subtraction (left-to-right)
    22 # Answer
    >>> # Example Two
    >>> (7 + 3) * 4 / 2 - 5 * 6
    -10.0 # Answer
    >>> (7 + 3) # Parentheses first
    10
    >>> 10 * 4 # Multiplication and Division (left-to-right)
    40
    >>> 40 / 2 # Multiplication and Division (left-to-right)
    20.0
    >>> 5 * 6 # Multiplication and Division (left-to-right)
    30
    >>> 20 - 30 # Addition and Subtraction (left-to-right)
    -10 # Answer
    >>>
    >>> # Example Three
    >>> 4 * 2 / 3 * 2
    5.333333333333333 # Answer
    >>> 4 * 2 # Multiplication and Division (left-to-right)
    8
    >>> 8 / 3 # Multiplication and Division (left-to-right)
    2.6666666666666665
    >>> 2.6666666666666665 * 2 # Multiplication and Division (left-to-right)
    5.333333333333333 # Answer
    >>>

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> # Example One
>>> 7 + 3 * 5
22 # Answer
>>> 3 * 5 # Multiplication and Division (left-to-right)
15
>>> 15 + 7 # Addition and Subtraction (left-to-right)
22 # Answer
>>> # Example Two
>>> (7 + 3) * 4 / 2 - 5 * 6
-10.0 # Answer
>>> (7 + 3) # Parentheses first
10
>>> 10 * 4 # Multiplication and Division (left-to-right)
40
>>> 40 / 2 # Multiplication and Division (left-to-right)
20.0
>>> 5 * 6 # Multiplication and Division (left-to-right)
30
>>> 20 - 30 # Addition and Subtraction (left-to-right)
-10 # Answer
>>>
>>> # Example Three
>>> 4 * 2 / 3 * 2
5.333333333333333 # Answer
>>> 4 * 2 # Multiplication and Division (left-to-right)
8
>>> 8 / 3 # Multiplication and Division (left-to-right)
2.6666666666666665
>>> 2.6666666666666665 * 2 # Multiplication and Division (left-to-right)
5.333333333333333 # Answer
>>>




</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}