First time here? Check out the FAQ!

Ask Your Question
1

Sage Range function

asked 7 years ago

this post is marked as community wiki

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?

Preview: (hide)

Comments

1

Is the following ok?

sage: print range(2,11,2)
[2, 4, 6, 8, 10]
dan_fulea gravatar imagedan_fulea ( 7 years ago )

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

Manoj gravatar imageManoj ( 7 years ago )

2 Answers

Sort by » oldest newest most voted
4

answered 7 years ago

slelievre gravatar image

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

sage: [2, 4 .. 10]
[2, 4, 6, 8, 10]
Preview: (hide)
link

Comments

wow, i didn't know that works!

mforets gravatar imagemforets ( 7 years ago )

Thanks....

Manoj gravatar imageManoj ( 7 years ago )
0

answered 7 years ago

mforets gravatar image

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.

Preview: (hide)
link

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 7 years ago

Seen: 3,723 times

Last updated: Sep 17 '17