If
For-loop
# Make sure that you use while-loops sparingly. Usually a for-loop is better.
# Python expects you to indent something after you end a line
with a : (colon).
if people < cats:
print "Too many cats! The world is doomed!"
if cars > people:
print "We should take the cars."
elif cars < people:
print "We should not take the cars."
else:
print "We can't decide."
if people > buses:
print "Alright, let's just take the buses."
else:
print "Fine, let's stay home then."For-loop
the_count = [1, 2, 3, 4, 5]
for number in the_count:
print "This is count %d" % number
elements = []
for i in range(0, 6):
print "Adding %d to the list." % i
elements.append(i)
for i in elements:
print "Element was: %d" % i
# Make sure that you use while-loops sparingly. Usually a for-loop is better.
本文介绍了Python编程中的条件判断(if语句)和循环(for与while循环),通过实例展示了如何使用这些基本控制结构进行简单的逻辑处理。
646

被折叠的 条评论
为什么被折叠?



