Python range function and numpy arange function

Source: Internet
Author: User

1.range()Returned is range object , and np.arange() The return is numpy.ndarray() the
 rangeCan be used for iterations, and np.arange much more than that, it is a sequence that can be used as a vector.

2. range() step is not supported decimal, np.arange() support step is decimal

3. Both are available for iterative

4. Both have three parameters, starting with the first argument, the third parameter is the step, and the data series that does not include the second argument before the second argument
In a sense, the interval between the iterator and the STL is the same, that is, the left-closed-right-open interval. [first, last)or to write without rigor.[first:step:last)

>>>range (1,5) range (1,5) >>>tuple (range (1, 5)) (1, 2, 3, 4) >>>list (range (1, 5)) [1, 2, 3, 4] >>>r = Range (1, 5) >>>type (r) <class ' range ' >>>>for  i in range (1, 5): ...    . Print (i) 1234>>> np.arange (1, 5) array ([1, 2, 3, 4]) >>>range (1, 5,. 1) Traceback (most recent call last): C2/>file "<stdin>", line 1, in <module>typeerror: ' Float ' object cannot is interpreted as an INTEGER>>&G T;np.arange (1, 5,. 5) Array ([1.,  1.5,  2.,  2.5,  3.,  3.5,  4.,  4.5]) >>>range (1, 5, 2) >>>for I in range (1, 5, 2): ...    Print (i) 13>>for i in Np.arange (1, 5): ...    Print (i) 1234

Note: The range () is canceled in python3.x, and Xrange is renamed to Range.

Xrange () is also used as a loop, except that Xrang (0,10) does not return a list, returning the Xrange object. Each call returns one of these values.

Xrange performance is better when a large number is returned or when a frequent break is required. Arange and Xrange no difference, and in a large number of times Xrang more superior, later coding when try to use xrange.

Python range function and numpy arange function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.