6)代码块使用缩进
7)if语句 if expression: if_suite else: else_suite
if expression1: if_suite elif expression2: elif_suite else: else_suite
while expression: while_suite
--------- >>> counter=0 >>> while counter<5 SyntaxError: invalid syntax >>> while counter<5: print 'loop #%d' %(counter) counter=counter+1
loop #0 loop #1 loop #2 loop #3 loop #4
8)for循环和内建的range()函数 Python的for循环类似于shell脚本的foreach迭代类型的循环。 >>> print 'I like to use the Internet for:' I like to use the Internet for: >>> for item in ['e-mail','net-surfing','homework','chat']: print item
e-mail net-surfing homework chat --------- Python提供了内建的range()函数,产生一个列表。如下所示: >>> for eachNum in range(6): print eachNum;