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 you explain how this Loop works and how do we get that output?

if __name__ == '__main__':
n = int(input())     
for i in range (0,n):     
 result = i**2      
 print(result)

#input : 5
#output : 0 1 4 9 16

range 0,n = 0,1,2,3,4 and
we gave i**2 but how we got 0,1,4,9,16 as output?

>Solution :

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

range(start, stop, step)

** = square of Number

  1. start Optional. An integer number specifying at which position to
    start. Default is 0
  2. stop Required. An integer number specifying at which position to
    stop (not included).
  3. step Optional. An integer number specifying the incrementation. Default is 1

you are passing required parameter as 5 which will not be included in the loop. so as per your calculation it will start from 0

result = 0**2 = 0
result = 1**2 = 1
result = 2**2 = 4
result = 3**2 = 9
result = 4**2 = 16

‘i’ will not reach 5 because of non-inclusive nature of range() operator.

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