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.Fri, 08 Oct 2021 22:40:44 +0200other way of creating a list of perfect squareshttps://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/ Hi there! I am currently explaining a certain exercise to my buddy and part of it consists in creating a list of perfect squares up to root of 64 (included) therefore the list has to look like this [0, 1, 4, 9, 16, 25, 36, 49, 64]
My first idea was this
squares=[i*i for i in range(sqrt(64)+1)]
which works but I am now trying to do it in a way so that is not compressed such us
for i in range(sqrt(64)+1)
squares=[i*i]
print(squares)
But I keep getting this error
for i in range(sqrt(Integer(64))+Integer(1))
^
SyntaxError: invalid syntax
Any ideas of what I am doing wrong?
Thu, 07 Oct 2021 12:34:38 +0200https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/Answer by tmonteil for <p>Hi there! I am currently explaining a certain exercise to my buddy and part of it consists in creating a list of perfect squares up to root of 64 (included) therefore the list has to look like this [0, 1, 4, 9, 16, 25, 36, 49, 64]
My first idea was this </p>
<pre><code>squares=[i*i for i in range(sqrt(64)+1)]
</code></pre>
<p>which works but I am now trying to do it in a way so that is not compressed such us </p>
<pre><code> for i in range(sqrt(64)+1)
squares=[i*i]
print(squares)
</code></pre>
<p>But I keep getting this error </p>
<pre><code> for i in range(sqrt(Integer(64))+Integer(1))
^
SyntaxError: invalid syntax
</code></pre>
<p>Any ideas of what I am doing wrong?</p>
https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?answer=59270#post-id-59270In a `for` loop, the line must end with a colon `:`
By the way, here are some hints for the exercise:
- if `L` is a list, you can add an element to `L` with `L.append(element)`
- the empty list is denoted by `[]`
- if you want to avoid the use of the square root, you can replace the `for` loop with a `while` loopThu, 07 Oct 2021 13:05:22 +0200https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?answer=59270#post-id-59270Comment by jhonvi2 for <p>In a <code>for</code> loop, the line must end with a colon <code>:</code></p>
<p>By the way, here are some hints for the exercise:</p>
<ul>
<li>if <code>L</code> is a list, you can add an element to <code>L</code> with <code>L.append(element)</code></li>
<li>the empty list is denoted by <code>[]</code></li>
<li>if you want to avoid the use of the square root, you can replace the <code>for</code> loop with a <code>while</code> loop</li>
</ul>
https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59294#post-id-59294@tmonteil ok so I could do something like
n =95
cuadrados=[ ]
i=0
root= int(sqrt(n)+1)
while i < root:
cuadrados.append(i*i)
but it does not seem to work I keep getting an error in `cuadrados.append(i*i)`Fri, 08 Oct 2021 22:40:44 +0200https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59294#post-id-59294Comment by tmonteil for <p>In a <code>for</code> loop, the line must end with a colon <code>:</code></p>
<p>By the way, here are some hints for the exercise:</p>
<ul>
<li>if <code>L</code> is a list, you can add an element to <code>L</code> with <code>L.append(element)</code></li>
<li>the empty list is denoted by <code>[]</code></li>
<li>if you want to avoid the use of the square root, you can replace the <code>for</code> loop with a <code>while</code> loop</li>
</ul>
https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59275#post-id-59275In a for loop, you have to know in advance where to stop, in a while loop, you test on-the-fly.Thu, 07 Oct 2021 20:15:36 +0200https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59275#post-id-59275Comment by jhonvi2 for <p>In a <code>for</code> loop, the line must end with a colon <code>:</code></p>
<p>By the way, here are some hints for the exercise:</p>
<ul>
<li>if <code>L</code> is a list, you can add an element to <code>L</code> with <code>L.append(element)</code></li>
<li>the empty list is denoted by <code>[]</code></li>
<li>if you want to avoid the use of the square root, you can replace the <code>for</code> loop with a <code>while</code> loop</li>
</ul>
https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59272#post-id-59272Hi! thank you so much for your help my code is better now! I just do not really understand your last statement 'if you want to avoid the use of the square root, you can replace the for loop with a while loop'
Isn't it the same the for loop and the wwhile loop?Thu, 07 Oct 2021 18:36:02 +0200https://ask.sagemath.org/question/59269/other-way-of-creating-a-list-of-perfect-squares/?comment=59272#post-id-59272