range syntax

C

Colin J. Williams

One of the little irritants of Python is that the range syntax is rather
long-winded:
[Dbg]>>> range(3, 20, 6)
[3, 9, 15]
[Dbg]>>>
It would be nice if one could have something like 3:20:6.

I've just come across the r_class in numpy which doesn't go that far
but does provide a generalization of the above:
[Dbg]>>> r_[1:6:2, 6:8, (10, 11, 13), [20,25]]
array([ 1, 3, 5, 6, 7, 10, 11, 13, 20, 25])
[Dbg]>>>

Colin W.
 
R

Roberto Bonvallet

Colin said:
One of the little irritants of Python is that the range syntax is rather
long-winded:
[Dbg]>>> range(3, 20, 6)
[3, 9, 15]
[Dbg]>>>
It would be nice if one could have something like 3:20:6.

In that case, how would the parser know which colon terminates the
'for' in the following example:

for i in 2:3:4:
...
 
A

Antoon Pardon

Colin said:
One of the little irritants of Python is that the range syntax is rather
long-winded:
[Dbg]>>> range(3, 20, 6)
[3, 9, 15]
[Dbg]>>>
It would be nice if one could have something like 3:20:6.

In that case, how would the parser know which colon terminates the
'for' in the following example:

for i in 2:3:4:
...

By the programmer using parenthesis. Like

for i in (2:3):
...


Just as you are supposed to use parthesis if you
want a tuple as a function argument although normally
a comma is enough to form a tuple.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top