How do I loop backward using indices in Python?

0 votes

I want to loop from 100 to 0, how do I do it?

for i in range (100,0) #this is not working
Jul 3 in Python by Shabnam
• 920 points
18 views

1 answer to this question.

0 votes

Hi,

You can make use of negative indices to start from back:

for i in range(100):
    k = 100 - i
    print(k)

Or You can use:

for i in reversed(range(101)):
    print(i)

Both will give same output that is 100 to 0

answered Jul 3 by Taj
• 1,040 points

Related Questions In Python

0 votes
1 answer

How can I use indices in math using python

Operator ^ is a bitwise operator, which does "bitwise exclusive or". More: http://wiki.python.org/moin/BitwiseOperators The ...READ MORE

answered Dec 11, 2018 in Python by ariaholic
• 7,340 points
55 views
0 votes
1 answer
+2 votes
3 answers

How can I play an audio file in the background using Python?

down voteacceptedFor windows: you could use  winsound.SND_ASYNC to play them ...READ MORE

answered Apr 3, 2018 in Python by charlie_brown
• 7,720 points
1,688 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 216 views
+4 votes
6 answers
+1 vote
1 answer

How can I reverse list in Python?

Reversing a list is a commonly used ...READ MORE

answered May 13 in Python by Taj
• 1,040 points
38 views
0 votes
1 answer

How to put time delay in Python?

You can use sleep() method to sleep ...READ MORE

answered May 28 in Python by Taj
• 1,040 points
27 views