range&xrange
Python 2
  • range() returns a list
  • xrange(), returns a iterable
  • l = range(4)
    print(type(l)) # list
    print(l) # [0, 1, 2, 3]
    
    for e in l:
        print(e)
    
    x = xrange(4)
    print(type(x)) # xrange
    print(list(x)) # [0, 1, 2, 3]
    
    for e in x:
        print(e)
    		
    Python 3
  • range() returns a range, which is a iteration and can be converted to list by calling list(), it is more like xrange() in Python 2
  • xrange() has been removed in Python 3
  • #!/usr/bin/python
    
    r = range(0, 10, 1)
    print(type(r)) # range
    print(len(r)) # 10
    print(list(r)) # convert a range to list, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    for e in r:
        print(e, end=' ')
    print()