master
/ 3.3.3 rang.ipynb

3.3.3 rang.ipynb @master

341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
d487d71
341084b
 
 
d487d71
341084b
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
d487d71
 
 
 
 
 
 
 
 
 
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
 
 
 
 
 
 
 
 
d487d71
341084b
 
 
 
 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
{
 "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. startstopstep都必须是<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()函数相对于列表和元组的优点在于占用内存固定占用内存较小他仅存储startstop和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 为循环控制变量经常用ij等表示每次循环从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
}