Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can any one explain the logic behind this code

Sorry for asking this very basic question – but I cannot understand why it does what it does.

a = int(input('Enter No. of rows'))
for i in range (a,0,-1):
  print(i*'*')

Thank you taking time to review and answer this question.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The range function accepts three arguments. The first arg. represents the starting integer, the second arg. represents the integer at which to stop, and the 3 arg. represents how much should the numbers increase by. Therefore, in this case, (start, stop, step) = (a, 0, -1). This basically means start from a (the inputted value), move till 0 and increment by -1.

Then, in each iteration, the asterisk symbol (*), repeated i many times, is printed.

You can refer the range function here and here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading