0

If range(1,10) generates a list from 1,2,3 so on till 9 Then will range(10,1) do it in the same work in reverse order i.e 10,9 8,7,6 so on till 2

19th Feb 2017, 2:46 AM
rebirth
2 Réponses
+ 12
When I started learning python, I wondered the same thing. I'm pretty sure it worked for me once but I might've dreamt it :P But the short answer is no. There are two common ways for backwards traversal: The first solution (the less elegant way) is to use reverse method- for i in reverse(range(1,10)): #your code here The other way is to remember that range takes in a third argument, step. If you set the step to be -1, it goes backwards- for i in range(10,1,-1): #your code here The second way doesn't appear to work on mobile, however. Well, it doesn't work for me
19th Feb 2017, 3:55 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
0
Tq
19th Feb 2017, 3:58 AM
rebirth