# Sage Range function

asked 2017-09-17 12:49:48 -0600

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I use sage range function to print numbers form 1 to say 10 using print [1..10]. How to print only even numbers using this?

edit retag close merge delete

1

Is the following ok?

sage: print range(2,11,2)
[2, 4, 6, 8, 10]

( 2017-09-17 13:06:12 -0600 )edit

I knew that. I was wondering if same can be done with [1..10]?

( 2017-09-17 13:28:54 -0600 )edit

Sort by ยป oldest newest most voted

The ellipsis operator looks at the previous two numbers to guess the step.

sage: [2, 4 .. 10]
[2, 4, 6, 8, 10]

more

wow, i didn't know that works!

( 2017-09-17 14:19:03 -0600 )edit

Thanks....

( 2017-09-17 14:37:07 -0600 )edit

using list comprehension:

sage: [i for i in [1..10] if i%2==0]
[2, 4, 6, 8, 10]


see more alternatives in the Programming in Python and Sage Thematic Tutorial.

more

Please start posting anonymously - your entry will be published after you log in or create a new account.

## Stats

Asked: 2017-09-17 12:49:48 -0600

Seen: 1,291 times

Last updated: Sep 17 '17