結果為: True True False 以上Python範例,還可以搭配「not」運算子【not 運算子請參考:Python控制結構3.布林邏輯:and,or,not】來檢查指定的物件(元素)是否「沒有」在串列中。若沒有,則回傳為 True;有的話則為 False。 Fruits = ["芭樂", "榴蓮", "柳丁", "草莓"] print(not "榴蓮" in Fruits) print("榴蓮" not in Fruits) print(not "草莓" in Fruits) print("牛肉" not in Fruits)
專門寫給Python初學者的練習一來啦!!千萬不要錯過嘍~ 本節 Python 練習中我們將使用複雜的字串 String 來建立一系列的變數,以更熟悉字串的用處。 首先,我們還是要了解一下字串這個概念。字串通常包含你想要展示出來的內容、或是你想要從程式裡導出 "" 的一小段字符。Python 可以通過文本裡的雙引號或者單引號,識別出何為字串。如果你把單引號或者雙引號括起來的文字放到 print 後面,它們就會被Python print 出來。 字串可以包含格式化字符 %s(即以 str() 函數輸出文字)。你只要將格式化的變數放到字串中,再緊跟著一個百分號 % (percent),再緊跟著變數名即可。唯一要注意的地方是如果你想要在字串中通過格式化字符放入多個變數的時候,你需要將變數放到( ) 圓括號(parenthesis) 中,而且變數之間用, 逗號(comma ) 隔開。就像你逛商店說『我要買牛奶、麵包、雞蛋、八寶粥』一樣,只不過程式設計師說的是「(milk, eggs, bread, soup)」。 在以下的練習中,我們將用簡化的變數名稱,輸入大量的字串、變數、和格式化字符,並且執行出來: x = "There are %d types of people." % 10 binary = "binary" do_not = "don't" y = "Those who know %s and those who %s." % (binary, do_not) print x print y print "I said: %r." % x print "I also said: '%s'." % y hilarious = False joke_evaluation = "Isn't that joke so funny?! %r" print joke_evaluation % hilarious w = "This is the left side of..." e = "a string with a right side." print w + e 如果上面的操作都沒問題,你將看到下面的結果: There are 10 types of people. Those who know binary and those who don't. I said: 'There are 10 types of people.'. I also said: 'Those who know binary and those who don't.'. Isn't that joke so funny?! False This is the left side of...a string with a right side.
最受歡迎的Python教學又回來啦!! 今天要來教大家dir()和help()的使用~ 快往下看吧!! Python 中 dir()、help()的使用 在使用Python來編寫程式碼時,經常會使用 Python 中的內建函數 (Built-in Functions)或是模組。若當你對一些函數、模組不清楚的話,就可以通過 dir() 和 help() 查詢說明或屬性。 help(): 用於查看屬性及方法的詳細解釋 Python 內建函數 - dir() 「dir()」的用途是用於用來查詢物件的全部屬性。例如說今天你想使用 dir() 函數用於查看物件「str」 的全部屬性,你就寫了: >>>print dir(str) 執行結果如下: ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] 如果 dir() 的括弧內不帶任何參數物件,執行結果則會最大限度地顯示出當前範圍內的變數、方法和屬性列表。 ['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__'] Python 內建函數 - help() help(): 用於查看函式或模組用途的詳細說明。例如說今天你想使用 help() 用於查看「dir」 的用途,你就寫了: >>>help(‘dir’) 執行結果如下: Help on built-in function dir in module builtins: dir(…) dir([object]) -> list of strings If called without an argument, return the names in the current scope. Else, return an alphabetized list of names comprising (some of) the attributes of the given object, and of attributes reachable from it. If the object supplies a method named __dir__, it will be used; otherwise the default dir() logic is used and returns: for a module object: the module’s attributes. for a class object: its attributes, and recursively the attributes of its bases. for any other object: its attributes, its class’s attributes, and recursively the attributes of its class’s base classes.
最多人想學的Python有新文章啦!! 這篇要和大家分享while 迴圈 Python-while 迴圈 Python教學中的 if 條件判斷式【請參考:Python控制結構2.if else條件判斷(1)】,若評估為 True【請參考:Python控制結構1.布林值Boolean-True or False?】 的話,就會執行一次;反之則不執行。 Python 的 while 迴圈跟 if 條件判斷式相似,但是不同的是,它不只執行一次。只要條件成立,它就會不斷地重複執行。就算是有其中一段代碼被評估為 False,下一段代碼仍會被執行。 while 迴圈構成,包含了 。 以下範例的 while 迴圈中有一個變數「i」,初始值為「1」;更新值(又稱為調整值)為「i+1」;讓迴圈結束的條件為「i <=5」。 以下範例讓 i 的從初始值開始,程式每執行一次,i 就等於上一回合的數值再「+1」一次,直到 i 等於 5 為止停止,寫法如下: i = 1 #初始值 while i <=5: #讓迴圈結束的條件 print(i) i = i + 1 #更新值 (調整)
則結果為: 1 2 3 4 5 >>> while迴圈的「infinite loop(無限循環)」 Python 的 while 迴圈中,有一種被稱作「infinite loop(無限循環)」,當條件(為True)成立時,程式就會永不停止地重複執行。 「infinite loop(無限循環)」的範例如下: while 7==7: print("這是無限循環迴圈") 結果,「這是無限循環迴圈」這行字就會不斷的重複顯示。 若你真的受不了,你可以按鍵盤快速鍵「Ctrl-C」以阻止它繼續重複顯示。 以下也是無限循環迴圈的另一個例子,看看它的結果如何。 x=1 >>> while x>0: print(x) x+2 結果馬上會跳出一堆數字,且不斷的在跳出中。後出來的數字,都是前一個數字 +2 的結果。真的受不了,就按鍵盤快速鍵「Ctrl-C 」跳出吧! 跳出while迴圈:break 要結束 while 迴圈循環,就使用 break 敘述。在迴圈循環時,一遇到 break 敘述,就會讓迴圈立即停止循環。範例如下: i = 1 #初始值 while 1==1: print(i) i = i + 1 #更新值 (調整) if i >= 5: #讓迴圈結束的條件 print("Breaking") break >>> 上述的代碼,意思若「1 等於 1」的敘述為真,則 i 就從 1 開始輸出,之後程式跑到「i = i + 1」,i 就以 i+1 的形式繼續循環輸出,直到 i 為「5」時,就輸出「Breaking」並結束循環。因此這段 Python 程式的執行結果為: 1 2 3 4 Breaking 繼續while迴圈:continue Python 的 while 迴圈中,有一個與 break 敘述相對的,就是 continue —— 跳回到迴圈的頂部繼續循環,而不是停止循環。 i = 1 #初始值 while True: print(i) i = i + 1 #更新值 (調整) if i == 5: print("Skipping 5") continue if i == 7: #讓迴圈結束的條件 print("Breaking") break 上述的 Python 代碼,意思若「1 等於 1」的敘述為真,則 i 就從 1 開始輸出,之後程式跑到「i = i + 1」,i 就以 i+1 的形式繼續循環輸出,直到 i 等於「5」時,就輸出「Skipping 5」並且跳回到迴圈的頂部「print(i)」繼續從「5」循環,直到 i 等於「7」時輸出「Breaking」並結束循環。因此這段 Python 程式的執行結果為: 1 2 3 4 Skipping 5 5 6 Breaking