list - Python - For Loop That Prints Each Number And Its Square on a Separate Line -


i have list of numbers:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20] 

my first task print each number on new line, this:

12 10 32 3 66 17 42 99 20 

i able creating loop printed integers in list:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20] in nums:     print(i) 

now need write loop prints each number , square on new line, this:

the square of 12 144 square of 10 100  square of 32 1024 square of 3 9 square of 66 4356 square of 17 289 square of 42 1764 square of 99 9801 square of 20 40 

so far have been able print squared values, can't them on separate lines themselves. need rid of brackets around values.

my attempt:

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20] in nums:     print(i)  squared = [ ]  in nums:     squared.append(i * i)     print("the square of", i, "is", squared) 

resulted in:

12 10 32 3 66 17 42 99 20 square of 12 [144] square of 10 [144, 100] square of 32 [144, 100, 1024] square of 3 [144, 100, 1024, 9] square of 66 [144, 100, 1024, 9, 4356] square of 17 [144, 100, 1024, 9, 4356, 289] square of 42 [144, 100, 1024, 9, 4356, 289, 1764] square of 99 [144, 100, 1024, 9, 4356, 289, 1764, 9801] square of 20 [144, 100, 1024, 9, 4356, 289, 1764, 9801, 400] 

i need rid of brackets , need include 1 squared value per line (that corresponds integer left). how should this?

i'm not sure if want track squares or if part of effort them printed.

assuming did want track squares, minor change be:

https://repl.it/glmw

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20] in nums:     print(i)  squared = [ ]  in nums:     sqr = *     squared.append(sqr)     print("the square of {} {}".format(i, sqr)) 

this allows have current square ready work without having reference array.

if did mean reference squared value array, you'd use negative index fetch end of array:

https://repl.it/glms

nums = [12, 10, 32, 3, 66, 17, 42, 99, 20] in nums:     print(i)  squared = [ ]  in nums:     squared.append(i*i)     print("the square of {} {}".format(i, squared[-1])) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -