title: Python: How to calculate the length of a range without creating the range?
body: <p>I need to calculate the length of a range, but ideally without creating the range, (hopefully will be faster and use less memory. This is important, because this function will be called a lot). The length is used to set an extended slice.</p>
  
  <p>Right now I have tried:</p>
  
  <pre><code>int_div = lambda n, d: (n + d // 2) // d
  
  def range_len(start, stop, step):
      return int_div(stop - start, step)
  </code></pre>
  
  <p>But on some cases, such as range_len(9, 100, 3) it gives 30 when the correct answer is 31. I feel like this should be simple, what am I doing wrong?</p>