Python 3 條件、迴圈和assert、pass、del

來源:互聯網
上載者:User

標籤:most   語句   eth   plain   boa   assertion   erro   條件   tab   

條件:

if 條件:
    語句塊
elif:
    語句塊
else:
    語句塊


elif 表示 else if


這居然是合法的!!!1 < x < 2!!!

[python] view plain copy print?
  1. >>> if 1 < x < 2:  
  2.     print(‘True‘)  
  3.   
  4.       
  5. True  


and 表示且

[python] view plain copy print?
  1. >>> if x > 1 and x < 2:  
  2.     print(‘True‘)  
  3.   
  4.       
  5. True  


or 表示 或

[python] view plain copy print?
  1. >>> x  
  2. 2  
  3. >>> if x == 2 or x == 3:  
  4.     print(x)  
  5.   
  6.       
  7. 2  

 

如果 b 為真則返回a,否則返回 c

a  if  b  else  c

[python] view plain copy print?
  1. >>> ‘True‘ if 1 < x <2 else ‘False‘  
  2. ‘True‘  

 

while 迴圈

while 條件:

   語句塊


不需要括弧哦!

[python] view plain copy print?
  1. >>> x  
  2. 1.2  
  3. >>> while x < 2:  
  4.     print(x)  
  5.     x += 0.2  
  6.   
  7.       
  8. 1.2  
  9. 1.4  
  10. 1.5999999999999999  
  11. 1.7999999999999998  
  12. 1.9999999999999998  
  13. >>>  


經常用 :

[python] view plain copy print?
  1. while True:  
  2.     ....  
  3.     if ... :  
  4.         break  
  5.     ....  


for 迴圈

for something in XXXX:

    語句塊


即表示對XXXX中的每一個元素,執行某些語句塊,XXXX可以是列表,字典,元組,迭代器等等。

[python] view plain copy print?
  1. >>> for x in range(0,10):  
  2.     print(x*x)  
  3.   
  4.       
  5. 0  
  6. 1  
  7. 4  
  8. 9  
  9. 16  
  10. 25  
  11. 36  
  12. 49  
  13. 64  
  14. 81  

這是 for..else...語句
僅在沒有 break 的情況下執行,或者說,只要你沒有 break,它就會執行


[python] view plain copy print?
  1. >>> for n in range(99,81,-1):  
  2.     root = sqrt(n)  
  3.     if root == int(root):  
  4.         print (n)  
  5.         break  
  6. else:  
  7.     print ("I didn‘t fint it")  
  8.   
  9.       
  10. I didn‘t fint it  


但你應該儘可能使用列表推導式,因為它更方便,清晰

[python] view plain copy print?
  1. >>> [x*x for x in range(1,5)]  
  2. [1, 4, 9, 16]  
  3. >>> [x**2 for x in range(1,10) if x % 2 ==0]  
  4. [4, 16, 36, 64]  
  5. >>> [(x,y) for x in range(1,3) for y in range(4,6)]  
  6. [(1, 4), (1, 5), (2, 4), (2, 5)]  


斷言 assert
後面語句為真,否則出現 AssertionError

用來檢查一個條件,如果它為真,就不做任何事。如果它為假,則會拋出AssertError並且包含錯誤資訊。

例如:

py> x = 23py> assert x > 0"x is not zero or negative"py> assert x%2 == 0"x is not an even number"Traceback (most recent call last):File "", line 1inAssertionError: x is not an even number
#常用在代碼開頭的注釋 assert  target  in  (x, y, z) if  target  = =  x:      run_x_code() elif  target  = =  y:      run_y_code() else :      assert  target  = =  z      run_z_code()
 

 

pass

pass 表示這裡什麼都沒有,不執行任何操作

如果你的程式還有未完成的函數和類等,你可以先添加一些注釋,然後代碼部分僅僅寫一個 pass,這樣程式可以運行不會報錯,而後期你可以繼續完善你的程式

[python] view plain copy print?
  1. >>> class Nothing:  
  2.     pass  
  3.   
  4. >>>   


del
del 刪除的只是引用和名稱,並不刪除值,也就是說,Python 會自動管理記憶體,負責記憶體的回收,這也是 Python 運行效率較低的一個原因吧

[python] view plain copy print?
    1. >>> x = [1,2,3]  
    2. >>> y = x    #x 和 y指向同一個列表  
    3. >>> del x  
    4. >>> x  
    5. Traceback (most recent call last):  
    6.   File "<pyshell#41>", line 1, in <module>  
    7.     x  
    8. NameError: name ‘x‘ is not defined  
    9. >>> y  
    10. [1, 2, 3] 

Python 3 條件、迴圈和assert、pass、del

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.