{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.5 Range"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range 类型表示不可变的数字序列,通常用于在 for 循环中指定循环次数,是for循环中应用最广泛的可迭代对象之一。 \n",
"range()是python中的一个内置函数,用于生成一系列连续的整数,创建一个整数列表,一般用在 for 循环中。 \n",
"range()函数既可用于控制循环,又可以用等差数列中的数求解很多数学问题。 \n",
"range()函数语法: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n"
]
}
],
"source": [
"# range(stop) # 0, 1, 2, 3, 4, …stop-1,初值为0,步长为1的等差数列\n",
"# range(start, stop[, step]) # start, start+step, start+2*step…,步长为step的等差数列\n",
"\n",
"for i in range(10):\n",
" print(i)\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"start 和 step 是可选参数,缺省时,start=0, step=1。 \n",
"range 生成的内容:\n",
"r[i] = start + step * i \n",
"\n",
"当 step 为正数,要求 i >= 0 且r[i] < stop; \n",
"当 step 为负数,要求 i >= 0 且 r[i] > stop。\n",
"range()具有一些特性:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.要全部输出range()生成的序列,不能直接输出range(n) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可以用print(<font color=Red>__list__</font>(range(n))) 的方法,将生成的序列转成列表形式输出。 \n",
"可以用print(<font color=Red>__tuple__</font>(range(n)))的方法,将生成的序列转成元组的形式输出。 \n",
"可以用print(<font color=Red>__*__</font>(range(n)))将其内容解包输出。 "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"range(0, 5)\n",
"[0, 1, 2, 3, 4]\n",
"(0, 1, 2, 3, 4)\n",
"0 1 2 3 4\n"
]
}
],
"source": [
"print(range(5)) # 输出range(0, 5),此语句不能输出整数序列\n",
"print(list(range(5))) # 输出[0, 1, 2, 3, 4]\n",
"print(tuple(range(5))) # (0, 1, 2, 3, 4)\n",
"print(*range(5)) # 解包输出:0 1 2 3 4\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font face='楷体' color='red' size=5> 练一练1 </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"编程第一行输出从0到10的整数列表、第二行输出从0到10的整数列表元组,第三行输出从0到10的整数,各数字间用空格分隔。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"23\n",
"21\n",
"19\n",
"17\n",
"15\n",
"13\n",
"11\n",
"9\n"
]
}
],
"source": [
"# 补充你的代码\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"测试用例:\n",
"输出\n",
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)\n",
"0 1 2 3 4 5 6 7 8 9"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. start、stop、step都必须是<font color=Red>__整数__</font>,否则抛出TypeError异常。 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"当 stop 值小于start 值且步长为正数时,返回的序列为空。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(range(3.5))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(range(1, 8)) # 8.0是值等于整数8的浮点数,不是整数"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(list(range(8, 1))) # range(8, 1)返回空序列,转列表结果为空列表"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. 如果<font color=Red>__start__</font>参数缺省,默认值为<font color=Red>__0__</font>; "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"如果<font color=Red>__step__</font>参数缺省,默认值为<font color=Red>__1__</font>; \n",
"当试图设置<font color=Red>__step__</font>为0时,会抛出ValueError异常。 "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# start参数缺省,默认值为0\n",
"print(*(range(8))) # *将range对象解包,取出里面对象输出0 1 2 3 4 5 6 7\n",
"print(list(range(8))) # range(8)返回空序列,转列表结果为空列表[0, 1, 2, 3, 4, 5, 6, 7]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# step参数缺省,默认值为1\n",
"print(*(range(1, 8))) # 输出1 2 3 4 5 6 7\n",
"print(list(range(1, 8))) # 输出[1, 2, 3, 4, 5, 6, 7]\n",
"\n",
"# step值为2\n",
"print(*(range(1, 8, 2))) # 输出1 3 5 7"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(*(range(1, 8, 0))) # step为0时,会抛出ValueError异常\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. 当step是正整数时,产生的序列<font color=Red>__递增__</font>; 当step为负整数时,生成的序列<font color=Red>__递减__</font>。 "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 3 5 7\n",
"8 6 4 2\n"
]
}
],
"source": [
"print(*(range(1, 8, 2))) # step是正整数时,产生的序列递增\n",
"print(*(range(8, 1, -2))) # step为负整数时,生成的序列递减"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font face='楷体' color='red' size=5> 练一练2 </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"编程在一行中输出从1到50的奇数,各数字间用空格分隔"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 补充你的代码\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"测试用例:\n",
"输出\n",
"1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. range()函数产生一个<font color=Red>__左闭右开__</font>的序列"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(*(range(5))) # 输出0 1 2 3 4,包含左边界0,不包括右边界5\n",
"print(*(range(1, 5)))\n",
"print(*(range(1, 5, 2)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <font face='楷体' color='red' size=5> 练一练3 </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"编程在一行中输出从1到10的整数,各数字间用空格分隔"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 补充你的代码\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"测试用例:\n",
"输出\n",
"1 2 3 4 5 6 7 8 9 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6. range()函数产生的是<font color=Red>__可迭代对象__</font>,不是迭代器,也不是列表类型。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可迭代对象(Iterable):\n",
"\n",
"凡是可以作用于for循环的对象都是可迭代对象,可用collections模块的Iterable来判断。\n",
"\n",
"迭代器(Iterator):\n",
"\n",
"不仅可以作用于for循环,还能作用于next()函数的对象是迭代器,表示一个惰性计算的序列,可用collections模块的Iterator来判断。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import Iterable, Iterator\n",
"\n",
"print(isinstance(range(8), Iterable)) # 查看是否为可迭代对象,结果 True\n",
"print(isinstance(range(8), Iterator)) # 查看是否为迭代器,结果 False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1, 10):\n",
" print(i) # 分行输出1到9的数字\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1, 10):\n",
" print(f'{i} x 1 = {1*i}', end=' ') # 输出乘法表的一行\n",
"# 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5 6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7. range对象是<font color=Red>__不可变数据类型__</font>,可用<font color=Red>__索引、切片__</font>等操作<font color=Red>__获取__</font>其中部分数据,但不可修改其中的数据。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(range(10)[2]) # 索引,根据序列返回一个元素,2\n",
"print(range(10)[2:5]) # 切片,返回序列的一部分,range(2, 5)\n",
"range(10)[2] = 5 # 修改range中序号为2的元素的值,触发异常\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 惰性求值"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range()函数相对于列表和元组的优点在于占用内存固定,占用内存较小,他仅存储start、stop和step值,在需要时通过计算生成序列中的每个值。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"print(range(10))会输出对象range(0, 10),说明range采用了惰性求值的方式产生数据,使用print()函数打印range数据并不能直接看到range数据的具体元素。但可以通过将其转换为列表或元组的方式,查看其生成的具体数据。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range(10000)可以按顺序产生一组数字0 , 1, 2, 3, 4,… 9998, 9999,但并不会一次将这些数据生成放在内存中,只有使用到其中的某个值时,range才会产生该值,可以减少生成数据和将数据写入内存的时间开销,对于某些应用来讲效率会非常高。如下例所示:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1000000000): # 存储的是range(1,1000000000)对象\n",
" if i < 5:\n",
" print(i, end=' ') # 输出0 1 2 3 4,只产生这5个数字"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"range()函数常与for和控制变量一起使用,遍历range()数列中的数并控制循环次数。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for <variable> in range([start,]stop[,step]):\n",
" <语句块>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"variable 为循环控制变量,经常用i、j等表示,每次循环从range()产生的数列中依次取一个值。首次进入循环时,变量取最小的值,即start值,当start缺省时,取值为0,后面每次循环依次取前一个值加步长step的值,当step值缺省时,取前一个值加1。"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}