[Tutor] for Loops with Squared

Narasimharao Nelluri narasimha928 at gmail.com
Sat May 23 13:34:39 EDT 2020


Hi Mary,

You can Below snippet to get the Desired Result.

In [7]: N= 3


In [8]: sum(num*num for num in range(N+1))

Out[8]: 14

Thanks
Narasimha.



On Sat, May 23, 2020 at 10:18 AM Mary Knauth via Tutor <tutor at python.org>
wrote:

> Hello!
>
> I am just diving into Python, and I’m having difficulty with for Loops
> right now.
>
> The exercise I am working on is:
> We will provide you with a value N. You should calculate the sum of each
> value of N squared from 0 up to and including N.
>
> In mathematical notation, this is written as  ∑Ni=0i2
>> i
> =
> 0
> N
> i
> 2
>
>
> So if we passed in 3, you would output  02+12+22+32=14
>
> My script is:
> # Get N from the command line
> import sys
> N= int(sys.argv[1])
>
>
>
> square = 0
>
> for i in range(0, N+1):
>   square = (i * i) + square
>   print(square)
>
>
> I’m pretty sure my mistake is in line 10, but I’m missing what I’m doing
> incorrectly.
>
> Thanks!
>
> Warm Regards,
>
> Mary
>
>
>
>  <https://zephyrsolutions.us/>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list