0

Can we make a range backeards like 20 to 5? (without using reverse)

4th Oct 2018, 12:32 AM
partha
partha - avatar
5 Respuestas
+ 4
yes! range(20, 5, -1)
4th Oct 2018, 12:36 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 4
if you want 5 to be included do: range(20, 4, -1)
4th Oct 2018, 12:38 AM
Ulisses Cruz
Ulisses Cruz - avatar
+ 3
You can't write range(20, 5) because the standard value for step is 1, just like the standard value for start is 0. Each parameter you don't write is replaced by the standard parameter. range(5) is equal to range(0, 5, 1). range(2, 5) is equal to range(2, 5, 1). range(20, 5) is equal to range(20, 5, 1) and tells python to count upwards from 20 to 5 using steps of 1, which is impossible.
4th Oct 2018, 6:42 AM
Anna
Anna - avatar
+ 1
oh now I understood thanks Anna
4th Oct 2018, 8:39 AM
partha
partha - avatar
0
Ulisses Cruz but then why can't we just write range(20,5)? what's the problem just going from 20 to 4 like that and we need to use -1
4th Oct 2018, 12:48 AM
partha
partha - avatar