473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

[0..9] list (range) syntax

many Python newcomers are confused why
range(10), does not include 10.

If there was a proposal for the new
syntax for ranges, which is known
e.g. from Pascal or Ruby...
>>[0..10]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

....is there a chance to be approved ?

We have had a short discussion on it
at the irc, I hope that the fact
that nobody agreed it is a good idea
was just an accident :)

-m.

PS:
to dream further..
>>(0..10)
<generator object at 0xb7618dac>

or
>>(0..10)
(0..10)

or
>>(0..10)
range(0, 11)
Oct 24 '07 #1
4 2082
On Oct 24, 5:44 pm, Michal Bozon <boz...@vscht.czwrote:
many Python newcomers are confused why
range(10), does not include 10.
How can they be confused?

Does base 10 have a digit ten?
Does base 2 have a digit two?
Does base 16 have a digit sixteen?

Haven't you stopped counting on your fingers when
you leave grade school?
>
If there was a proposal for the new
syntax for ranges, which is known
e.g. from Pascal or Ruby...
>[0..10]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

...is there a chance to be approved ?
"These go to 11."
>
We have had a short discussion on it
at the irc, I hope that the fact
that nobody agreed it is a good idea
was just an accident :)
Wait a minute...comp.lang.python. I must have
accidentally gotten into the wrong newsgroup.
>
-m.

PS:
to dream further..
>(0..10)

<generator object at 0xb7618dac>

or
>(0..10)

(0..10)

or
>(0..10)

range(0, 11)

Oct 24 '07 #2
On Wed, 24 Oct 2007 16:28:20 -0700, me********@aol.com wrote:
On Oct 24, 5:44 pm, Michal Bozon <boz...@vscht.czwrote:
>many Python newcomers are confused why range(10), does not include 10.

How can they be confused?
Because in common English, counting starts at 1 and ranges normally
include both end points (that is, it is a "closed" interval). If you say
"I'll be away from the 4th to the 7th" and then turn up on the 7th,
nearly everyone will wonder why you're back a day early.

Does base 10 have a digit ten?
Does base 2 have a digit two?
Does base 16 have a digit sixteen?

Haven't you stopped counting on your fingers when you leave grade
school?
range(N) is not designed to return the digits used in base N strings, and
any connection is weak (what base does range(3, 25, 4) correspond to?).
What little correspondence there is is an accidental coincidence of how
we write numbers, not a fundamental fact about half-open intervals like
range().

Having said that, and regardless of common English usage, using half-open
intervals almost always leads to simpler programming and fewer errors.
Creating syntax support for beginners to make extra off-by-one bugs is a
terrible idea, no matter how seductive it seems for newbies. I know Ruby
has (to my mind confusing!) support for both closed and half-open
intervals, but I wonder how useful it is in practice?

Ruby expression =Python equivalent

1..5 =range(1, 6)
1...5 =range(1, 5)

--
Steven.
Oct 25 '07 #3
On Oct 24, 6:44 pm, Michal Bozon <boz...@vscht.czwrote:
many Python newcomers are confused why
range(10), does not include 10.

If there was a proposal for the new
syntax for ranges, which is known
e.g. from Pascal or Ruby...
>[0..10]

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

...is there a chance to be approved ?

We have had a short discussion on it
at the irc, I hope that the fact
that nobody agreed it is a good idea
was just an accident :)

No, sorry, it's just not going to happen.

In fact, the opposite is happening. Python is removing the few cases
that use a closed interval. For example, random.randint(a,b), which
returned a random integer in range a to b inclusive, was deprecated in
favor of random.randrange(a,b), which generates a random integer in
range a to b, not inclusive.
The advantages of open ranges are especially apparent to someone like
me, who uses Python at home, then has to go to work and Matlab. It's
not that I accidentally use open intervals a lot (I've gotten into the
habit of making sure I am using closed intervals whenever my tension
level is high :), but all the extra "+1"s and "-1"s that I need when
splicing arrays gets pretty old.
Carl Banks

Oct 25 '07 #4
Michal Bozon wrote:
The .. syntax was not meant only as something
which would include the last item,
but also/rather a range list syntactic shortcut:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -->
[0, 1, ... 9, 10] -->
[0..10]
OK, I see.

But I still fail to see where this is useful. All these 3 statements
create a list from 0 to 10 including.

If, however, the ".." operator would recognize patterns before and after
itself, I could see your point (e.g. [0, 1, 2, 4, 8, .. 128, 256, 512]).
Buts thats pretty academic, maybe good for a specialized computation
language.

And I feel that my "write a function" argument still holds.

/W
Oct 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: nnes | last post by:
I have really no mayor gripes, and the few things I would change would break backward compatibility, but here is the list anyway: 1.) Eliminate the whole long stuff. E.g. built-in long ()...
8
by: Nickolay Kolev | last post by:
Hi all, I have a list whose length is a multiple of 3. I want to get a list of tuples each of which has 3 consecutive elements from the original list, thus packing the list into smaller...
7
by: Frank Millman | last post by:
Hi all Assume a 2-dimensional list called 'table' - conceptually think of it as rows and columns. Assume I want to create a temporary copy of a row called 'row', allowing me to modify the...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
4
by: Gregory Guthrie | last post by:
Sorry for a simple question- but I don't understand how to parse this use of a list comprehension. The "or" clauses are odd to me. It also seems like it is being overly clever (?) in using a...
6
by: fdu.xiaojf | last post by:
Hi all, I can use list comprehension to create list quickly. So I expected that I can created tuple quickly with the same syntax. But I found that the same syntax will get a generator, not a...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
0
by: Hatem Nassrat | last post by:
on Wed Jun 13 10:17:24 CEST 2007, Diez B. Roggisch deets at nospam.web.de wrote: Well I have looked into this and it seems that using the list comprehension is faster, which is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.