热线电话:13121318867

登录
2018-11-19 阅读量: 721
Python里的for和while

可以用 if 语句来执行一种有条件的行动:

if 1 > 2:
message = "if only 1 were greater than two..."
elif 1 > 3:
message = "elif stands for 'else if'"
else:
message = "when all else fails use else (if you want to)"

也可以在一行语句中使用 if-then-else,我们有时候需要这么做:

parity = "even" if x % 2 == 0 else "odd"

Python 也有 while 循环:

x = 0
while x < 10:
print x, "is less than 10"
x += 1

尽管我们更常用 for 和 in:

for x in range(10):
print x, "is less than 10"
0.0000
3
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子