Count Down in a for Loop in Python

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.

Output:

5
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.

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.

Output:

5
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,

Output:

5
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.

import_contacts

Related Posts

• 01 January

PowerShell – Count Files in Folders

Table of ContentsCount Files in Folders in PowerShellCount Files in multiple Folders in PowerShellCount Files in Folders and Subfolders in PowerShellCount only folders inside folder in PowerShellCount Files by Extension in Folder in PowerShell Count Files in Folders in PowerShell We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, […]

• 31 December

Check if Object has Property in PowerShell

Table of ContentsUsing the -match ParameterUsing -contains ParameterUsing if-else BlockUsing .Match() Function with if-else BlockUsing Get-Member Cmdlet Using the -match Parameter Use the -match parameter to check if an object has a property in PowerShell. [crayon-63b2ca59e83b2360777011/] [crayon-63b2ca59e83b6043566356/] Here, we used the -match parameter to check if the given object, which is \$result in our case, […]

• 28 December

Create Empty File in PowerShell

Table of ContentsUsing New-Item CmdletUse the ni AliasUsing Out-File CmdletUsing the fsutil file CommandUsing Text EditorUsing echo CommandUsing for Loop Using New-Item Cmdlet Use the New-Item cmdlet to create an empty .txt file in PowerShell. [crayon-63b2ca59e8563847603498/] [crayon-63b2ca59e8566277045654/] We used the New-Item cmdlet with -Path, -Name, and -ItemType parameters to create an empty .txt file using […]

• 27 December

Run PowerShell as Another User

Table of ContentsUsing the runas CommandOpen the PowerShell as an AdministratorRun the runas commandUsing the Start-Process cmdlet with the Credential ParameterUI-Based SolutionScript-Based Solution Using the runas Command The runas command in PowerShell launches programs using credentials different from the current user. It provides users with limited privileges to execute commands or access resources available only […]

• 27 December

Create Array of All NaN Values in Python

Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63b2ca59e88bb202113056/] [crayon-63b2ca59e88bf996514045/] We used numpy.empty() […]

• 27 December

Call Function from Another Function in Python

Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63b2ca59e8aa2433203777/] [crayon-63b2ca59e8aa6019822366/] To call a function, […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.