- TD
wuestenblume + 14 comments Thought my solution turned out looking pretty good so I thought I'd share:

print(*[num**2 for num in range(n)], sep='\n')

- MG
surfer2047 + 1 comment How does print(*[list]) works, could you please tell.

rpepato + 0 comments This is an arbitrary argument list. It works by expanding a list into a sequence of positional parameters by using the * operator. Take a look at this examples at the python tutorial.

nthomas1999 + 0 comments Beautiful. You taught me something today. Thanks

Canas + 5 comments Though I like your solution (great use of unpacking), I think this is just more straightforward

[print(i**2) for i in range(n)]

- AP
arpita0116 + 1 comment why the square bracket ?

Canas + 2 comments The square bracket in this case corresponds to a List Comprehension in Python. They're neat for many reasons, but for this particular case it's mostly a better looking (and probably faster) for-loop.

- AP
arpita0116 + 0 comments Got it. Thanks

- MD
maheswaradito + 0 comments youu using python 2 or 3

chadwalt + 2 comments FROM THIS:

counter = 0; while counter < n : print (counter**2) counter = counter + 1

TO THIS:

[print(i**2) for i in range(n)]

Thanks alot for that link

- C
Lukewardly + 1 comment I would have said, from this:

for i in range(n): print(i**2)

To the following code

- K
kumarparth123 + 3 comments ME TOO GETTING ERROR PLEASE HELP

nick3499 + 2 comments [deleted]- K
kumarparth123 + 0 comments its working thanks.

- K
kumarparth123 + 0 comments one more doubt are last five topics waste in hacker rank python

nick3499 + 0 comments [deleted]- NB
namanbhoj99 + 0 comments # using python 3

n = int(input())

for i in range(0,n): print(i*i) i= i+1

- RG
rupeshjawadwar + 2 comments File "solution.py", line 4 [print(i**2) for i in range(n)] ^ SyntaxError: invalid syntax

Getting error please help

- NS
nikhilnsr1998 + 0 comments i hope you got it by now, but the square brackets are at the wrong position... here we are printing a list ------->(unpacking) list : i**2 for i in range(n) where i iterates over the provided range and i**2 is computed out Syntax:

a=[i**2 for i in range(N)] print(a)

or in one line: print([i**2 for i in range(N)])

thomasokonkwo91 + 0 comments if __name__ == '__main__': n = int(raw_input()) i=0 for i in range(n): print pow(i,2)

- M
meanagray + 1 comment How to integrate the constraint of n < 20 ?

Canas + 0 comments In this case the assumption is that input is between 1 and 20, so we don't expect any other input.

If you wanted to enforce the constraint, the code would be extended to this

[print(i**2) for i in range(n) if n in range(1, 21)]

- D
DahliaSR + 0 comments No it is not, this means, that

*print()*will be called n-times instead of once.Besides that you need to keep in mind that any function or method in python

**always**returns*None*unless it is told explicitly to return something else. In your solution, the list comprehension will return a list of length n, where each item is*None*.`>>> n = 5 >>> [print(i**2) for i in range(n)] 0 1 4 9 16 [None, None, None, None, None]`

bryon_nicoson + 0 comments it is an elegant python 3 solution

- AD
adocto01 + 1 comment When testing with input 21 I get 21 lines of output which violates the constraints as it's supposed to limit N to between 1 and 20. I'm kind of confused because I'm not sure whether the Problem's constraints mean we're supposed to restrain inputs to that range in the program or if the end user of the program will restrain their inputs to that range.

b0kater + 0 comments This refers to the test cases that hackerrank will use to check your work. The constraints state "our test data will fit these constraints."

nick3499 + 0 comments [deleted]engr_ravijain + 0 comments thanks you thaught me something new and usefull

- FB
ic3balandran + 2 comments The problem was about loops. If you show that to a beginner most likely they will not understand.

The solution with loops is just as short.

for i in range(0, n): print(i * i)

- D
DahliaSR + 1 comment This approach, would fail!

The exponent has to be*2*and not*i*.- CS
chris_m_sullens + 0 comments That's not an exponent: i*i = i**2

- NB
namanbhoj99 + 0 comments buT it only prints 0 and n squared

- HL
huanyu_liao + 0 comments it seems that py2 doesn't support this syntax

- D
DahliaSR + 0 comments Replace the list comprehension by a generator expression and you got the most efficient and shortest solution.

Lehonti + 0 comments Nice solution! How about using iterators instead of arrays? It's more memory-efficient

print(*(num**2 for num in range(n)), sep='\n')

- SJ
sunnyjagtap23_sj + 0 comments error.......

Ashish25 + 0 comments Thanks for teaching argument unpacking along with list comprehension.

- WL
twistagent + 0 comments hey thats pretty good

ElFernando + 3 comments This is my solution papus

n=int(input())

for i in range(n):

`print(i**2)`

- KS
karthikeyan1204 + 1 comment try for i in range(0,n):

ElFernando + 0 comments It's a same! range(0,n) that range(n):

KavinRanawella + 0 comments this is working right?

- NS
namansharma0206 + 0 comments N=int(input()) for i in range(N): print(i**2) This one works

pkhadka56 + 2 comments I don't know why test case 1 keeps on failing.

PRASHANTB1984 + 1 comment Do take a look at the successful submissions on the leaderboard!

- MS
eng_shomer + 0 comments how to know the successful submission

pkhadka56 + 0 comments okay

marinskiy + 0 comments Here is Python solution from my HackerrankPractice repository:

for i in range(int(input())): print(i ** 2)

Feel free to ask if you have any questions :)

merrdem + 2 comments for py3

x = int(input())

if x<=20 and x>=0:

`for a in range(0,x): print(a**2)`

- S.
Sonalj + 2 comments getting an error in this

merrdem + 1 comment x = int(input()) if x<=20 and x>=0: for a in range(0,x): print(a**2)

it is working mate

- MD
munishsinghdanu1 + 0 comments [deleted]

- MD
munishsinghdanu1 + 1 comment in python 2 use

for a in range(0,x): if x>=0 and x<=20: print (a**2)StorenthCatena + 0 comments python2 print without ()

- DP
dkapuriya99 + 0 comments Brother why we need to take new letter "a"?

*(for a in range(0,x):)*can you explain? Thank you

Darshit_Shah + 3 comments single line code

for i in map(lambda x : x**2,range(input())):print i

- PB
polmki99 + 2 comments Doesn't count if you use a colon! :-)

print(*map(lambda x: x**2, range(int(input()))), sep='\n')

VladimirFedotov + 2 comments this works too :)

print('\n'.join( [str(x**2) for x in range(int(input()))] ))

akhilsharma359 + 0 comments best would be

n=input()

for i in list(range(n))

print i*i- PP
patrick_x99 + 1 comment why use a list comprehension when you can use a generator expression?

print('\n'.join(str(x**2) for x in range(int(input()))))

nikajalabadze007 + 0 comments Why it doesn't work for

print('\N'.join(str(x**2) for x in range(int(input()))))

- JW
Pythonhaha + 1 comment what is the difference between *map and map?

- TD
wuestenblume + 2 comments Here the * is an 'unpacking' operator (in Python 3). So it's passing each of the elements of the resulting list instead of the list itself. For example:

>>> a_list = [1, 2, 3, 4, 5] >>> print(a_list, sep='\n') [1, 2, 3, 4, 5] >>> print(*a_list, sep='\n') 1 2 3 4 5

- JW
Pythonhaha + 0 comments Fully understand. Thank you very much :)

- NY
__Nemo__ + 0 comments Thanks

- DP
darpanlp + 0 comments this is only working for input() method but not for raw_input or int(raw_input()) because of it is python formula

- SG
Sanket_Gaykar + 0 comments its not working in python 3

fraxz + 0 comments I love python 2.

`n = input() i = 0 for i in range(n): print(i**2)`

ckastam + 0 comments if __name__ == '__main__': n = int(raw_input()) for i in range(0,20): if (i<n): x=i*i print(x)

it works fine in python 2 in python 3 the exact code gives me

File "solution.py", line 2 int n = int(raw_input()) ^ SyntaxError: invalid syntax

Finally why i in range(0,20) when the constraint is 1<=N<=20 ?

- PT
02pankaj + 0 comments n = int(input()) for i in range(0,n): print(i**2)

- JS
jacob_sparks + 0 comments n = int(input()) for i in range(0, n): if i > -1 and i < n: print(int(i**2))

Sort 174 Discussions, By:

Please Login in order to post a comment