Table of Contents
How to count down in a for
loop in Python?
A loop is a basic tool of programming that allows us to execute some code till a given set of conditions returns True. The for
loop is one such loop available in many programming languages.
In Python, we can use the for
loop to iterate over a sequence of elements. Such a sequence can be a list, a tuple, and many more. The for
loop can iterate over such sequences till its end is reached. The range()
function returns such a sequence (In Python 2, the range()
function directly returns a list of elements).
It returns a sequence of numbers from the given range. The range can be provided within the function using the start
, stop
, and step
parameters. The start
and stop
parameters define the endpoints of the sequence. The increment between successive numbers is determined by the step
parameter.
We will now discuss how to count down in a for
loop in Python.
Using the step
parameter to count down in a for
loop in Python
As discussed, the increment between two successive numbers is determined using the step
parameter in the range()
function. We can count down in a for
loop in Python using this.
For this, the start
value should be greater than the end
value. The step
parameter needs to have a negative value. To count down by one, we can specify this parameter as -1
. Essentially, we will traverse through a sequence that is in reverse order.
See the code below.
1 2 3 4 |
for i in range(5,0,-1): print(i) |
Output:
4
3
2
1
In the above example, we count down from 5 to 0 using the for
loop. This is the most efficient and fast method to count down in a for
loop in Python.
Further reading:
Using the reversed()
function to count down in a for
loop in Python
The reversed()
function takes a sequence and reverses its order. We can create a simple sequence using the range()
function and reverse it using this function.
After reversing the sequence, we can iterate over it using the for
loop to count down.
See the code below.
1 2 3 4 |
for i in reversed(range(1,6)): print(i) |
Output:
4
3
2
1
In the above example, we create a sequence from 1 to 5 using the range()
function and reverse it using the reversed()
function. We count down in a for
loop in Python using this sequence.
Using the slicing technique to count down in a for
loop in Python
Slicing refers to the technique of extracting portions from a sequence like a list, string, and more. We use the square brackets for this.
Essentially, we need to specify the starting and ending index within the brackets along with the step value if required. We can put this step value as -1
and not mention any starting or ending index to reverse the sequence.
After reversing the sequence, we can iterate over it using the for
loop.
For example,
1 2 3 4 |
for i in range(1,6)[::-1]: print(i) |
Output:
4
3
2
1
Conclusion
To conclude, we discussed how to count down in a for
loop in Python. Essentially, we had to display a sequence of numbers in reverse order.
In the first method, we used the range()
function to create a sequence in reverse order using its parameters. The step
parameter needs to be specified as -1
to create the sequence in reverse order and iterate it using the for
loop. This was the most direct and fast method.
The remaining two methods involved creating a normal sequence using the range()
function and then reversing it using the reversed
function and slicing technique. These methods are comparatively slow.
That’s all about how to count down in a for loop in Python.