the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# 遍历列表the_count(int)的for循环,可以使用%d和%s和%r
for number in the_count:
print("This is count %d" % number)
# 遍历列表fruits(str)的for循环,可以使用%s和%r,不能使用%d
for fruit in fruits:
print("A fruit of type: %s" % fruit)
# 遍历列表fruits(str和int)的for循环,可以使用%s和%r,不能使用%d
for i in change:
print("I got %s" % i)
# 新建空列表
elements = []
# 使用range函数从0至5,range()函数会从第一个数到最后一个数,但不包含最后一个数
for i in range(0, 6):
print("Adding %d to list." % i)
# 列表末尾逐个添加元素使用append函数
elements.append(i)
# 再次打印新列表
for i in elements:
print("Element was: %d" % i)
ex32——循环和列表
最新推荐文章于 2024-06-12 11:47:29 发布