<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Inclusive range() in Python</title>
	<atom:link href="http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/feed/" rel="self" type="application/rss+xml" />
	<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=inclusive-range-in-python</link>
	<description>my very own personal corner</description>
	<lastBuildDate>Fri, 06 Jan 2012 03:20:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Abhinav Sood</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-13398</link>
		<dc:creator>Abhinav Sood</dc:creator>
		<pubDate>Thu, 04 Aug 2011 19:43:34 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-13398</guid>
		<description>I&#039;d suggest that you create an iterator like this:
&lt;code&gt;
class inclusive_range():
    def __init__(self, *args):
        numargs = len(args)

        if numargs &lt; 1:
            raise TypeError(&#039;Inclusive Range requires at least 1 argument&#039;)

        elif numargs == 1:
            self.stop = args[0]
            self.start, self.step = 0, 1

        elif numargs == 2:
            (self.start, self.stop) = args
            self.step = 1

        elif numargs == 3:
            (self.start, self.stop, self.step) = args

        else:
            raise TypeError(&#039;Inclusive Range expects at the most 3 arguments, got {}&#039;.format(numargs))

    def __iter__(self):
        i = self.start
        while i &lt;= self.stop:
            yield i
            i += self.step
&lt;/code&gt;

Then you can use it like an iterable object like range(). For example, &lt;code&gt;
for i in inclusive_range(5,100,5): print(i, end = &#039; &#039;)
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;d suggest that you create an iterator like this:<br />
<code><br />
class inclusive_range():<br />
    def __init__(self, *args):<br />
        numargs =&nbsp;len(args)</p>
<p>        if numargs &lt; 1:<br />
            raise TypeError(&#039;Inclusive Range requires at least 1&nbsp;argument&#039;)</p>
<p>        elif numargs == 1:<br />
            self.stop = args[0]<br />
            self.start, self.step = 0,&nbsp;1</p>
<p>        elif numargs == 2:<br />
            (self.start, self.stop) = args<br />
            self.step =&nbsp;1</p>
<p>        elif numargs == 3:<br />
            (self.start, self.stop, self.step) =&nbsp;args</p>
<p>        else:<br />
            raise TypeError(&#039;Inclusive Range expects at the most 3 arguments, got&nbsp;{}&#039;.format(numargs))</p>
<p>    def __iter__(self):<br />
        i = self.start<br />
        while i &lt;= self.stop:<br />
            yield i<br />
            i += self.step<br />
</code></p>
<p>Then you can use it like an iterable object like range(). For example, <code><br />
for i in inclusive_range(5,100,5): print(i, end = ' ')<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Abhinav Sood</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-13370</link>
		<dc:creator>Abhinav Sood</dc:creator>
		<pubDate>Tue, 02 Aug 2011 11:54:10 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-13370</guid>
		<description>Nick&#039;s &lt;a href=&quot;http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-12398&quot; rel=&quot;nofollow&quot;&gt;solution&lt;/a&gt; will best serve the purpose and behaves exactly like range() of Python. That is, it can be passed 1, 2 or 3 arguments just like the range(). Anything else would raise an appropriate exception.

Example:
inclusive_range(25) for range from 0 to 25 inclusive with default step of 1.
inclusive_range(10,25) for range from 10 to 25 inclusive with default step of 1.
inclusive_range(10,25,5) for range from 10 to 25 inclusive with a custom step of 5.

inclusive_range and inclusive_range(1,10,1,32) will raise exceptions.</description>
		<content:encoded><![CDATA[<p>Nick&#8217;s <a href="http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-12398" rel="nofollow">solution</a> will best serve the purpose and behaves exactly like range() of Python. That is, it can be passed 1, 2 or 3 arguments just like the range(). Anything else would raise an appropriate&nbsp;exception.</p>
<p>Example:<br />
inclusive_range(25) for range from 0 to 25 inclusive with default step of 1.<br />
inclusive_range(10,25) for range from 10 to 25 inclusive with default step of 1.<br />
inclusive_range(10,25,5) for range from 10 to 25 inclusive with a custom step of&nbsp;5.</p>
<p>inclusive_range and inclusive_range(1,10,1,32) will raise&nbsp;exceptions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geo</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-12738</link>
		<dc:creator>Geo</dc:creator>
		<pubDate>Thu, 30 Jun 2011 09:29:33 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-12738</guid>
		<description>&lt;code&gt;
def inclusive_range(start, stop=None, step=1):
    if stop == None:
        start, stop = 0, start

    extension = -1 if step &lt; 0 else 1

    return range(start, stop + extension, step)
&lt;/code&gt;

It&#039;ll differ from range() in that a second argument of None will cause it to do a [0..N] range as in one argument. It&#039;ll also throw somewhat different errors if you feed it strings or similar, but so do most of the other examples.

The other code probably communicates better, but this was fun to try to write concisely!</description>
		<content:encoded><![CDATA[<p><code><br />
def inclusive_range(start, stop=None, step=1):<br />
    if stop == None:<br />
        start, stop = 0,&nbsp;start</p>
<p>    extension = -1 if step &lt; 0 else&nbsp;1</p>
<p>    return range(start, stop + extension, step)<br />
</code></p>
<p>It&#8217;ll differ from range() in that a second argument of None will cause it to do a [0..N] range as in one argument. It&#8217;ll also throw somewhat different errors if you feed it strings or similar, but so do most of the other&nbsp;examples.</p>
<p>The other code probably communicates better, but this was fun to try to write&nbsp;concisely!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nick</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-12398</link>
		<dc:creator>Nick</dc:creator>
		<pubDate>Wed, 08 Jun 2011 10:12:04 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-12398</guid>
		<description>&lt;code&gt;
def inclusive_range(*args):
    numargs = len(args)
    if numargs &lt; 1:
        raise TypeError(&quot;irange requires at least one argument.&quot;)
    elif numargs == 1:
        stop = args[0];
        start = 0;
        step = 1;
    elif numargs == 2:
        (start,stop) = args
        step = 1
    elif numargs == 3:
        (start,stop,step) = args
    else:
        raise TypeError(&quot;irange expected at most 3 arguments, got %s&quot; % (str(numargs)))
    i = start
    if step == 0:
        step = 1
    while i &lt;= stop:
        yield i
        i += step
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
def inclusive_range(*args):<br />
    numargs = len(args)<br />
    if numargs &lt; 1:<br />
        raise TypeError(&quot;irange requires at least one argument.&quot;)<br />
    elif numargs == 1:<br />
        stop = args[0];<br />
        start = 0;<br />
        step = 1;<br />
    elif numargs == 2:<br />
        (start,stop) = args<br />
        step = 1<br />
    elif numargs == 3:<br />
        (start,stop,step) = args<br />
    else:<br />
        raise TypeError(&quot;irange expected at most 3 arguments, got %s&quot; % (str(numargs)))<br />
    i = start<br />
    if step == 0:<br />
        step = 1<br />
    while i &lt;= stop:<br />
        yield i<br />
        i += step<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-3643</link>
		<dc:creator>Jon</dc:creator>
		<pubDate>Fri, 19 Jun 2009 19:16:25 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-3643</guid>
		<description>I think Chris&#039;s definition is not quite right. Consider these calls:

&lt;code&gt;
range(1, 5, 3) =&gt; [1, 4]
inclusive_range(1, 5, 3) =&gt; [1, 4, 7]
&lt;/code&gt;

The inclusive range shouldn&#039;t include 7, since it is strictly greater than 5.

I think this is one way to define the interface with the same semantics as the range() built-in other than changing the right bound to be inclusive:

&lt;code&gt;
def inclusive_range(*args):
    if len(args) == 1:
        start = 0
        stop = args[0]
        step = 1
    elif len(args) == 2:
        (start, stop) = args
        step = 1
    elif len(args) == 3:
        (start, stop, step) = args
    else:
        raise TypeError(&quot;Expected 1-3 args, got&quot;, len(args))
    if step &gt; 0:
        return range(start, stop + 1, step)
    else:
        return range(start, stop - 1, step)
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>I think Chris&#8217;s definition is not quite right. Consider these&nbsp;calls:</p>
<p><code><br />
range(1, 5, 3) =&gt; [1, 4]<br />
inclusive_range(1, 5, 3) =&gt; [1, 4, 7]<br />
</code></p>
<p>The inclusive range shouldn&#8217;t include 7, since it is strictly greater than&nbsp;5.</p>
<p>I think this is one way to define the interface with the same semantics as the range() built-in other than changing the right bound to be&nbsp;inclusive:</p>
<p><code><br />
def inclusive_range(*args):<br />
    if len(args) == 1:<br />
        start = 0<br />
        stop = args[0]<br />
        step = 1<br />
    elif len(args) == 2:<br />
        (start, stop) = args<br />
        step = 1<br />
    elif len(args) == 3:<br />
        (start, stop, step) = args<br />
    else:<br />
        raise TypeError("Expected 1-3 args, got", len(args))<br />
    if step &gt; 0:<br />
        return range(start, stop + 1, step)<br />
    else:<br />
        return range(start, stop - 1, step)<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kenny Katzgrau</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-219</link>
		<dc:creator>Kenny Katzgrau</dc:creator>
		<pubDate>Sat, 16 Aug 2008 14:17:18 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-219</guid>
		<description>@ Comments 1 &amp; 2:

Lol. That&#039;s great -- been there.</description>
		<content:encoded><![CDATA[<p>@ Comments 1 &amp;&nbsp;2:</p>
<p>Lol. That&#8217;s great&thinsp;&#8212;&thinsp;been&nbsp;there.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wes Turner</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-177</link>
		<dc:creator>Wes Turner</dc:creator>
		<pubDate>Thu, 05 Jun 2008 04:54:16 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-177</guid>
		<description>irange = lambda x,y,z=1: range(x,y+1,z)</description>
		<content:encoded><![CDATA[<p>irange = lambda x,y,z=1:&nbsp;range(x,y+1,z)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Brinker</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-156</link>
		<dc:creator>Chris Brinker</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:21:26 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-156</guid>
		<description>Dern lack of preview :)
&lt;code&gt;
def inclusive_range(start, stop, step=1):
    return(range(start, stop+step, step))
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Dern lack of preview :)<br />
<code><br />
def inclusive_range(start, stop, step=1):<br />
    return(range(start, stop+step, step))<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Brinker</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-155</link>
		<dc:creator>Chris Brinker</dc:creator>
		<pubDate>Tue, 22 Apr 2008 20:20:29 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-155</guid>
		<description>You may want to consider a situation with a negative step as well, in which case it is just better to add the step (positive or negative) to the stop:

def inclusive_range(start, stop, step=1):
return(range(start, stop+step, step))

-Chris</description>
		<content:encoded><![CDATA[<p>You may want to consider a situation with a negative step as well, in which case it is just better to add the step (positive or negative) to the&nbsp;stop:</p>
<p>def inclusive_range(start, stop, step=1):<br />
return(range(start, stop+step,&nbsp;step))</p>
<p>-Chris</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Giuliani Vito, Ivan</title>
		<link>http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/comment-page-1/#comment-130</link>
		<dc:creator>Giuliani Vito, Ivan</dc:creator>
		<pubDate>Thu, 06 Mar 2008 22:41:51 +0000</pubDate>
		<guid isPermaLink="false">http://zeta-puppis.com/2008/03/06/inclusive-range-in-python/#comment-130</guid>
		<description>goddamn... this is what happens when coding 8 hours straight on...</description>
		<content:encoded><![CDATA[<p>goddamn&#8230; this is what happens when coding 8 hours straight&nbsp;on&#8230;</p>
]]></content:encoded>
	</item>
</channel>
</rss>

