473,289 Members | 1,963 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,289 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 2075
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...

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.