What is the built-in function used in Python to iterate over a sequence of numbers?

Questions by fred   answers by fred

Showing Answers 1 - 36 of 36 Answers

Nagappan

  • Nov 13th, 2007
 

range is the built-in function to be used for interating the sequence of numbers.

for iter in range (0, 10):
   print iter # prints 0 to 9


for iter in range (0, 10, 2): # The last argument is the sequence to use. Default is 1.
   print iter # prints 0, 2, 4, 6, 8

  Was this answer useful?  Yes

addy

  • Oct 15th, 2012
 

Yup, I think youre right. enumerate will iterate over a list, and that list could have numbers in it e.g.:

>>> [ (i,j) for i,j in enumerate([9,10,11,12])]
[(0, 9), (1, 10), (2, 11), (3, 12)]

  Was this answer useful?  Yes

Paulo Kuong

  • Jun 10th, 2015
 

__next__ and __iter__

  Was this answer useful?  Yes

Tushar Gupta

  • Aug 9th, 2015
 

You can use iter to iterate through a list of integers as

Code
  1. a = iter(list(range(10)))

  2. for i in a:

  3.     print(i)

  Was this answer useful?  Yes

suchitra

  • Oct 5th, 2015
 

Code
  1. a = iter(range(4))

  2. for i in a:

  3.  print i

  Was this answer useful?  Yes

Rupali

  • Jan 24th, 2016
 

range()

  Was this answer useful?  Yes

sethuraman

  • Feb 5th, 2016
 

Using for loop is best one
Exp:
for i in range (start , end)
works fine

  Was this answer useful?  Yes

saravanan

  • Mar 16th, 2016
 

a = iter(range(10))
a.next()

  Was this answer useful?  Yes

alok

  • Apr 25th, 2016
 

class Counter:
def __init__(self, low, high):
self.current = low
self.high = high
def __iter__(self):
return self
def next(self): # Python 3: def __next__(self)
if self.current > self.high:
raise StopIteration
else:
self.current += 1
return self.current - 1
for c in Counter(3, 8):
print c

  Was this answer useful?  Yes

madan

  • Jun 26th, 2017
 

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

  Was this answer useful?  Yes

Prem M S

  • Aug 17th, 2017
 

syntax : range(start,end,step count)
Ex:
a = range(1,10,2)
print a
o/p:
[1, 3, 5, 7, 9]
If using to iterate
for i in range(1,10):
print i
o/p:
1
2
3
4
5
6
7
8
9

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions