Python range()

John Smith發表於2016-03-28
'''
range() is a built-in function

return: a list of integers

it could be used like this:
    range(stop)
    range(start, stop)
    range(start, stop, step)

in terminal:
    python
    help(range)
    q
    exit()
    clear
'''

list = range(5)
print(list)
# [0, 1, 2, 3, 4]
#
# stop is excluded
# default start is 0
# default step  is 1

# default start is 0
# default step is 1
# stop is excluded
# start and stop should not be ==
list = range(0)
print(list)
# []

# stop should not be == start
list = range(1, 1)
print(list)
# []

# default start is 0
# default step is 1
# stop should not be smaller than start
list = range(-1)
print list
# []

# stop should not be smaller than start
list = range(5, 3)
print list
# []

# when stop IS smaller than start, step should be a negative
list = range(5, 3, -1)
print list
# [5, 4]

# same with negative
# stop should not be smaller than start
list = range(-3, -5)
print list
# []

# when stop is smaller than start, step should be a negative
list = range(-3, -5, -1)
print list
# [-3, -4]

list = range(-5, -3)
print list
# [-5, -4]

相關文章