注意一下 range 的用法。查一下 range 函数并理解它
在第 22 行(我的答案),你可以直接将 elements 赋值为 range(0, 6) ,而无需使用 for 循环?
在 python 文档中找到关于列表的内容,仔细阅读一下,除了 append 以外列表还支持哪些操作?

我的答案

32.0 基础练习

 the_count = [1,2,3,4,5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count:
print(f"This is count {number}") # same as above for fruit in fruits:
print(f"A fruit of type: {fruit}") # also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it for i in change:
print(f"I got {i}") # we can also build lists, first start with an empty one
elements = [] #then use the range function to do 0 to 5 counts for i in range(0,6):
print(f"Adding {i} to the list.")
# append is a function that lists understand
elements.append(i) # now we can print them out too
for i in elements:
print(f"Element was: {i}") #自己加的,看elements结果
print(elements)

32.1 range 的用法

最简单的变法就是查看帮助文档了,还记得我们用过的两种方法么? 
( ↓↓↓↓ 刮开查看 ↓↓↓↓ )

Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表。

Python3 list() 函数是对象迭代器,可以把range()返回的可迭代对象转为一个列表,返回的变量类型为列表。

range(stop)
range(start, stop[, step])

创建 range 函数的实例 
range 函数会返回一个数字序列。它最多接受 3 个参数

      • start:启始值(被包含),默认是 0
      • stop:结束值(不包含),必填
      • step:步长,默认是1,不可为0,例如:range(0, 5) 等价于 range(0, 5, 1)
 >>>range(5)
range(0, 5)
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(0))
[]
>>>

有两个参数或三个参数的情况(第二种构造方法)::

 >>>list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(1, 0))
[]
>>>
>>>

32.2 不使用 for-loop ,直接为 elements 赋值为 range(6)

 the_count = [1,2,3,4,5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters'] # this first kind of for-loop goes through a list for number in the_count:
print(f"This is count {number}") # same as above for fruit in fruits:
print(f"A fruit of type: {fruit}") # also we can go through mixed lists too
# notice we have to use {} since we don't know what's in it for i in change:
print(f"I got {i}") # we can also build lists, first start with an empty one
elements = [] #then use the range function to do 0 to 5 counts elements = list(range(0,6)) # now we can print them out too
for i in elements:
print(f"Element was: {i}") #自己加的,看elements结果
print(elements)

需要为range(0,6)加上list才是需要的elements列表

《笨方法学Python》加分题32的更多相关文章

  1. "笨方法学python"

    <笨方法学python>.感觉里面的方法还可以.新手可以看看... 本书可以:教会你编程新手三种最重要的技能:读和写.注重细节.发现不同.

  2. 笨方法学python 22,前期知识点总结

    对笨方法学python,前22讲自己的模糊的单词.函数进行梳理总结如下: 单词.函数 含义 print() 打印内容到屏幕 IDLE 是一个纯Python下自带的简洁的集成开发环境 variable ...

  3. 笨办法学python 13题:pycharm 运行

    笨办法学python 13题 代码: # -*- coding: utf-8 -*- from sys import argv # argv--argument variable 参数变量 scrip ...

  4. 《笨方法学Python》加分题20

    加分练习通读脚本,在每一行之前加注解,以理解脚本里发生的事情.每次 print_a_line 运行时,你都传递了一个叫 current_line 的变量,在每次调用时,打印出 current_line ...

  5. 《笨方法学Python》加分题17

    题目通过前学习的文件操作把一个文件中的内容拷贝到另一个文件中,并使用 os.path.exists 在拷贝前判断被拷贝的文件是否已经存在,之后由用户判断是否继续完成拷贝. 新知识os.path.exi ...

  6. 《笨方法学Python》加分题15

    本题本题开始涉及文件的操作,文件操作是一件危险的事情,需要仔细细心否则可能导致重要的文件损坏. 本题除了 ex15.py 这个脚本以外,还需要一个用来读取的文件 ex15_sample.txt 其内容 ...

  7. 《笨方法学Python》加分题33

    while-leep 和我们接触过的 for-loop 类似,它们都会判断一个布尔表达式的真伪.也和 for 循环一样我们需要注意缩进,后续的练习会偏重这方面的练习.不同点在于 while 循环在执行 ...

  8. 《笨方法学Python》加分题29

    加分练习猜一猜 “if 语句” 是什么,他有什么作用.在做下一道题之前,试着用自己的话回答下面的问题: 你认为 if 对他下一行代码做了什么?为什么 if 语句的下一行需要 4 个空格缩进?如果不缩进 ...

  9. 《笨方法学Python》加分题28

    #!usr/bin/python # -*-coding:utf-8-*- True and True print ("True") False and True print (& ...

随机推荐

  1. 逆向工程vgenerator(二)

    前言 接上篇,这一篇主要的工具类和工厂类,包括数据库方法.通用方法,三个工厂. 常量 /** *author vvxtoys *默认xml开头 *文档分隔 *默认方法名 */ package cc.v ...

  2. 74.纯 CSS 创作一台 MacBook Pro

    原文地址:https://segmentfault.com/a/1190000015568609 HTML code: <div class="macbook"> &l ...

  3. ubuntu10.04 搭建海思开发环境

    (1)Ubuntu 10.04.4 LTS (Lucid Lynx) 下载地址:http://old-releases.ubuntu.com/releases/lucid/ (2)passwd roo ...

  4. LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. springboot学习随笔(一):springboot环境构建:eclipse+maven+jdk1.8

    一:所需环境 1.jdk1.8(配置环境变量,可自行搜索相关文档) 2.maven(maven的配置不在赘述,可自行搜索相关文档) 3.eclipse(第三种方式,eclipse集成sts时需要,直接 ...

  6. LWIP之ARP协议

    描述ARP缓存表的数据结构: struct etharp_entry { struct etharp_q_entry *q; //数据包缓冲队列指针 ip_addr_t ipaddr; //目标IP地 ...

  7. 电脑开机出现intel UNDI,PXE-2.1(build 003),是怎么回事?

    restore defaults恢复出厂设置 https://jingyan.baidu.com/article/20b68a88587205796cec6290.html

  8. FIN_WAIT_2状态解释

    关于网络设备的FIN_WAIT_2状态解释出处:http://hi.baidu.com/netdemon1981/blog/item/584bfbb2aeb1d4acd9335ad9.html 在HT ...

  9. nodeJs 控制台打印中文显示为Unicode解决方案

    在使用 NodeJs 采集其他网站网页时遇到的,在获取源代码后发现里面原来的中文被转成了 Unicode(UTF8) 编码的中文(如:&# [xxx]),这当然不是真正想要的中文实体 解决方案 ...

  10. 查看log日志

    本地环境的的log日志 可以直接查看, 对于新手来说怎么查看正式环境下的log日志呢 1, SSH到服务器 2,cd 到logs所在目录 3, tail -f 对应日志名字