Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An expression like 'x % y' evaluates to the remainder of 'x / y'. Precedence rules are like '/' and '*'.</p> <pre><code>&gt;&gt;&gt; 9 / 2 4 &gt;&gt;&gt; 9 % 2 1 </code></pre> <ul> <li>9 divided by 2 is equal to 4.</li> <li>4 times 2 is 8</li> <li>9 minus 8 is 1 - the remainder.</li> </ul> <p><strong>Python gotcha</strong>: depending on the Python version you are using, <code>%</code> is also the (deprecated) string interpolation operator, so watch out if you are coming from a language with automatic type casting (like PHP or JS) where an expression like <code>'12' % 2 + 3</code> is legal: in Python it will result in <code>TypeError: not all arguments converted during string formatting</code> which probably will be pretty confusing for you.</p> <p><strong>[update for Python 3]</strong></p> <p>User n00p comments:</p> <blockquote> <p>9/2 is 4.5 in python. You have to do integer division like so: 9//2 if you want python to tell you how many whole objects is left after division(4).</p> </blockquote> <p>To be precise, integer division used to be the default in Python 2:</p> <pre><code>$ python2.7 Python 2.7.10 (default, Oct 6 2017, 22:29:07) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; 9 / 2 4 &gt;&gt;&gt; 9 // 2 4 &gt;&gt;&gt; 9 % 2 1 </code></pre> <p>In Python 3 now <code>9 / 2</code> results <code>4.5</code> indeed, but keep in mind the original answer is very old.</p> <pre><code>$ python3.6 Python 3.6.1 (default, Apr 27 2017, 00:15:59) [GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; 9 / 2 4.5 &gt;&gt;&gt; 9 // 2 4 &gt;&gt;&gt; 9 % 2 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload