Python range

来源:互联网 发布:保温杯十大排名 知乎 编辑:程序博客网 时间:2024/05/16 07:21

range is a Python function for integer number

1、range(int_num),输出一个从0-(int_num-1)的list

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

2、range(start, end),输出一个从start到end-1的list

range(1,5)1,2,3,4

3 range(start, end, steps) will product a list from start to end by steps. The list`s last element will not larger than end - 1.

range(1,6,2)[1,3,5]range(2,6,2)[2,4] 

frange is a Python function for float number, its head file is pylab. You should import pylab when you use this function.

1 frange(int_num) will product a list from 0 to int_num

pylab.frange(4)[ 0.  1.  2.  3.  4.]# it`s from 0 to 4
2 frange(start, end) will product a list from start to end and the step is 1.0

print pylab.frange(1,4)[ 1.  2.  3.  4.]
3 frange(start, end, steps) will product a list from start to end by steps.

pylab.frange(1,4,0.5)[ 1.   1.5  2.   2.5  3.   3.5  4. ]pylab.frange(1,4.4,0.5)[ 1.   1.5  2.   2.5  3.   3.5  4.   4.5]

xrange is a Python function. Its use and parameters are same with range. But it is looked as a type and return an object, the object is iterable,  rather than a list. It is faster and less memory than range

>>>xrange(1,3)# produce a objectxrange(1,3)>>>x = xrange(1,3)>>>xxrange(1,3)>>>x[1]2>>>range(1,3)# produce a list[1,2]

 








原创粉丝点击