Question #255747

Write Python code that uses nested loops to display the following pattern:

1

1 2 1

1 2 4 2 1

1 2 4 8 4 2 1

1 2 4 8 16 8 4 2 1

.

.

.

1 2 4 8 16 32 64 128 64 32 16 8 4 2 1


Expert's answer

for i in range(8):
    lst = []
    for j in range(i):
        lst.append(2 ** j)
    lst.append(2 ** i)
    for j in range(i - 1, -1, -1):
        lst.append(2 ** j)
    print(*lst)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS