python基礎之迴圈語句

來源:互聯網
上載者:User

標籤:成功   keyword   結束   loop   執行   對象   好用   12px   之間   

一、if條件陳述式:

文法:

1.if單分支(單重條件判斷)

 

       if expression:

 

           expr_true_suite

 

注釋:expession為真執行代碼expr_true_suite

if單分支(單重條件判斷)    

     if not  active or over_time >= 10:

        print(‘Warning:service is dead‘)

         warn_tag+=1

2.if+else迴圈語句

    if 1<2:

      print(‘ok‘)

   else:

   print(‘no‘)

3.多分支if+elif+else

oldboyage= 53 

guessage=input(‘請輸入老男孩的年齡:‘)

if guess<53:

 print("猜小了")

elif guess>53:

print("猜大了")

else:

print("猜對了"):

if語句小結:

  1. if 後運算式傳回值為True則執行其子代碼塊,然後此if語句到此終結,否則進入下一分支判斷,直到滿足其中一個分支,執行後終結if
  2. expression可以引入運算子:not,and,or,is,is not
  3. 多重expression為加強可讀性最好用括弧包含
  4. if與else縮排層級一致表示是一對
  5. elif與else都是可選的
  6. 一個if判斷最多隻有一個else但是可以有多個elif
  7. else代表if判斷的終結
  8. expession可以是傳回值為布爾值的運算式(例x>1,x is not None)的形式,也可是單個標準對象(例 x=1;if x:print(‘ok‘))
  9. 所有標準對象均可用於布爾測試,同類型的對象之間可以比較大小。每個對象天生具有布 爾 True 或 False 值。Null 物件、值為零的任何數字或者 Null 對象 None 的布爾值都是 False。

 二、while迴圈語句

      基本的文法:

        while expression:

        suite_to_repeat

      註解:重複執行suite_to_repeat,直到expression不再為真

       具體結構如下:

        while 條件:

          迴圈體的代碼1

          迴圈體的代碼2

          迴圈體的代碼3 

          ..........

     1. 計數迴圈

    count=0

    while (count < 9):

     print(‘the loop is %s‘ %count)

  count+=1

  2.無限迴圈

   count=0

   while True:

   print(‘the loop is %s‘ %count)

   count+=1

 ps: 

      tag=True
      count=0
      while tag:
      if count == 9:
     tag=False
     print(‘the loop is %s‘ %count)
      count+=1

 3.while迴圈與break、countinue

  break跳出本層迴圈

 ps:

      #break:跳出本層迴圈
      count=0
     while count < 10:
     if count == 5:
     break
     print(count)
     count+=1

 

  countinue跳出本次迴圈   

ps: 

    #continue:跳出本次迴圈
    #0 1 2 3 7 8 9

     count=0
    while count < 10:
    if count >=4 and count <=6:
    count += 1
    continue
    print(count)
    count+lse=1

  • break用於完全結束一個迴圈,跳出迴圈體執行迴圈後面的語句
  • continue和break有點類似,區別在於continue只是終止本次迴圈,接著還執行後面的迴圈,break則完全終止迴圈 
while ... else ..

      與其它語言else 一般只與if 搭配不同,在Python 中還有個while ...else 語句

      while 後面的else 作用是指,當while 迴圈正常執行完,中間沒有被break 中止的話,就會執行else後面的語句

 

   ps:   

count = 0while count <= 5 :    count += 1    print("Loop",count)else:    print("迴圈正常執行完啦")print("-----out of while loop ------")

小節練習:

1.使用while迴圈輸出1 2 3 4 5 6 8 9 10
count=1
while True:
if(count!=7):
print(count)
count+=1
if(count>10):
break

2 .求1-100的所有數的和
sum=0
for i in range(1,101):
sum=sum+i
print("從1加到100等於",str(sum))

 

3. 輸出 1-100 內的所有奇數
for i in range(1,101):
if i%2!=0:
print(i)


4. 輸出 1-100 內的所有偶數
for i in range(1,101):
if i%2==0:
print(i)

5. 求1-2+3-4+5 ... 99的所有數的和
sum=0
for i in range(1,100):
if(i%2==0):
sum=sum-i
else:
sum=sum+i
print("和是"+str(sum))


6. 使用者登陸(三次機會重試)
count = 3
while count>0:
username = input("please your username ")
password = input("please your password ")
if(username=="admin" and password=="admin"):
print("登入成功")
break
else:
count=count-1
print("你輸入的使用者名稱或密碼有誤請重新登入還有%s次機會."%(count))

 

 

   

  

 

  

   

  

    

 

 

 

 

 

 

python基礎之迴圈語句

聯繫我們

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