- Practice
- Python
- Introduction
- Loops
- Discussions

# Loops

# Loops

wuestenblume + 16 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')

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)]

arpita0116 + 2 comments 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.

arpita0116 + 0 comments Got it. Thanks

maheswaradito + 0 comments youu using python 2 or 3

shankarmurthy88 + 0 comments where is that square bracket?

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

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

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

To the following code

kumarparth123 + 3 comments ME TOO GETTING ERROR PLEASE HELP

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

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

nick3499 + 0 comments [deleted]namanbhoj99 + 1 comment # using python 3

n = int(input())

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

pratik15045569 + 0 comments no need to write i=i+1 . otherwise its perfect.

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

Getting error please help

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 + 2 comments if __name__ == '__main__': n = int(raw_input()) i=0 for i in range(n): print pow(i,2)

karan_katam16 + 0 comments why i=0?

ashishfrancis291 + 0 comments This is the only thing that helped me because the output was not required in the list format. Thankyou so much !

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)]

DahliaSR + 1 comment 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]`

h150303105219 + 0 comments how to get output for testcase 1 result

bryon_nicoson + 0 comments it is an elegant python 3 solution

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

ic3balandran + 3 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)

DahliaSR + 1 comment This approach, would fail!

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

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

vnsst123 + 0 comments Yes sir. I am a beginner and was searching for this kind of a one. Thank you

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

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')

sunnyjagtap23_sj + 0 comments error.......

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

twistagent + 0 comments hey thats pretty good

sfasu77 + 0 comments Awesome, blows my solution away lol

ram031197 + 0 comments Why * is used in print statement

ElFernando + 3 comments This is my solution papus

n=int(input())

for i in range(n):

`print(i**2)`

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?

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!

eng_shomer + 0 comments how to know the successful submission

pkhadka56 + 0 comments okay

Sowbarani + 0 comments n = int(raw_input()) for i in range(0,n): print i*i

while compiling the above code its showing the IndentationError, Why its occuring ? Can any one help on this ?

**Sorry: IndentationError: expected an indented block (solution.py, line 3)**

merrdem + 2 comments for py3

x = int(input())

if x<=20 and x>=0:

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

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

munishsinghdanu1 + 0 comments [deleted]

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 ()

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

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

shivamnamdeo43 + 0 comments Can someone tell me why I am getting error while submission on below code ?

N=5 if N >= 1 and N <= 20: for i in range(0,N): print(i ** 2)

norriskyle00 + 0 comments Total newbie status but am confused about the problem itself, loops, and how to implement the integers(1

sandy_aquarius90 + 0 comments Simplest solution -

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

richdad_solomon + 0 comments If you enjoy using map and lambda, check this out:

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

9999Rish + 0 comments print ('This one is the easiest') n = int(input()) for i in range(n): print(i**2)

Sort 228 Discussions, By:

Please Login in order to post a comment