+ 1
How to make a loop inclusive from beginning to end indexes?
I mean if we write this program # squares squares=[i**2 for i in range(6)] print(squares) Output will be:- [0,1,4,9,16,25] But I want to print all the squares of whole numbers till 6 i.e. 36 which is not possible in this code.Is there any method to do it? Quick response is expected from this wonderful community
3 Antworten
+ 8
Would there be a problem if you replaced 6 with 7? Let's say you replace the integer with n, and let the user input to n, all you need to do is to add 1 to n and then do the range thing.
+ 1
@Hatsy Rei This thing came to my mind but I was thinking if there is any library function to do it,which I am unaware of.
0
# squares
squares=[i**2 for i in range(7)]
print(squares)
Output will be:-
[0,1,4,9,16,25,36]
Try it now!!