- 论坛徽章:
- 0
|
有谁能帮忙贴出 python程序用的基本结构
详细请看Python的Manual文档 Tutorial的第4章
if语句
- >;>;>; x = int(raw_input("Please enter an integer: "))
- >;>;>; if x < 0:
- ... x = 0
- ... print 'Negative changed to zero'
- ... elif x == 0:
- ... print 'Zero'
- ... elif x == 1:
- ... print 'Single'
- ... else:
- ... print 'More'
- ...
复制代码
for语句
- >;>;>; # Measure some strings:
- ... a = ['cat', 'window', 'defenestrate']
- >;>;>; for x in a:
- ... print x, len(x)
- ...
- cat 3
- window 6
- defenestrate 12
复制代码
用range函数实现计数循环
- >;>;>; for i in range(3): print i, 'Pythons'
- ...
- 0 Pythons
- 1 Pythons
- 2 Pythons
复制代码
while语句
- >;>;>; x = 'spam'
- >;>;>; while x:
- ... print x,
- ... x = x[1:]
- ...
- spam pam am m
复制代码
break continue pass else循环
break 跳出循环
continue 跳到循环顶部
pass 什么也不做,只是一个占位的空语句
else 运行并且只有在循环正常退出的情况下运行
- while <条件测试>;:
- <语句>;
- if <条件测试>;: break #现在跳出循环,忽略else
- if <条件测试>;: continue #现在转到循环顶部
- else:
- <语句>; #如果没有遇到break
复制代码 |
|