习题32 循环和列表

学习目标:

ex32.py


学习内容:

the_count = [1,2,3,4,5]
fruits = ['apples','orange','pear','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 though mixed lists too我们也可以通过混合列表
#notice we have to use {} since we don't konw what's in it请注意,我们必须使用 {},因为我们不知道里面有什么
for i in change:
    print(f"I got {i}")

#we can slso build lists,first start with an empty one先从一个空的开始
elements = []

#then use the range function to do 0 to 5 counts范围函数进行 0 到 5 次计数
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"Elements was:{i}")
This is count 1
This is count 2
This is count 3
This is count 4
This is count 5
A fruit of type: apples
A fruit of type: orange
A fruit of type: pear
A fruit of type: apricots
I got 1
I got pennies
I got 2
I got dimes
I got 3
I got quarters
Adding 0 to the list
Adding 1 to the list
Adding 2 to the list
Adding 3 to the list
Adding 4 to the list
Adding 5 to the list
Elements was:0
Elements was:1
Elements was:2
Elements was:3
Elements was:4
Elements was:5

学习时间:

2023.4.20


学习产出:

ex32.py

你可能感兴趣的:(python,python)