Can you repeat the number back without using a list?

How would you number a number back without using a list in Python 3?

Using lists, I would probably do something like:

li = list(range(100))
for i in li[::-1]:
    print(i)

This is a great solution, but it does not work with huge numbers. Right now, I am trying to repeat the number back with large numbers, but an overflow error occurs, so I think I need to somehow find a way to repeat the number back without using lists.

Is there any way?

+3
source share
4 answers

Iterators that provide a special method __reversed__()also support reverse iteration. In Python 3.x, range()iterators have this method, so you can also use

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

Python 2.x, xarnge() :

for i in reversed(xrange(100)):
    print i

.

+11

? . Python . (Edit: , . .)

range(n, m): range(m-1, n-1, -1) (-1 - , , - range ).

+2

Try using range] and release ...

for i in range(100, 0, -1):

[update] I just noticed that you said Python 3, in which case you can just use a range instead of xrange.

+1
source
for i in range(10,0,-1):
    print(i)
+1
source

Source: https://habr.com/ru/post/1795111/


All Articles