python脚本编程100例_python 100例 (持续更新)

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 n

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值