在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ exercise28. 布爾表達式
附錄 A-練習 9:生成一個空文件(Touch, New-Item)
附錄 A-練習 10:復制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習 14:刪除文件 (rm)
附錄 A-練習 11:移動文件 (mv)
exercise46.項目骨架
附錄 A-練習 3:如果你迷路了
exercise37.復習符號
exercise47.自動化測試
exercise3.數字和數學計算
附錄 A-練習 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對象、類、以及從屬關系
exercise48.更復雜的用戶輸入
下一步
簡介
附錄 A-練習 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對象
附錄 A-練習 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數, 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達式
附錄 A-練習 4:創(chuàng)建一個路徑 (mkdir)
附錄 A-練習 15:退出命令行 (exit)
exercise25. 更多更多的練習
exercise6.字符串和文本
exercise2.注釋和井號“#”
exercise21. 函數的返回值
附錄 A-下一步
exercise1.第一個程序
exercise23. 閱讀代碼
附錄 A-練習 5:改變當前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習
exercise19.函數和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學到了什么?
exercise41.學會說面向對象
exercise52.開始你的 web 游戲
exercise20. 函數和文件
exercise15.讀文件
exercise45.你來制作一個游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數
exercise26. 恭喜你,可以進行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設計和調試
exercise0.安裝和準備
exercise50.你的第一個網站
附錄 A-練習 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習 6:列出當前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向對象的分析和設計
附錄 A-簡介
附錄 A-練習 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise28. 布爾表達式

上一節(jié)學到的邏輯組合的正式名稱是“布爾邏輯表達式(boolean logic expression)”。在編程中,布爾邏輯可以說是無處不在。它們是計算機運算的基礎和重要組成部分,掌握它們就跟學音樂掌握音階一樣重要。

在這節(jié)練習中,你將在 python 里使用到上節(jié)學到的邏輯表達式。先為下面的每一個邏輯問題寫出你認為的答案,每一題的答案要么為 True 要么為 False。寫完以后,你需要將 python 運行起來,把這些邏輯語句輸入進去,確認你寫的答案是否正確。

True and True
False and True
1 == 1 and 2 == 1
"test" == "test"
1 == 1 or 2 != 1
True and 1 == 1
False and 0 != 0
True or 1 == 1
"test" == "testing"
1 != 0 and 2 == 1
"test" != "testing"
"test" == 1
not (True and False)
not (1 == 1 and 0 != 1)
not (10 == 1 or 1000 == 1000)
not (1 != 10 or 3 == 4)
not ("testing" == "testing" and "Zed" == "Cool Guy")
1 == 1 and (not ("testing" == 1 or 1 == 0))
"chunky" == "bacon" and (not (3 == 4 or 3 == 3))
3 == 3 and (not ("testing" == "testing" or "Python" == "Fun"))

在本節(jié)結尾的地方我會給你一個理清復雜邏輯的技巧。

所有的布爾邏輯表達式都可以用下面的簡單流程得到結果:

1.找到相等判斷的部分 (== 或者 !=),將其改寫為其最終值 (True 或 False)。 2.找到括號里的 and/or,先算出它們的值。 3.找到每一個 not,算出他們反過來的值。 4.找到剩下的 and/or,解出它們的值。 5.等你都做完后,剩下的結果應該就是 True 或者 False 了。

下面我們以 20 行的邏輯表達式演示一下:

3 != 4 and not ("testing" != "test" or "Python" == "Python")

接下來你將看到這個復雜表達式是如何逐級解為一個單獨結果的:

1.解出每一個等值判斷:

a. 3 != 4 為 True: True and not ("testing" != "test" or "Python" == "Python") b. "testing" != "test" 為 True: True and not (True or "Python" == "Python") c. "Python" == "Python"為 True: True and not (True or True)

2.找到括號中的每一個 and/or:

a. (True or True) 為 True: True and not (True)

3.找到每一個 not 并將其逆轉:

a. not (True) 為 False: True and False

4.找到剩下的 and/or,解出它們的值:

a. True and False 為 False

這樣我們就解出了它最終的值為 False.

Warning: 復雜的邏輯表達式一開始看上去可能會讓你覺得很難。而且你也許已經碰壁過了,不過別灰心,這些“邏輯體操”式的訓練只是讓你逐漸習慣起來,這樣后面你可以輕易應對編程里邊更酷的一些東西。只要你堅持下去,不放過自己做錯的地方就行了。如果你暫時不太能理解也沒關系,弄懂的時候總會到來的。

你看到的結果

以下內容是在你自己猜測結果以后,通過和 python 對話得到的結果:

$ python
Python 2.5.1 (r251:54863, Feb  6 2009, 19:02:12)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> True and True
True
>>> 1 == 1 and 2 == 2
True

附加題

1.Python 里還有很多和!=、 == 類似的操作符. 試著盡可能多地列出 Python 中的等價運算符。例如 < 或者 <= 。 2.寫出每一個等價運算符的名稱。例如 != 叫 “not equal(不等于)”。 3.在 python 中測試新的布爾操作。在敲回車前你需要喊出它的結果。不要思考,憑自己的第一感就可以了。把表達式和結果用筆寫下來再敲回車,最后看自己做對多少,做錯多少。 4.把習題 3 那張紙丟掉,以后你不再需要查詢它了。

常見問題

Q: 為什么"test" and "test" 返回 "test"以及 1 and 1 返回 1 而不是 True?

python 和很多語言可以返回布爾表達式中的一個操作數,而不僅僅是真或假。這意味著如果你計算 False and 1 你會得到表達式的第一個操作數 (False) ,但是如果你計算 True and 1 的時候,你得到它的第二個操作數(1)。試一試吧。

Q:!=和 <>有什么不同嗎?

Python 已經聲明贊成使用!=而棄用<>所以盡量使用!=吧。其他的應該沒有區(qū)別了。

Q:有沒有捷徑去判斷布爾表達式的值?

有的。任何的 and 表達式包含一個 False 結果就是 False,任何 or 表達式有一個 True 結果就是 True,你就可以在此處得到結果,但要確保你能處理整個表達式,因為后面這是一個很有用的技能。