第一节
1 介绍了for循环的用法
for variable in values:
statement
2 for循环打印出列表的每一项
for item in [1 , 2 , 3]:
print item
那么将打印出1,2,3
3 练习:使用for循环,把列表中的每一项打印出来
names = ["Adam","Alex","Mariah","Martine","Columbus"]
# use for loop
for str in names:
print str
第二节
1 介绍了我们可以使用for循环打印出字典中的每一个key
2 比如这个例子,我们可以打印出key为foo的value值为bar
# A simple dictionary
d = {"foo" : "bar"}
for key in d:
# prints "bar"
print d[key]
3 练习:打印出字典webster的所有key对应的value
webster = {
"Aardvark" : "A star of a popular children's cartoon show.",
"Baa" : "The sound a goat makes.",
"Carpet": "Goes on the floor.",
"Dab": "A small amount."
}
# Add your code below!
for key in webster:
print webster[key]
第三节
1 介绍了for里面我们可以添加if/else语句来判断
2 比如
for item in numbers:
if condition:
# Do something
3 练习:只输出列表中的7个数
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
for num in a:
if(a < 7):
print a