### Thread: What is range in python and why we have to use it?

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
13
Rep Power
0

#### What is range in python and why we have to use it?

what is range in python and why we have to use it?
2. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
10
Rep Power
0
http://docs.python.org/3/library/stdtypes.html#range

to iterate over a sequence of numbers, the built-in function range(). It generates lists containing arithmetic progressions

The whole theory of built in functions is to have the basic tools already available. They are tuned to be as fast as they can and be as adaptable as they can, whereas writing your own, may not live up to the standards per se.

for example you could write your own basic range function like so, but to be honest using the builtins allows you to focus on your code and not on re-designing the wheel:

Code:
```def ranger(num):
n = 0
lister = []
while n != num:
lister.append(n)
n += 1
return lister
for i in ranger(10): #your own
print(i)
print()
for i in range(10): #built in
print(i)```
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2007
Location
Joensuu, Finland
Posts
446
Rep Power
67
Originally Posted by metulburr
for example you could write your own basic range function like so,
...in Python 2, where range() returns a list. In Python 3, the following is more close to the truth:

Code:
```def ranger(num):
n = 0
while n < num:
yield n
n += 1```

• dariyoosh agrees
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
13
Rep Power
0

#### thank you

Originally Posted by metulburr
http://docs.python.org/3/library/stdtypes.html#range

to iterate over a sequence of numbers, the built-in function range(). It generates lists containing arithmetic progressions

The whole theory of built in functions is to have the basic tools already available. They are tuned to be as fast as they can and be as adaptable as they can, whereas writing your own, may not live up to the standards per se.

for example you could write your own basic range function like so, but to be honest using the builtins allows you to focus on your code and not on re-designing the wheel:

Code:
```def ranger(num):
n = 0
lister = []
while n != num:
lister.append(n)
n += 1
return lister
for i in ranger(10): #your own
print(i)
print()
for i in range(10): #built in
print(i)```

Thank you for the reply. I have a question If "range(3,12,0,-4)" is written, what will be happen or will it show error?
5. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
114
Rep Power
4
Originally Posted by superversion970
Thank you for the reply. I have a question If "range(3,12,0,-4)" is written, what will be happen or will it show error?
Code:
```>>> range(3,12,0,-4)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
range(3,12,0,-4)
TypeError: range expected at most 3 arguments, got 4```
Really you could've just tested that yourself. Hell, in this case the error message is even pretty clear as to what the problem is.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
10
Rep Power
0
check the docs with help()

Code:
```metulburr@ubuntu:~\$ python3
Python 3.2.3 (default, Oct 19 2012, 20:10:41)
[GCC 4.6.3] on linux2
>>> help(range)

>>> exit()
metulburr@ubuntu:~\$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39)
[GCC 4.6.3] on linux2
>>> help(xrange)

>>> exit()
metulburr@ubuntu:~\$```
I left out the yeild because i figured if he didnt know range he wouldn't know yield

>>> help(list())

>>> dir(list())

>>> help(list.append)