1、题目:列表转换为字典。
程序源代码:
1 #!/usr/bin/env python
2 #-*- coding: UTF-8 -*-
3
4 i = ['a', 'b']5 l = [1, 2]6 printdict([i, l])
以上实例输出结果为:
{'a': 'b', 1: 2}
2、一个简单的while循环
1 #!/usr/bin/env python
2
3 count =04 while (count < 9):5 print 'The count is:', count6 count = count + 1
7
8 print "good bye"
以上实例输出的结果为:
The count is: 0
The countis: 1The countis: 2The countis: 3The countis: 4The countis: 5The countis: 6The countis: 7The countis: 8good bye
3、一个简单的循环continue
1 #!/usr/bin/env python
2 i = 1
3 while i < 10:4 i += 1
5 if i%2 >0:6 continue
7 print i
以上实例输出的结果为:
2
4
6
8
10
4、break的用法
1 #!/usr/bin/env python
2 i = 1
3 while 1:4 printi5 i += 1
6 if i > 10:7 break
以上实例的实验结果为:
1
2
3
4
5
6
7
8
9
10
5、 一个无限循环的小例子
1 #!/usr/bin/python
2 #-*- coding: UTF-8 -*-
3
4 var = 1
5 while var == 1: #该条件永远为true,循环将无限执行下去
6 num = raw_input("Enter a number:")7 print "You entered:", num8
9 print "Good bye!"
以上实例输出结果(使用ctrl + c 推出无限循环):
Enter a number:5You entered:5Enter a number:6You entered:6Enter a number:^CTraceback (most recent call last):
File"wuxian.py", line 6, in