学习内容:
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
# nothing we have to use {} since wu 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}")
运行结果:
知识点:
1.for循环
基本结构:for var in list: (var为variable,list为序列),注意后面必须有冒号(😃
例1:
something = [1, 2, 3]
for i in something:
print (i)
结果:
例2:
dic = {'k1':'v1', 'k2':'v2', 'k3':'v3'}
for k,v in dic:
print (k, v)
例3:
aString = "Hello fearlazy"
for letter in aString:
print (letter)
结果:
例4:
range()
表示一个范围,第一个for
只有一个参数,表示终点(不包含这个值)。第二个for
中的两个参数代表起点(包含这个值)和终点(不包含这个值)。range
还包含第三个参数表示步长,就是每次增加多少,默认为1。
for i in range(4):
print (i)
print ('_' * 15)
for i in range(2, 4):
print (i)
结果:
例5:
else
和for
一起使用,只有for
循环正常终止(不是遇到break语句)时才执行else
块。
for i in range(3):
print (i)
else:
print ("over!")
结果:
2.range()函数
range()返回的是一个可迭代对象(类型是对象),而不是列表类型,打印的时候不会打印列表。
list()的对象为迭代器,可以把range()返回的可迭代对象转为一个列表,返回的变量类型为列表
语法:①range(stop): ② range(start, stop [, step])
start:从start开始,默认为0,例:range(5)
结果:range(0, 5)
stop:计数到stop为止,但不包括stop。例 range(0, 5)
结果[0, 1, 2 ,3 ,4],没有5
step:步长,默认为1.例 range(0, 6, 2)
结果[0, 2, 4]
3.append()函数
append():在列表末尾添加新的对象
语法:list.append(obj) obj:添加到列表末端的对象
例:
list1 = ['Google', 'Runoob', 'Taobao']
list1.append('Baidu')
print ("The updated list:", list1)
结果:
4.分解脚本:
the_count = [1, 2, 3, 4, 5]
fruits = ['apples', 'oranges', 'pears', 'apricots']
change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
# 打印列表the_count
# this first kind of for-loop goes through a list
for number in the_count:
print (f"This is count {number}")
# 打印列表fruits
# same as above
for fruit in fruits:
print (f"A fruit of type: {fruit}")
# 打印列表change
# also we can go through mixed lists too
# nothing we have to use {} since wu don't know what's in it
for i in change:
print (f"I got {i}")
# 创建一个空列表elements
# we can also build lists, first start with an empty one
elements = []
# 给空列表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)
# 打印列表elements
# now we can print them out too
for i in elements:
print (f"Element was: {i}")
5.补充内容:
① 若想获取第几个数字可以如下:
x = range(0, 20, 3)
print (x [0])
print (x [1])
②倒序排列:如果是列表,可以直接用reversed(seq),seq为需要转换的序列。可以是tuple(元组)、string(字符串)、list(列表)或range(值域)。
例1
a = [1, 2, 3, 4, 5]
for x in reversed(a):
print (x)
结果
例2
a = [1, 2, 3, 4, 5]
for x in range(len(a)-1, -1, -1):
print (a[x])
结果