+ 2
What really happens in this code
How each loop works? https://code.sololearn.com/c3UBQ5p302ht/?ref=app
10 odpowiedzi
+ 6
Hi alagammai!
Inorder to understand this code, it's better to divide it into two snippets.
1. Without variable j, output pattern would be like this
(for i in range(1,10,2):
print(i*'*')), here i = 1,3,5...
*
***
*****
2. After you added variable j with i times '*', each line of snippet1 is added with j times space hence, j is decreasing in each iteration.
J = 9,8,7,
(spaces) ........*
......***
.....*****
+ 3
https://code.sololearn.com/cAcU7m0Wri4M/?ref=app
Playing with it is most fun
+ 2
Okay.
First line: j = 9 (pretty obvious)
Second line: for i in range (1, 10, 2):
This for loop will go from 1 through to 9 in increments of 2 i.e 1, 3, 5, 7, 9.
Third line: print(' ' * j + i * '*')
This is the part that may confuse you. This line prints : "j" spaces, then prints "i" stars. In the first run of the loop, it will print 9 spaces, then 1 star. In the second run, it will print 8 spaces, then 3 stars (j is decremented by 1 after each loop run: j -= 1).... In the last run of the loop it will print 5 spaces and 9 stars.
+ 2
+ 2
Barnik Roy thank you
+ 2
Now check the print function carefully now you can understand
j=9
for i in range(1,10,2):
print((' '*j) + (i*'*'))
j-=1
here print function prints stars and spaces with single print function
+ 1
alagammai uma Just analyze the code by yourself. You'll understand it. Try this:
j = 9
for i in range(1, 10, 2):
print(f"i = {i}, j = {j}\n")
print(" " * j + "*" * i, "\n")
j -= 1
+ 1
Yes this question is the question In which anyone got confused first time as I am but when you will see it carefully you will got it what is happening in really.
alagammai uma
JUMP_LINK__&&__Python__&&__JUMP_LINK Learner you have explained very correctly.
0
Narendhar Kothuri thank you