Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 25th, 2012, 01:03 AM
 superversion970
Registered User

Join Date: Dec 2012
Posts: 13
Time spent in forums: 2 h 45 m 18 sec
Reputation 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
December 25th, 2012, 01:20 AM
 metulburr
Registered User

Join Date: Dec 2012
Posts: 10
Time spent in forums: 5 h 43 m 57 sec
Reputation 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
December 25th, 2012, 04:30 AM
 SuperOscar
Contributing User

Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 420
Time spent in forums: 1 Week 18 h 21 m 49 sec
Reputation Power: 66
Quote:
 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!
__________________
My armada: openSUSE 13.1 (home desktop, home laptop), Ubuntu 13.04 (work laptop), FreeBSD 9.2 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), Android 4.1.2 (tablet)

#4
December 25th, 2012, 07:05 AM
 superversion970
Registered User

Join Date: Dec 2012
Posts: 13
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
thank you

Quote:
 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
December 25th, 2012, 09:00 AM
 Nyktos
Contributing User

Join Date: Dec 2012
Posts: 114
Time spent in forums: 1 Day 11 h 54 m 6 sec
Reputation Power: 3
Quote:
 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
December 25th, 2012, 09:12 AM
 metulburr
Registered User

Join Date: Dec 2012
Posts: 10
Time spent in forums: 5 h 43 m 57 sec
Reputation 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

#7
December 25th, 2012, 11:46 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,124
Time spent in forums: 1 Month 3 Weeks 2 Days 4 h 49 m 23 sec
Reputation Power: 455

>>> help(list())

>>> dir(list())

>>> help(list.append)

http://docs.python.org/3/index.html

Note that where this says

Library Reference

They mean it seriously! It's not tongue-in-cheek.
__________________
[code]Code tags[/code] are essential for python code!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > What is range in python and why we have to use it?