Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)
1. 利用for循环遍历整个列表
magicians = ['alice', 'dsvid', 'carolina']
# 遍历整个列表
for magician in magicians:
print(magician)
2. 使得打印结果变得更加有实际意义
for magician in magicians:
print(magician.title() + ', that was a great trick!')
运行结果:
Alice, that was a great trick!
Dsvid, that was a great trick!
Carolina, that was a great trick!
for代码行后边缩进的代码块都是循环的一部分,继续增加打印语句:
for magician in magicians:
print(magician.title() + ', that was a great trick!')
print("I can't wait to see your next trick, " + magician.title() + "\n")
运行结果:

3. 对于for循环后边的属于循环模块的代码行一定要缩进。
3.1 若没有缩进:
for magician in magicians:
print(magician.title() + ', that was a great trick!')
运行结果会报错:
IndentationError: expected an indented block
3.2 若有缩进的,有没有缩进的:
for magician in magicians:
print(magician.title() + ', that was a great trick!')
print("I can't wait to see your next trick, " + magician.title() + "\n")
程序运行不会出错,但是没有缩进的代码将在循环结束之后执行一次,只打印出有关列表最后一个元素的信息:
Alice, that was a great trick!
Dsvid, that was a great trick!
Carolina, that was a great trick!
I can't wait to see your next trick, Carolina
3.3 若缩进了本应在循环结束之后执行的代码,则这些代码将针对每个元素循环执行一次,程序不会报错。
4. 对于for循环还要注意的一点是——for语句的末尾千万不要忘了冒号(太容易忘了,太容易忘了,太容易忘了。。。)。
SyntaxError: invalid syntax
Python中遍历整个列表及注意点(参考书籍Python编程从入门到实践)的更多相关文章
- python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍
目录 python学习第八讲,python中的数据类型,列表,元祖,字典,之字典使用与介绍.md 一丶字典 1.字典的定义 2.字典的使用. 3.字典的常用方法. python学习第八讲,python ...
- python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍
目录 python学习第七讲,python中的数据类型,列表,元祖,字典,之元祖使用与介绍 一丶元祖 1.元祖简介 2.元祖变量的定义 3.元祖变量的常用操作. 4.元祖的遍历 5.元祖的应用场景 p ...
- python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍
目录 python学习第六讲,python中的数据类型,列表,元祖,字典,之列表使用与介绍. 二丶列表,其它语言称为数组 1.列表的定义,以及语法 2.列表的使用,以及常用方法. 3.列表的常用操作 ...
- Python中多个列表与字典的合并方法
Python中多个列表与字典的合并方法 1多列表的合并 1)a+=b a=['] b = ['] a += b print(a) >>>['] 2) a.extend(b) a=[' ...
- python中如何删除列表中的所有元素
今天写代码的时候遇到一个小问题,Python中要删除列表中的所有元素.Python本身就提供了pop.remove.del这些删除的函数.我想着用循环实现,结果很麻烦.几番周折上了stackoverf ...
- Python编程从入门到实践笔记——操作列表
Python编程从入门到实践笔记——操作列表 #coding=utf-8 magicians = ['alice','david','carolina'] #遍历整个列表 for magician i ...
- Python编程从入门到实践笔记——列表简介
Python编程从入门到实践笔记——列表简介 #coding=utf-8 #列表——我的理解等于C语言和Java中的数组 bicycles = ["trek","cann ...
- Python编程从入门到实践笔记——用户输入和while循环
Python编程从入门到实践笔记——用户输入和while循环 #coding=utf-8 #函数input()让程序暂停运行,等待用户输入一些文本.得到用户的输入以后将其存储在一个变量中,方便后续使用 ...
- Python编程从入门到实践笔记——字典
Python编程从入门到实践笔记——字典 #coding=utf-8 #字典--放在{}中的键值对:跟json很像 #键和值之间用:分隔:键值对之间用,分隔 alien_0 = {'color':'g ...
随机推荐
- ICEM-管肋
原视频下载地址:https://yunpan.cn/cMgkmd7u9ZPdC 访问密码 8a73
- 找到树中指定id的所有父节点
const data = [{ id: 1, children: [{ id: 2, children: [{ id: 3, }, { id: 4, }], }], }, { id: 5, child ...
- appium+python 微信小程序的自动化
sudo kill -9 $(lsof -i:8889 -t) mitmweb -p 8889 -s addons.py mitmdump -q -p 8889 -s addons.py http: ...
- 修改layui的表单手机、邮箱验证可以为空怎么实现?
修改layui的表单手机.邮箱验证可以为空 解决办法: 修改源码: 把表单验证源代码(form.js)的正则表达式改一下,例如手机的正则为:/^1d{10}$/,可以改成/^$|^1d{10} ...
- linux postgresql
- JEECG hibernate.hbm2ddl.auto
配置hibernate根据实体类自动建表功能 - lixuyuan的专栏 - CSDN博客https://blog.csdn.net/lixuyuan/article/details/8057119 ...
- Visual Studio 2019更新到16.2.2
Visual Studio 2019更新到16.2.2 此次更新的变化有以下几点: (1)修复了Visual Studio在关闭时的停止响应问题.(2)修复CVE-2019-1211权限提升漏洞.(3 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第7节 SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf_28..SpringBoot Starter讲解
笔记 1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-b ...
- OctetString 转String
/// <summary> /// OctetString转时间 /// </summary> /// <param name="ss">字符串 ...
- Flutter生命周期
生命周期是一个组件加载到卸载的整个周期,熟悉生命周期可以让我们在合适的时机做该做的事情, flutter中的State生命周期和android以及React Native的生命周期类似. 大致可以分为 ...