#1
  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. #2
  3. 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)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    430
    Rep Power
    66
    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

    Comments on this post

    • dariyoosh agrees
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  6. #4
  7. 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?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    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.
  10. #6
  11. 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
    Type "help", "copyright", "credits" or "license" for more information.
    >>> help(range)
    
    >>> exit()
    metulburr@ubuntu:~$ python
    Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
    [GCC 4.6.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> help(xrange)
    
    >>> exit()
    metulburr@ubuntu:~$
    I left out the yeild because i figured if he didnt know range he wouldn't know yield
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Please read manuals


    To superversion, please read the python manuals and tutorials, or use the interpreter. Please learn to help your self, and try experiments in the interpreter.

    >>> help(list())

    >>> dir(list())

    >>> help(list.append)

    Please bookmark this link and refer to it:

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



    Note that where this says

    Library Reference
    keep this under your pillow

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

IMN logo majestic logo threadwatch logo seochat tools logo