We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

The entire argument to join is a list comprehension. Within the comprehension, for n in [int(raw_input())] for i in xrange(n) technically sets up a nested loop, although the outer loop only executes once.

[int(raw_input())] creates a list with one element, the height of the staircase. for n in [int(raw_input())] goes through (each of) the element(s) of that list, setting n to that element. (This only happens once because there is only one element). This part is just a trick to set n to the staircase height without needing a separate statement (separate line or semicolon).

The inner loop, for i in xrange(n), is the real loop. Because it's the inner loop, n has already been set to the height of the staircase. Of course, this inner loop sets i to every integer from 0 to n-1, in turn.

('#' * (i + 1)).rjust(n) creates each line. For each iteration of the inner loop (and therefore for each value of n and i), the list of results has an element with (i + 1) '#' characters, right-justified in a field of width n.

## Staircase

You are viewing a single comment's thread. Return to all comments →

For oneliners:

`n=int(input());[print(" "*(n-i)+'#'*i) for i in range(1,n+1)]`

Or for those who feel a semicolon in a one-liner is cheating, Python2:

Python3:

How does it keep looping and jumping to the next line? (Python2)

The entire argument to

`join`

is a list comprehension. Within the comprehension,`for n in [int(raw_input())] for i in xrange(n)`

technically sets up a nested loop, although the outer loop only executes once.`[int(raw_input())]`

creates a list with one element, the height of the staircase.`for n in [int(raw_input())]`

goes through (each of) the element(s) of that list, setting`n`

to that element. (This only happens once because there is only one element). This part is just a trick to set n to the staircase height without needing a separate statement (separate line or semicolon).The inner loop,

`for i in xrange(n)`

, is the real loop. Because it's the inner loop,`n`

has already been set to the height of the staircase. Of course, this inner loop sets`i`

to every integer from 0 to n-1, in turn.`('#' * (i + 1)).rjust(n)`

creates each line. For each iteration of the inner loop (and therefore for each value of`n`

and`i`

), the list of results has an element with (i + 1) '#' characters, right-justified in a field of width`n`

.for java one liner's