# Python range()函数用法介绍和示例

range()是Python的一个内置函数。当用户需要执行某个动作的特定次数时使用。Python(3.x)中的range()只是Python(2.x)中名为xrange的函数的重命名版本。range()函数用于生成一个数字序列。

range()通常用于for循环，因此，在处理任何类型的Python代码时，相同的知识都是关键方面。range()函数在Python中最常见的用法是迭代序列类型(List, string等)。With for和while循环。

Python range()基础:

• start：整数, 从该整数开始返回整数序列
• stop：整数, 在该整数之前要返回整数序列。
整数范围在停止处结束– 1。
• step：整数值, 确定序列中每个整数之间的增量
``````# Python Program to
# show range() basics

# printing a number
for i in range ( 10 ):
print (i, end = " " )
print ()

# using range for iteration
l = [ 10 , 20 , 30 , 40 ]
for i in range ( len (l)):
print (l[i], end = " " )
print ()

# performing sum of natural
# number
sum = 0
for i in range ( 1 , 11 ):
sum = sum + i
print ( "Sum of first 10 natural number :" , sum )``````

``````0 1 2 3 4 5 6 7 8 9
10 20 30 40
Sum of first 10 natural number : 55``````

• range(stop)有一个参数。
• range(start, stop)有两个参数。
• range(start, stop, step)需要三个参数。

#### range(stop)

``````# Python program to
# print whole number
# using range()

# printing first 10
# whole number
for i in range ( 10 ):
print (i, end = " " )
print ()

# printing first 20
# whole number
for i in range ( 20 ):
print (i, end = " " )``````

``````0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``````

#### range(start, stop)

``````# Python program to
# print natural number
# using range

# printing a natural
# number upto 20
for i in range ( 1 , 20 ):
print (i, end = " " )
print ()

# printing a natural
# number from 5 t0 20
for i in range ( 5 , 20 ):
print (i, end = " " )``````

``````1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``````

#### range(start, stop, step)

``````# Python program to
# print all number
# divisible by 3 and 5

# using range to print number
# divisible by 3
for i in range ( 0 , 30 , 3 ):
print (i, end = " " )
print ()

# using range to print number
# divisible by 5
for  i in range ( 0 , 50 , 5 ):
print (i, end = " " )``````

``````0 3 6 9 12 15 18 21 24 27
0 5 10 15 20 25 30 35 40 45``````

``````# Python program to
# increment with
# range()

# incremented by 2
for i in range ( 2 , 25 , 2 ):
print (i, end = " " )
print ()

# incremented by 4
for i in range ( 0 , 30 , 4 ):
print (i, end = " " )
print ()

# incremented by 3
for i in range ( 15 , 25 , 3 ):
print (i, end = " " )``````

``````2 4 6 8 10 12 14 16 18 20 22 24
0 4 8 12 16 20 24 28
15 18 21 24``````

``````# Python program to
# decrement with
# range()

# incremented by -2
for i in range ( 25 , 2 , - 2 ):
print (i, end = " " )
print ()

# incremented by -4
for i in range ( 30 , 1 , - 4 ):
print (i, end = " " )
print ()

# incremented by -3
for i in range ( 25 , - 6 , - 3 ):
print (i, end = " " )``````

``````25 23 21 19 17 15 13 11 9 7 5 3
30 26 22 18 14 10 6 2
25 22 19 16 13 10 7 4 1 -2 -5``````

Python range()函数不支持浮点数。即用户不能在任何参数中使用浮点数或非整数。用户只能使用整数。例如

``````# Python program to
# show using float
# number in range()

# using a float number
for i in range ( 3.3 ):
print (i)

# using a float number
for i in range ( 5.5 ):
print (i)``````

``````for i in range(3.3):
TypeError: 'float' object cannot be interpreted as an integer``````

#### 两个range()函数的串联

``````# Python program to concatenate
# the result of two range functions

from itertools import chain

# Using chain method
print ( "Concatenating the result" )
res = chain( range ( 5 ), range ( 10 , 20 , 2 ))

for i in res:
print (i, end = " " )``````

``````Concatenating the result
0 1 2 3 4 10 12 14 16 18``````

#### 用索引值访问range()

``````# Python program to demonstrate
# range function

ele = range ( 10 )[ 0 ]
print ( "First element:" , ele)

ele = range ( 10 )[ - 1 ]
print ( "\nLast element:" , ele)

ele = range ( 10 )[ 4 ]
print ( "\nFifth element:" , ele)``````

``````First element: 0

Last element: 9

Fifth element: 4``````

range()函数仅适用于整数, 即整数。

range()是Python中的一种类型

``````# Python program to
# show range() type

# checking a type of
# range
type ( range ( 3 ))``````

``````# Python program to
# access items in a range

# accessing a items
range ( 3 )[ 1 ]

# accessing a items
range ( 3 )[ 2 ]``````

• 回顶