Tags
python
Asked 2 years ago
6 Aug 2021
Views 542
Mittie

Mittie posted

Counting backward from an integer using Python range

Counting backward from an integer using Python range
sqltreat

sqltreat
answered Aug 9 '21 00:00

yes you can use step is -1 in range() function and you will get backward counting in the Python range function

see how :

v=range(6,0,-1)
print(list(v))


we start range in a reverse manner and stop at starting index, so suppose you want to count backward 6 to 1 than start should be 6 and 0 should be stop parameter and increase should be -1 per step

so it will give you output :

[6, 5, 4, 3, 2, 1]


let's count the integer in backward, i am considering count means you want a total of values

v=range(6,0,-1)
count=0
for i in v:
	count=count+i
print(count)


it will give you a total is 21 but it does not matter how you count forward or backward it gives you total : 21 so maybe i mistaken what you asking really .
so write down some more detail about your question so i answer better in your problem
Post Answer