0

I wanted coding a pattern. Please modify it and if possible please explain line by line (I'm facing difficulty understanding it)

I wanted:- 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 my code:- for i in range(6): for j in range(6,i,-1): print(i, end=' ') print() my out put:- 0 0 0 0 0 0 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5

10th Oct 2018, 1:17 PM
partha
partha - avatar
5 Respuestas
+ 3
It's actually the same as for i in range(1, 6): print(i * (str(i) + ' ')) In python, you can "multiply" strings. print(3 * 'hi') prints 'hihihi'. So the code will just print 1 * '1 ' and a newline, 2 * '2 ' and a newline, 3 * '3 '...
10th Oct 2018, 5:10 PM
Anna
Anna - avatar
+ 2
for i in range(1,6): for j in range(0,i): print(i, end=' ') print() ===== First line - you set the range of what to print and number of lines: range(1,6) will print integers from 1 to 5 and do it in five lines Second line - you set how many times you want an integer to be printed: range (0, i) will print your integer i-times
10th Oct 2018, 1:50 PM
strawdog
strawdog - avatar
+ 1
print(*(i * (str(i) + ' ') + '\n' for i in range(1, 6)), sep = '')
10th Oct 2018, 4:59 PM
Anna
Anna - avatar
0
thanks strawdog
10th Oct 2018, 1:55 PM
partha
partha - avatar
0
thanks Anna but as for your code I'll be infinitely grateful if you help me understand it😋
10th Oct 2018, 5:05 PM
partha
partha - avatar