ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 17 Sep 2017 14:37:07 -0500Sage Range functionhttps://ask.sagemath.org/question/38825/sage-range-function/ 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?Sun, 17 Sep 2017 12:49:48 -0500https://ask.sagemath.org/question/38825/sage-range-function/Comment by Manoj for <p>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?</p>
https://ask.sagemath.org/question/38825/sage-range-function/?comment=38827#post-id-38827I knew that. I was wondering if same can be done with [1..10]?Sun, 17 Sep 2017 13:28:54 -0500https://ask.sagemath.org/question/38825/sage-range-function/?comment=38827#post-id-38827Comment by dan_fulea for <p>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?</p>
https://ask.sagemath.org/question/38825/sage-range-function/?comment=38826#post-id-38826Is the following ok?
sage: print range(2,11,2)
[2, 4, 6, 8, 10]Sun, 17 Sep 2017 13:06:12 -0500https://ask.sagemath.org/question/38825/sage-range-function/?comment=38826#post-id-38826Answer by slelievre for <p>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?</p>
https://ask.sagemath.org/question/38825/sage-range-function/?answer=38829#post-id-38829The ellipsis operator looks at the previous two numbers to guess the step.
sage: [2, 4 .. 10]
[2, 4, 6, 8, 10]
Sun, 17 Sep 2017 14:16:57 -0500https://ask.sagemath.org/question/38825/sage-range-function/?answer=38829#post-id-38829Comment by Manoj for <p>The ellipsis operator looks at the previous two numbers to guess the step.</p>
<pre><code>sage: [2, 4 .. 10]
[2, 4, 6, 8, 10]
</code></pre>
https://ask.sagemath.org/question/38825/sage-range-function/?comment=38832#post-id-38832Thanks....Sun, 17 Sep 2017 14:37:07 -0500https://ask.sagemath.org/question/38825/sage-range-function/?comment=38832#post-id-38832Comment by mforets for <p>The ellipsis operator looks at the previous two numbers to guess the step.</p>
<pre><code>sage: [2, 4 .. 10]
[2, 4, 6, 8, 10]
</code></pre>
https://ask.sagemath.org/question/38825/sage-range-function/?comment=38830#post-id-38830wow, i didn't know that works!Sun, 17 Sep 2017 14:19:03 -0500https://ask.sagemath.org/question/38825/sage-range-function/?comment=38830#post-id-38830Answer by mforets for <p>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?</p>
https://ask.sagemath.org/question/38825/sage-range-function/?answer=38828#post-id-38828using 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](http://doc.sagemath.org/html/en/thematic_tutorials/tutorial-programming-python.html#data-structures). Sun, 17 Sep 2017 14:16:13 -0500https://ask.sagemath.org/question/38825/sage-range-function/?answer=38828#post-id-38828