天天看點

python 學習筆記(03)

python 的 if, while, for, break, continue 語句

python 中的語句與其它程式設計語言格式略有不同

if 語句:

    文法:if a = b :

         print " %s = %s" %(a,b)

       elif a < b :

        print " %s < %s " %(a,b)

       elif a > b :

        print " %s > %s " %(a,b)

例:

#!/usr/bin/env python

a = 10

b = int(raw_input("Enter A by number b:")) #此處是手動輸入,相當于read -p

if a == b :

    print "%s = %s " %(a,b)

elif a > b :

    print "%s > %s" %(a,b)

elif a < b :

else:

    print "erhuo,qingshuru shuzi!!!"

while 語句:

    文法:

    while 條件:

    else:

b = 10

running = True

while running:

    a = int(raw_input('Enter A Number b :'))

    if a == b

        print 'good,a is %s equal b is %s' $(a,b)

        running = False

    elif a < b:

        print 'No,it is little b'

        print 'No,it is grant b'

    print 'The while loop exec end !'

for 循環語句:

    for 變量 in 條件或指令:

<a href="http://s3.51cto.com/wyfs02/M00/75/07/wKiom1YwpyWCHZLBAAIO8YTRYlo047.jpg" target="_blank"></a>

break 語句:

    break 是終止語句的意思,滿足條件執行退出操作,具體如下:

<a href="http://s3.51cto.com/wyfs02/M02/75/04/wKioL1YwqMmyC6sgAAH5XND0hyI174.jpg" target="_blank"></a>

執行結果如下:

<a href="http://s3.51cto.com/wyfs02/M02/75/07/wKiom1YwqPCwCvZqAAK1Een6pYU520.jpg" target="_blank"></a>

continue 語句:

    滿足條件繼續進行

<a href="http://s3.51cto.com/wyfs02/M02/75/04/wKioL1YwqxPwBoX8AAJU1fx-7yc318.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/75/07/wKiom1Ywqt_yLaSSAAPbDzQU5xc695.jpg" target="_blank"></a>

本文轉自   tianshuai369   51CTO部落格,原文連結:http://blog.51cto.com/songqinglong/1707416