How to make a loop inclusive from beginning to end indexes? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 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

17th Jul 2017, 1:33 PM
Munindra
Munindra - avatar
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.
17th Jul 2017, 1:39 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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.
17th Jul 2017, 1:48 PM
Munindra
Munindra - avatar
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!!
17th Jul 2017, 3:30 PM
PierX
PierX - avatar