Python编程从入门到实践:
# -*- coding: utf-8 -*-
"""
chapter4 操作列表
"""
import math
# 4.1 遍历列表 for循环
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)
# 4.1.1 深入的研究循环
# 4.1.2 在for循环中执行更多的操作
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician.title() + ', that was a great trick!')
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print("i can't wait to see your next trick" + magician.title() + '.\n')
# 4.1.3 在for循环结束后执行一些操作
magicians = ['alice', 'david', 'carolina']
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')
print('thanks everyone,that was a great show') # 不缩进 将在循环结束后执行
# 4.2 避免缩进错误
'''
一些常见的缩进错误:
1.忘记缩进-python使用缩进代码行与前一个代码行的关系;
2.忘记缩进额外的代码行-注意你的逻辑,你要对便利出来的每个元素执行操作,还是要在循环结束后,执行操作;
3.不必要的缩进
4.遗忘了for之后的冒号
'''
# 4.3 创建数值列表
# 4.3.1 使用函数range()生成一系列数字
for number in range(1, 5): # 左闭右开 ,实际上会打印1,2,3,4 而不会打印出5
print(number)
for number2 in range(5): # 会打