ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 17 Sep 2017 21:37:07 +0200Sage 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 19:49:48 +0200https://ask.sagemath.org/question/38825/sage-range-function/Comment 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 20:06:12 +0200https://ask.sagemath.org/question/38825/sage-range-function/?comment=38826#post-id-38826Comment 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 20:28:54 +0200https://ask.sagemath.org/question/38825/sage-range-function/?comment=38827#post-id-38827Answer 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 21:16:13 +0200https://ask.sagemath.org/question/38825/sage-range-function/?answer=38828#post-id-38828Answer 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 21:16:57 +0200https://ask.sagemath.org/question/38825/sage-range-function/?answer=38829#post-id-38829Comment 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 21:19:03 +0200https://ask.sagemath.org/question/38825/sage-range-function/?comment=38830#post-id-38830Comment 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 21:37:07 +0200https://ask.sagemath.org/question/38825/sage-range-function/?comment=38832#post-id-38832