习题32:循环和列表
theCount = [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 theCount:
print("This is count %d" % number)
# same as above
for fruit in fruits:
print("A fruit of type:%s" % fruit)
# also we can go through mixed lists too
# notice we have to use %r since we don't know what's in it
for i in change:
print("I got %r" % 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("Adding %d to the list." % i)
# append is a function that lists understand
elements.append(i)
# now we can print them out too
for i in elements:
print("Element was:%d" % i)
三个实例for-loop
range(n1,n2),包含n1不包含n2
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:oranges
A fruit of type:pears
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.
Element was:0
Element was:1
Element was:2
Element was:3
Element was:4
Element was:5
1. 注意一下 range 的用法。查一下 range 函数并理解它。
2. 在第 22 行,你可以可以直接将 elements 赋值为 range(0,6),而无需使用 for 循环?
3. 在 Python 文档中找到关于列表的内容,仔细阅读以下,除了 append 以外列表还支持哪些操作?
习题33:While循环
i = 0
numbers = []
while i < 6:
print("At the top i is %d" % i)
numbers.append(i)
i += 1
print("Numbers nuw: ", numbers)
print("At the bottom i is %d" % i)
print("The numbers: ")
for num in numbers:
print(num)
At the top i is 0
Numbers nuw: [0]
At the bottom i is 1
At the top i is 1
Numbers nuw: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers nuw: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers nuw: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers nuw: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers nuw: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0
1
2
3
4
5
1. 将这个 while 循环改成一个函数,将测试条件(i < 6)中的 6 换成一个变量。
def outputNumber(number):
i = 0
numbers = []
while i < number:
print("At the top i is %d" % i)
numbers.append(i)
i += 1
print("Numbers nuw: ", numbers)
print("At the bottom i is %d" % i)
print("The numbers: ")
for num in numbers:
print(num)
又想吐槽一下,由于变量没有定义,导致函数对象的参数的类型根本不清楚,很容易弄错。。。反正就是觉得python不行,呵呵
2. 使用这个函数重写你的脚本,并用不同的数字进行测试。
3. 为函数添加另外一个参数,这个参数用来定义第 8 行的加值 + 1 ,这样你就可以让它任意加值了。
4. 再使用该函数重写一遍这个脚本。看看效果如何。
5. 接下来使用 for-loop 和 range 把这个脚本再写一遍。你还需要中间的加值操作吗?如果你不去掉它,会有什么样的结果?
def outputNumber1(number):
numbers = []
for i in range(0, number):
print("At the top i is %d" % i)
numbers.append(i)
i += 1
print("Numbers nuw: ", numbers)
print("At the bottom i is %d" % i)
print("The numbers: ")
for num in numbers:
print(num)
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)