Understanding range(n, 0, -1) in Python

rajneesh

rajneesh

3 min read

  • python
 range Python -1

Are you looking for? How range function works with these parameters n, 0, -1? Then you have come to the right post because I will cover everything about range function and how it works with different parameters. First of all we will learn about range function and their parameters and try with different parameters. This will help you to easily understand range function and what will be their output with those parameters. Let's begin

Breaking down range function

range function returns a sequence of numbers according to their input parameter values. let start understanding the about parameter:

  1. start: its an Optional parameter. and its required integer number specifying that at which position it start. Default is 0

  2. stop: its an Required parameter. and its required integer number specifying at which position it stop.

  3. step: it is also an Optional parameter. and its required integer number specifying the incrementation. Default is 1

coding syntax of range function:

range(start, stop, step)

When using range(n, 0, -1), these parameters are defined as follows:

  • start: n - it will start with n.

  • stop: 0 - it will stop before 0.

  • step: -1 - it increment by -1 you can also say that its decrement by 1 in each iteration.

    This means range(n, 0, -1) will generate a sequence of numbers from n down to 1, decrementing by 1 at each step.

    example with different parameters

    here is an simple example of range function with this parameters n,0,-1

for i in range(5, 0, -1):
    print(i, end=" ") output:- 5 4 3 2 1

here, loop start with 5 and each iteration it will decrement by 1 after reaching 1 it stop the loop.

some practical examples are:-

first example is time counter one of the most common use.

code example:

import time

for i in range(10, 0, -1):
    print(i)
    time.sleep(1)
print("time over....")

in this above example a time count start for 10 to 1 after 10sec its become 1. it means time is over.

second example is reversing the list by this you can print list in reverse order.

code example:

list = ['a', 'b', 'c', 'd', 'e']

for i in range(len(list) - 1, -1, -1):
    print(list[i], end=" ") output:- e d c b a

Conclusion

you can use this parameter to print number in reverse order. you can also try range function with different parameter to better understanding of range function. i have cover this topic with n,0,-1 parameters you can try different parameters more negative values. if you think this post help you to understand the range with -1 values as a step parameter then you can comment 😊 for my gift.

rajneesh

rajneesh

Creative, Elegant and Visionary

Latest

from the blog

The latest industry news,interviews and resources

Python's One-Line if else Magic

Python's One-Line if else Magic

Python's One-Line if else Magic

Best Alternatives to Adobe's Generative Fill AI Free

Best Alternatives to Adobe's Generative Fill AI Free

Fill for free online which enhance and boost your design works

Join our newsletter

we'll send you a nice letter once per week. No spam.

BiyondBytes

© 2024 BiyondBytes. All rights reserved.