Pyhon Cat 1: NameError: name 'xrange' is not defined

NameError: name 'xrange' is not defined

1 a=xrange(10)
1 ---------------------------------------------------------------------------
2 NameError                                 Traceback (most recent call last)
3 <ipython-input-21-076e08fdae66> in <module>()
4 ----> 1 a=xrange(10)
5 
6 NameError: name 'xrange' is not defined

Python 2: BOTH range() and xrange()

Python 3: ONLY range(), the same as xrange() in Python 2

In Python 2,

The advantage of xrange() over range() :

http://python-reference.readthedocs.io/en/latest/docs/functions/xrange.html

xrange

Syntax
xrange (stop) xrange (start, stop[, step])
start
    Required when full syntax is used. An integer specifying start value for the range.
stop
    Required. The boundary value for the range.
step
    Optional. Step value.

Note
CPython implementation detail: xrange() is intended to be simple and fast. Implementations may impose restrictions to achieve this. The C implementation of Python restricts all arguments to native C longs (“short” Python integers), and also requires that the number of elements fit in a native C long. If a larger range is needed, an alternate version can be crafted using the itertools module: islice(count(start, step), (stop-start+step-1+2*(step<0))//step).

Remarks
This function is very similar to range(), but returns an xrange object instead of a list. This is an opaque sequence type which yields the same values as the corresponding list, without actually storing them all simultaneously. The advantage of xrange() over range() is minimal (since xrange() still has to create the values when asked for them) except when a very large range is used on a memory-starved machine or when all of the range’s elements are never used (such as when the loop is usually terminated with break).

In Pyhon 3,

https://docs.python.org/3/library/stdtypes.html?highlight=range#range

The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents (as it only stores the start, stop and step values, calculating individual items and subranges as needed).

a.tpye of range()

a=range(10)

print(a)

print(type(a))

print(list(a))

print(type(list(a)))

print(a[0],a[1],a[9])

print(type(a[0]))
range(0, 10)
<class 'range'>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<class 'list'>
0 1 9
<class 'int'>

b.index of range()

print(list(range(10)))

print(list(range(0,10,1)))

print(list(range(-5,10,2)))

print(list(range(10,-5,2)))

print(list(range(10,-5,-2)))

print(list(range(10,0,-1)))

print(list(range(0)))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[-5, -3, -1, 1, 3, 5, 7, 9]
[]
[10, 8, 6, 4, 2, 0, -2, -4]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[]

c.equality (==, !=) of range()

a=range(1,10,2)

print(list(a))

print(a[1])

print(3 in a)

print(a.index(7))

print(a[3])

b=range(1,10,3)

print(list(b))

c=range(1,9,3)

print(list(c))

print(b==c)

d=range(1,8,3)

print(list(d))

print(b==d)

e=range(1,7,3)

print(list(e))

print(b==e)
[1, 3, 5, 7, 9]
3
True
3
7
[1, 4, 7]
[1, 4, 7]
True
[1, 4, 7]
True
[1, 4]
False
原文地址:https://www.cnblogs.com/wordchao/p/9244827.html