I
I
Igor2018-09-03 23:53:24
Python
Igor, 2018-09-03 23:53:24

Explain if there is a difference for (for example in js) from range in python?

Explain if there is a difference for (for example in js) from range in python?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Teslaman, 2018-09-04
@aif88

range has nothing to do with for. The for statement is for looping, while the range function returns a sequence.
For example, range(5) will return the list [0, 1, 2, 3, 4].
This list can be traversed with for.

for num in range(5):
    print(num)

K
Komesk, 2018-09-04
@Komesk

There is a difference in the for themselves.
In Python, for works with sequences, see iterables
for example loop

for i in range(5):
  print(i)

can be expanded into the following for greater clarity
a = iter(range(5))
while True:
    try:
        print(next(a))
    except:
        break

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question