title: How should the interquartile range be calculated in Python?
body: <p>I have a list of numbers <code>[1, 2, 3, 4, 5, 6, 7]</code> and I want to have a function to return the interquartile range of this list of numbers. The interquartile range is the difference between the upper and lower quartiles. I have attempted to calculate the interquartile range using NumPy functions and using Wolfram Alpha. I find all of the answers, from my manual one, to the NumPy one, tothe Wolfram Alpha, to be different. I do not know why this is.</p>
  
  <p>My attempt in Python is as follows:</p>
  
  <pre><code>&gt;&gt;&gt; a = numpy.array([1, 2, 3, 4, 5, 6, 7])
  &gt;&gt;&gt; numpy.percentile(a, 25)
  2.5
  &gt;&gt;&gt; numpy.percentile(a, 75)
  5.5
  &gt;&gt;&gt; numpy.percentile(a, 75) - numpy.percentile(a, 25) # IQR
  3.0
  </code></pre>
  
  <p>My attempt in Wolfram Alpha is as follows:</p>
  
  <ul>
  <li><a href="http://www.wolframalpha.com/input/?i=first+quartile+1%2C+2%2C+3%2C+4%2C+5%2C+6%2C+7" rel="noreferrer">"first quartile 1, 2, 3, 4, 5, 6, 7": 2.25</a></li>
  <li><a href="http://www.wolframalpha.com/input/?i=third+quartile+1%2C+2%2C+3%2C+4%2C+5%2C+6%2C+7" rel="noreferrer">"third quartile 1, 2, 3, 4, 5, 6, 7": 5.75</a></li>
  <li>(comment: 5.75 - 2.25 = 3.5)</li>
  <li><a href="https://www.wolframalpha.com/input/?i=interquartile+range+1%2C+2%2C+3%2C+4%2C+5%2C+6%2C+7" rel="noreferrer">"interquartile range 1, 2, 3, 4, 5, 6, 7": ~3.5</a></li>
  </ul>
  
  <p>So, I find that the values returned by NumPy and Wolfram Alpha for what I think are the first quartile, the third quartile and the interquartile range are not consistent. Why is this? What should I be doing in Python to calculate the interquartile range correctly?</p>
  
  <p>As far as I am aware, the interquartile range of <code>[1, 2, 3, 4, 5, 6, 7]</code> should be the following:</p>
  
  <pre><code>median(5, 6, 7) - median(1, 2, 3) = 4.
  </code></pre>