+ 1

[SOLVED] How range works?????

why : print (range(20)==range (0,20)) is true And print (range (20)==range (20,0)) is false

31st Oct 2017, 4:12 PM
Shyamal Bhatt
Shyamal Bhatt - avatar
3 Answers
+ 10
This data type is characterized as: range(Start, Stop, Step). You specify the Start point, Stop (range ends *before* Stop, not including it) and skips every Step. So range(3, 8, 2) will produce 3, 5, 7. Now, range(20) is equal to range(0, 20, 1). But range(20, 0) is empty, as there are no numbers between 20 and 0, counting left-to-right. You can count right-to-left though. In order to do that, your Step should be negative: range(20, 0, -1) will produce 20, 19, 18, ..., 2, 1. That won't be equal 0, 1, 2..., 19 (so range(20)), though.
31st Oct 2017, 4:23 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
@kuba, Luka thank you both
31st Oct 2017, 4:32 PM
Shyamal Bhatt
Shyamal Bhatt - avatar