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
5 Respostas
+ 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 '...
+ 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
+ 1
print(*(i * (str(i) + ' ') + '\n' for i in range(1, 6)), sep = '')
0
thanks strawdog
0
thanks Anna
but as for your code I'll be infinitely grateful if you help me understand it😋