<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                看官是否記得,在上一部分的時候,有一講專門介紹if語句的:[從if開始語句的征程](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/111.md)。在學習if語句的時候,對python編程的基礎知識了解的還不是很多,或許沒有做什么太復雜的東西。本講,要對它進行一番復習,通過復習提高一下。如果此前有的東西忘記了,建議首先回頭,看看前面那講。 ## 基本語句結構 ~~~ if 判斷條件1: 執行語句1…… elif 判斷條件2: 執行語句2…… elif 判斷條件3: 執行語句3…… else: 執行語句4…… ~~~ 只有當“判斷條件”的值是True的時候,才執行下面的執行語句。 那么,在python中,怎么知道一個判斷條件是不是真呢?這個問題我們在[眼花繚亂的運算符](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/110.md)中已經講解了一種數據類型:布爾類型。可以通過一個內置函數bool()來判斷一個條件的結果True還是False。看看下面的例子,是不是能夠理解bool()的判斷規則? ~~~ >>> bool("") False >>> bool(0) False >>> bool('none') True >>> bool(False) False >>> bool("False") True >>> bool(True) True >>> bool("True") True >>> bool(3>4) False >>> bool("b">"a") True >>> bool(not "") True >>> bool(not True) False ~~~ 忘記了怎么辦?看下面的語句: ~~~ if 忘記: 復習-->眼花繚亂的運算符一講 ~~~ 在執行語句中,其實不一定非要把bool()寫上的。如同這樣: ~~~ >>> x = 9 >>> if bool(x>7): #條件為True則執行下面的 ... print "%d more than 7"%x ... else: ... print "%d not more than 7"%x ... 9 more than 7 >>> if x>7: ... print "%d more than 7"%x ... else: ... print "%d not more than 7"%x ... 9 more than 7 ~~~ 以上兩個寫法是等效的,但是,在實際的編程中,我們不用if bool(x>7)的格式,而是使用if x>7的樣式,還要特別提醒,如果寫成if (x>7),用一個括號把條件表達式括起來,是不是可以呢?可以,但也不是python提倡的。 ~~~ >>> if (x>7): #不提倡這么寫,這不是python風格 ... print "%d more than 7"%x ... 9 more than 7 ~~~ ## 拉出來溜溜 平時總有人在不服氣的時候說“是騾子是馬,拉出來溜溜”,趙本山有一句名言“走兩步”。其本質都是說“光說不練是假把式”。今天收到一個朋友的郵件,也詢問,在學習python的時候,記不住python的內容。其實不用記,我在前面的課程中已經反復講過了。但是,在應用中,會越來越熟練。 下面就做一個練習,要求是: 1. 接收任何字符和數字的輸入 2. 判斷輸入的內容,如果不是整數是字符,就告訴給用戶;如果是小數,也告訴用戶 3. 如果輸入的是整數,判斷這個整數是奇數還是偶數,并且告訴給用戶 在這個練習中,顯然要對輸入的內容進行判斷,以下幾點需要看官注意: * 通過raw_input()得到的輸入內容,都是str類型 * 要判斷一個字符串是否是由純粹數字組成,可以使用str.isdigit()(建議看官查看該內置函數官方文檔) 下面的代碼是一個參考: ~~~ #! /usr/bin/env python #coding:utf-8 print "請輸入字符串,然后按下回車鍵:" user_input = raw_input() result = user_input.isdigit() if not result: print "您輸入的不完全是數字" elif int(user_input)%2==0: print "您輸入的是一個偶數" elif int(user_input)%2!=0: print "您輸入的是一個奇數" else: print "您沒有輸入什么呢吧" ~~~ 特別提醒列為,這個代碼不是非常完善的,還有能夠修改的地方,看官能否完善之? 再來一個如何? 已知一個由整數構成的list,從中跳出奇數和偶數,并且各放在一個list中。 請看官在看下面的參考代碼之前,自己寫一寫。 ~~~ #!/usr/bin/env python #coding:utf-8 import random numbers = [random.randint(1,100) for i in range(20)] #以list解析的方式得到隨機的list odd = [] even = [] for x in numbers: if x%2==0: even.append(x) else: odd.append(x) print numbers print "odd:",odd print "even:",even ~~~ 用這個例子演示一下if在list解析中的應用。看能不能繼續改進一些呢? 可以將循環的那部分用下面的list解析代替 ~~~ #!/usr/bin/env python #coding:utf-8 import random numbers = [random.randint(1,100) for i in range(20)] #以list解析的方式得到隨機的list odd = [x for x in numbers if x%2!=0] even = [x for x in numbers if x%2==0] print numbers print "odd:",odd print "even:",even ~~~ ## 一個有趣的賦值--三元操作符 對賦值,看官應該比較熟悉了吧,如果要復習,請看[《賦值,簡單也不簡單》](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/127.md)以及[《正規地說一句》](https://github.com/qiwsir/ITArticles/blob/master/BasicPython/201.md)的相關內容。 這里說的有趣賦值是什么樣子的呢?請看: ~~~ >>> name = "qiwsir" if "laoqi" else "github" >>> name 'qiwsir' >>> name = 'qiwsir' if "" else "python" >>> name 'python' >>> name = "qiwsir" if "github" else "" >>> name 'qiwsir' ~~~ 總結一下:A = Y if X else Z 什么意思,結合前面的例子,可以看出: * 如果X為真,那么就執行A=Y * 如果X為假,就執行A=Z > > > x = 2 y = 8 a = "python" if x>y else "qiwsir" a 'qiwsir' b = "python" if x<y else "qiwsir" b 'python' 再看看上面的例子,是不是這樣執行呢? if語句似乎簡單,但是在編程時間中常用到。勤加練習吧。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看