Python的控制結構__Python

來源:互聯網
上載者:User

條件陳述式:

if  判斷條件:

       執行語句...     (條件為true時執行)

else:

       執行語句...      (條件為false時執行。else可有可無)

註:Python中任何非空和非零值為true。  空(null)或零為false。

多分支條件陳述式:

if   判斷條件1:

       執行語句...

elif  判斷條件2:

       執行語句2...

elif  判斷條件3:

       執行語句3...

else:

       執行語句4...

如果判斷語句中需要多個條件同時判斷時,可以使用or(或者)兩個條件其中一個成立則判斷條件成立。可以使用and(與)兩個條件同時成立則判斷條件成立。

----------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------

迴圈語句:

for迴圈

for  variable  in sequence:

       statements(s)

for迴圈通常用來迭代(遍曆)對象,使用for迴圈時,只要作用於一個可迭代對象,for迴圈就能夠正常運行,判斷一個對象是否是可迭代對象,通過collections模組的Iterable類型判斷:

# -*- coding: cp936 -*-from collections import Iterablebloon1=isinstance('abc', Iterable)  # 字串abc是否可迭代bloon2=isinstance(1234,Iterable)  # 整數1234是否可迭代print bloon1,"\n",bloon2>>>TrueFalse

continue與break:

continue用於跳過此次迴圈:跳過當前迴圈的剩餘語句,繼續進行下一輪迴圈

break用於跳出整個迴圈。

eg1:

for  a  in  'hello' :      print  aprint"------------------------"c=['S','G',3,6]for  b  in  c :      print  bfor  index  in  range(len(c))      print  c[index]

#  len()函數返回列表長度,即元素個數。range()函數返回一個序列的數(會產生數列)。

range(5)  -->  0,1,2,3,4

range(2,10)  -->  2,3,4,5,6,7,8,9

range(2,16,2)  -->  2,4,6,8,10,12,14

for...else:

for語句沒什麼不同,else語句在整個迴圈正常執行完的情況下執行(不是通過break終止)

eg:

for  n  in  range(10,20):      for  i  in  range(2,n):            if  n%i==0:                j=n/i               print  '%d  等於  %d  *  %d'  %  (n,i,j)               break       else:               print  n,  '是一個質數'

----------------------------------------------------------------------------------------------------------------------------------

while迴圈:

while  判斷條件:

           執行語句...

continue與break:

continue用於跳過此次迴圈:跳過當前迴圈的剩餘語句,繼續進行下一輪迴圈

break用於跳出整個迴圈。

eg

i=0while  i<10:    i+=1    if  i%2>0:        continue    print  iprint"---------------"    i=1while True:    print (i)    i +=1    if  i>10:      break

while ...else:

while語句沒什麼不同,else語句在整個迴圈正常執行完的情況下執行(不是通過break終止)

eg:

count =0while  count<5:      print  count,"小於5"      count=count+1else:      print  count,"等於5"

----------------------------------------------------------------------------------------------------------------------------------

pass語句:

pass語句是空語句,為了保持程式結構的完整性。不做任何事情,一般用作佔位語句。

用法:

   pass

eg:

while  True:     pass      # 等待鍵盤中斷(ctrl+c)



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.