\$\begingroup\$
\$\endgroup\$
I want to print the below pattern
*
* *
* * *
* * * *
* * * * *
My logic written
for row in range(1,6):
for col in range(1,6):
if row is col:
print(row * '* ')
Can someone review the code and suggest any improvements needed
The code which i have written is it a right approach ?
1 Answer 1
\$\begingroup\$
\$\endgroup\$
0
Welcome to Code Review.
Your code is literally 4 lines. So there isn't much to review.
Do not use the
is
operator to compare the integers. See difference betweenis
and==
You can save the nested loop: you are already ensuring
col == row
so you can instead write:
for row in range(1,6):
print("* " * row)
- If we want to get technical, the code you have written has a time complexity of O(n^3), but the problem can be solved in O(n^2). You can read more on this topic here.
answered Dec 3, 2021 at 5:01
lang-py