473,796 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 2102
On Oct 24, 5:44 pm, Michal Bozon <boz...@vscht.c zwrote:
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.l ang.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.c zwrote:
>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.c zwrote:
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.randrang e(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
1742
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 () function, "L", etc. It is a Python language implementation detail and I do not care about how it is done as long as it is fast and correct. Make python int adapt according to size of the integer and underlying architecture. 8088-80286=16 bit integers,...
8
2775
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 packets. Like this: l = tups =
7
1929
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 contents of 'row' without modifying the contents of 'table'. I used to fall into the newbie trap of 'row = table', but I have
23
9526
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 will print literally as {{-Hello}} World, instead of
6
2175
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. === PEP: xxx Title: Unification of for-statement and list-comprehension syntax
4
1813
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 lc expression as a for loop to drive the recursion. Thanks for any insight! Gregory
6
2008
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 tuple. Here is my example: In : a = (i for i in range(10)) In : b =
7
1516
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 value per pixel, and set it to something else if it meets or falls below a certain threshold - i.e., a Red value of 0 would be changed to 50. I've built my list by using a Python Image Library statement akin to the following:
0
1795
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 reasonable since generators require iteration and stop iteration and what not.
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9533
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10239
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10019
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7555
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4122
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.