+ 1

Can someone please breakdown the for loop number patterns (tree shapes)?

After going through basic python and exposure to pandas and numpy, I decided to learn by doing for the next few days. In the process I understood function and for loops but the tree shape pattern just defeats me. The code is found below: print("Fouth number pattern") lastNumber = 9 for row in range(1, lastNumber): for column in range(-1 + row, - 1, - 1): print(format(format(2**column, "4d"), end=" ") print(" ")

21st Apr 2020, 8:31 PM
Butcher
Butcher - avatar
1 Resposta
+ 1
This Code will Print 8 rows (lastNumber - 1). Each row will have x collumns. The First has 1, the second 2 etc... (Row - 1, until -1, in descending Order) . The columns will then contain 2 to the Power of column number.. (128, 64, 32, ... , 1) At the end of each column, a space will be ran printed (end = ' '), and the Numbers will have 4 Digits ("4d") At the end of each row, there will be a new line... Regarding Format, See Code below: lastNum = 9 for row in range(1, lastNum): for col in range(-1+row, -1, -1): print("{:4d}".format(2**col), end=' ') print(" ")
21st Apr 2020, 9:20 PM
G B
G B - avatar