Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python (1..n) syntax?

Tags:

python

sage

I see in the code on this Sage wiki page the following code:

@interact
def _(order=(1..12)):

Is this (1..n) syntax unique to Sage or is it something in Python? Also, what does it do?

like image 828
exupero Avatar asked Aug 18 '10 11:08

exupero


3 Answers

It's Sage-specific. You can use preparse to see how it is desugared to:

sage: preparse("(1..12)")
'(ellipsis_iter(Integer(1),Ellipsis,Integer(12)))'

See here for documentation of ellipsis_iter, here for information on the preparser.

like image 195
sdcvvc Avatar answered Sep 28 '22 16:09

sdcvvc


There was a Python PEP to add this notation to Python, but it was rejected. Robert Bradshaw decided to implement it anyways, but for the Sage preparser. He implemented the following:

  • (a..b) -- like xrange, so an iterator

  • [a..b] -- list, including endpoints

  • [a,b,..,c] -- arithmetic progression

like image 36
William Stein Avatar answered Sep 28 '22 15:09

William Stein


This is not Python syntax. I would guess that it creates a range from 1 to 12.

like image 23
Mark Byers Avatar answered Sep 28 '22 17:09

Mark Byers