Python的基本運算運算式
(1)判斷語句
關於if語句的規則我不再介紹,只在這裡提出Python下if語句的用法,以及特點。
#!/usr/bin/python<br /># Filename: if.py </p><p>number = 23<br />guess = int(raw_input('Enter an integer : '))</p><p>if guess == number:<br /> print 'Congratulations, you guessed it.' # New block starts here<br /> print "(but you do not win any prizes!)" # New block ends here<br />elif guess < number:<br /> print 'No, it is a little higher than that' # Another block<br /> # You can do whatever you want in a block ...<br />else:<br /> print 'No, it is a little lower than that'<br /> # you must have guess > number to reach here</p><p>print 'Done'<br /># This last statement is always executed, after the if statement is executed </p><p>
以下為輸出
$ python if.py<br />Enter an integer : 50<br />No, it is a little lower than that<br />Done<br />$ python if.py<br />Enter an integer : 22<br />No, it is a little higher than that<br />Done<br />$ python if.py<br />Enter an integer : 23<br />Congratulations, you guessed it.<br />(but you do not win any prizes!)<br />Done </p><p>
在這裡,我們使用了一個函數raw_input(),用以擷取使用者的輸入。
我們為內建的raw_input
函數提供一個字串,這個字串被列印在螢幕上,然後等待使用者的輸入。一旦我們輸入一些東西,然後按斷行符號鍵之後,函數返回輸入。對於raw_input
函數來說是一個字串。我們通過int
把這個字串轉換為整數,並把它儲存在變數guess
中。事實上,int
是一個類,不過你對它所需瞭解的只是它把一個字串轉換為一個整數。
注意:我們使用了縮排層次來告訴Python每個語句分別屬於哪一個塊。這就是為什麼縮排在Python如此重要的原因。
在Python中沒有switch
語句。不過你可以使用if..elif..else
語句來完成同樣的工作。
(2)迴圈語句
1.while語句
Python的while語句,與其他語言的while語句區別不大。
#!/usr/bin/python<br /># Filename: while.py</p><p>number = 23<br />running = True</p><p>while running:<br /> guess = int(raw_input('Enter an integer : '))</p><p> if guess == number:<br /> print 'Congratulations, you guessed it.'<br /> running = False # this causes the while loop to stop<br /> elif guess < number:<br /> print 'No, it is a little higher than that'<br /> else:<br /> print 'No, it is a little lower than that'<br />else:<br /> print 'The while loop is over.'<br /> # Do anything else you want to do here</p><p>print 'Done'
輸出為:
$ python while.py<br />Enter an integer : 50<br />No, it is a little lower than that.<br />Enter an integer : 22<br />No, it is a little higher than that.<br />Enter an integer : 23<br />Congratulations, you guessed it.<br />The while loop is over.<br />Done </p><p>
注意:在Python中,你可以在while
迴圈中使用一個else
從句。
2.for語句
for語句,在這裡,我通過一個例子在作出說明
#!/usr/bin/python<br /># Filename: for.py</p><p>for i in range(1, 5):<br /> print i<br />else:<br /> print 'The for loop is over'
輸出為:
$ python for.py<br />1<br />2<br />3<br />4<br />The for loop is over </p><p>
注意:else
部分是可選的。如果包含else,它總是在for
迴圈結束後執行一次,除非遇到break語句。
與C/C++語言相比,Python的if語句,無疑簡單了許多。
(3)其他語句
1.break語句
break語句在for迴圈和while迴圈中使用。
while True:<br /> s = raw_input('Enter something : ')<br /> if s == 'quit':<br /> break<br /> print 'Length of the string is', len(s)<br />print 'Done'
輸出為
$ python break.py<br />Enter something : Programming is fun<br />Length of the string is 18<br />Enter something : When the work is done<br />Length of the string is 21<br />Enter something : if you wanna make your work also fun:<br />Length of the string is 37<br />Enter something : use Python!<br />Length of the string is 12<br />Enter something : quit<br />Done </p><p>
2.continue語句
在這裡,我只通過一個例子來說明。
#!/usr/bin/python<br /># Filename: continue.py</p><p>while True:<br /> s = raw_input('Enter something : ')<br /> if s == 'quit':<br /> break<br /> if len(s) < 3:<br /> continue<br /> print 'Input is of sufficient length'<br /> # Do other kinds of processing here... </p><p>
輸出為:
$ python continue.py<br />Enter something : a<br />Enter something : 12<br />Enter something : abc<br />Input is of sufficient length<br />Enter something : quit