<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國際加速解決方案。 廣告
                [TOC] ## **if...else 語句** ### 單分支 ``` if 條件: 滿足條件后要執行的代碼 ``` ### 雙分支 ``` if 條件: 滿足條件執行代碼 else: if條件不滿足就走這段 ``` AgeOfOldboy = 48 if AgeOfOldboy > 50 : print("Too old, time to retire..") else: print("還能折騰幾年!") ### 多分支 ``` if 條件: 滿足條件執行代碼 elif 條件: 上面的條件不滿足就走這個 elif 條件: 上面的條件不滿足就走這個 elif 條件: 上面的條件不滿足就走這個 else: 上面所有的條件不滿足就走這段 ``` * 猜年齡的游戲案例 ``` age_of_oldboy = 48 guess = int(input(">>:")) if guess > age_of_oldboy : print("猜的太大了,往小里試試...") elif guess < age_of_oldboy : print("猜的太小了,往大里試試...") else: print("恭喜你,猜對了...") ``` 上面的例子,根據你輸入的值不同,會最多得到3種不同的結果 ## **while循環** ### 普通while語法 ```python while 條件: 執行代碼 ``` > while 指當其后面的條件成立,就執行while下面的代碼 * 案例: 寫個讓程序從0打印到100的程序 ,每循環一次,+1 ```python count = 0 while count <= 100 : #只要count<=100就不斷執行下面的代碼 print("loop ", count ) count +=1 #每執行一次,就把count+1,要不然就變成死循環啦,因為count一直是0 ``` > * 輸出 loop 0 loop 1 .... loop 99 loop 100 ### 死循環 有一種循環叫死循環,一經觸發,就運行個天荒地老、海枯石爛. while 是只要后邊條件成立(也就是條件結果為真)就一直執行,怎么讓條件一直成立呢? ```python count = 0 while True: #True本身就是真呀 print("你是風兒我是沙,纏纏綿綿到天涯...",count) count +=1 ``` ### 循環中止語句 如果在循環的過程中不想繼續循環了,怎么把它中止掉呢?這就用到break 或 continue 語句 * break 完全結束一個循環,跳出循環體執行循環后面的語句 * continue 終止本次循環,進入下一次循環 **continue和break類似,區別在于continue只是終止本次循環,接著還執行后面的循環,break則完全終止循環** * 例子:break ```python count = 0 while count <= 100 : #只要count<=100就不斷執行下面的代碼 print("loop ", count) if count == 5: break count +=1 #每執行一次,就把count+1,要不然就變成死循環啦,因為count一直是0 print("-----out of while loop ------") ``` >loop 0 loop 1 loop 2 loop 3 loop 4 loop 5 -----out of while loop ------ * 例子:continue ```python count = 0 while count <= 100 : count += 1 if count > 5 and count < 95: #只要count在6-94之間,就不走下面的print語句,直接進入下一次loop continue print("loop ", count) print("-----out of while loop ------") ``` >loop 1 loop 2 loop 3 loop 4 loop 5 loop 95 loop 96 loop 97 loop 98 loop 99 loop 100 loop 101 -----out of while loop ------ ### while ... else .. 與其它語言else 一般只與if 搭配不同,在Python 中還有個`while ...else` 語句 while 后面的else 作用是指,當while 循環正常執行完,中間沒有被break 中止的話,就會執行else后面的 * 案例 ``` count = 0 while count <= 5 : count += 1 print("Loop",count) else: print("循環正常執行完啦") print("-----out of while loop ------") ``` >Loop 1 Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 循環正常執行完啦 -----out of while loop ------ **#如果執行過程中被break,就不會執行else語句** ## **for循環** while循環長用來的條件判斷循環,for循環用來做范圍循環,不能做死循環,可以跟esle子句. 可以用來循環打印一個列表,元組中的所有值 ### 語法 ``` for i in 范圍: 執行代碼 ``` * 例子 ``` for i in 1,2,3,4: print(i) ``` >1 2 3 4 ### rang函數生成列表 ``` for i in range(3): print(i) ``` >0 1 2 ### 循環列表數據和枚舉 * 循環打印列表中的每個元素 ``` L2=['A','B','C'] for i in L2: print(i) ``` >A B C * 循環打印列表每個元素及索引號 需要用到enumerate函數枚舉 ``` L2=['A','B','C'] for i,n in enumerate(L2): print(i,n) ``` >0 A 1 B 2 C
                  <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>

                              哎呀哎呀视频在线观看