大爽Python入門公開課教案 點擊查看教程總目錄
1 布爾值介紹
從判斷說起
回顧第一章介紹的簡單的判斷
>>> x = 10
>>> if x > 5:
... print("x is greater than 5")
重點來看下if x > 5:
這一句。
這一句可以分為兩步
x > 5
: 本質是一個運算式,其值是一個布爾值。if
根據布爾值來判斷。
具體如下
>>> x = 10
>>> x > 5
True
>>> if True:
... print("x is greater than 5")
上面的True
就是布爾值,
if
條件判斷本質上是根據布爾值來判斷的。
布爾值
布爾值(Booleans)只有兩個:
True
: 真,正確False
: 假,錯誤
其數據類型為bool
。
之前第一張簡單判斷的比較運算符,其運算結果就是布爾值。
if
判斷,使用布爾值來判斷是否執行冒號后的語句的。
if True
就執行。
if False
就不會執行。
>>> b = 1 > 5
>>> b
False
>>> type(b)
<class 'bool'>
>>> if b:
... print("1 > 5")
...
>>> c = 1 < 5
>>> c
True
>>> if c:
... print("c<5")
...
1<5
>>> if False:
... print("Only output when true")
...
>>> if True:
... print("Only output when true")
...
Only output when true
布爾轉換
if
條件判斷, 本質上是根據布爾值來判斷的。
即得到if
后內容的布爾值。
當后面內容結果不是布爾對象時,
會將結果使用bool()
方法轉換成布爾對象。
變量使用bool()
方法轉換后的布爾值,
一般簡稱為變量的布爾值。
示例如下
>>> bool(1)
True
>>> bool(-1)
True
>>> bool(0)
False
>>> if 0:
... print("Only output when true")
...
>>> if 1:
... print("Only output when true")
...
Only output when true
>>> if -1:
... print("Only output when true")
...
Only output when true
結論(不必去記,用的時候敲一遍代碼就知道了)
- 只有0的布爾值是
False
,其他數(包括負數)的布爾值都是True
尤其是-1的布爾值,也是True
。
小技巧:
if
語句后面的輸出不確定,想測試的時候,
沒有必要把整個if
語句敲一遍。
直接把if
判斷的內容的布爾值取一下就好。
常用對象的布爾值
結論(不必去記,用的時候敲一遍代碼就知道了)
空容器的布爾值是False
,非空容器的布爾值都是True
適用於:字符串,元組,列表,字典等等。
代碼示例
>>> bool("")
False
>>> bool("a")
True
>>> bool(())
False
>>> bool((1,2))
True
>>> bool([])
False
>>> bool([1])
True
>>> bool({})
False
>>> bool({"a": 1})
True
布爾運算符
二元運算符:
and
: 滿足兩個條件or
: 滿足兩個條件中任意一個即可
一元運算符:
not
: 不滿足這個條件
代碼示例
>>> A = 1 > 0
>>> B = 10 > 5
>>> C = 10 > 20
>>> D = 10 > 100
>>> A, B, C, D
(True, True, False, False)
>>> A and B
True
>>> A and C
False
>>> C and D
False
>>> A or B
True
>>> A or C
True
>>> C or D
False
>>> not A
False
>>> not C
True
返回布爾值
什么是返回值,即這個語句執行之后得到的值,
執行之后得到又稱為返回,具體我們上完第四章節就理解了。
返回布爾值的語法
in
: 判斷一個值是否在容器中。
比如值是否在序列中,以及鍵key
是否在字典中
使用示例
>>> "d" in "abcde"
True
>>> "z" in "abcde"
False
>>> 123 in [1, 2, 3]
False
>>> 23 in [11, 23, 35]
True
>>> dic ={"a":123, "b": 456}
>>> "a" in dic
True
>>> "d" in dic
False
>>> 123 in dic
False
返回布爾值的函數
這里介紹一些常用的函數。
isinstance(object, classinfo)
:
ReturnTrue
if theobject
argument is an instance of theclassinfo
argument, or of a (direct, indirect or virtual) subclass thereof. Ifobject
is not an object of the given type, the function always returnsFalse
.
如果object
變量是classinfo
類的實例(或者通俗點講,object
變量的類型是classinfo
),則返回True
。
否則返回False
。
該函數和
type(object)==classinfo
效果比較相似
(並不完全一樣,但對新手而言,其差別基本碰不到)
一般判斷變量類型,推薦使用函數isinstance
使用示例
>>> isinstance(123, int)
True
>>> isinstance(123, str)
False
>>> isinstance("123", str)
True
>>> isinstance("123", int)
False
>>> type(123) == int
True
>>> type("123") == str
True
>>> type([1, 2, 3]) == list
True
>>> isinstance([1, 2, 3], list)
True
補充:未來會遇到種種運算,
運算之前可能會要判斷數據的類型,
是否是可以運算的種類。
返回布爾值的方法
有很多判斷方法,是可以放回布爾值的。
比如字符串就有一堆方法。
這里列舉幾個相對還比較常用的,大家了解一下,有個概念即可,
不必記住,用的時候再來查就好。
str.startswith(prefix)
:
ReturnTrue
if string starts with theprefix
, otherwise returnFalse
.
字符串以prefix
變量值開頭,則返回True
,否則返回False
。str.endswith(suffix)
:
ReturnTrue
if the string ends with the specifiedsuffix
, otherwise returnFalse
.
字符串以suffix
變量值結尾,則返回True
,否則返回False
。str.isdigit()
:
ReturnTrue
if all characters in the string are digits and there is at least one character,False
otherwise.
字符串中的所有字符都是數字,且至少有一個字符,則返回True
,否則返回False
。str.islower()
:
ReturnTrue
if all cased characters in the string are lowercase and there is at least one cased character,False
otherwise.
字符串中的所有字符都是小寫,且至少有一個字符,則返回True
,否則返回False
。str.isupper()
:
ReturnTrue
if all cased characters in the string are uppercase and there is at least one cased character,False
otherwise.
字符串中的所有字符都是大寫,且至少有一個字符,則返回True
,否則返回False
。
使用示例
>>> "abcde".startswith("a")
True
>>> "abcde".startswith("abc")
True
>>> "abcde".startswith("bc")
False
>>> "abcde".endswith("e")
True
>>> "ab123".isdigit()
False
>>> "123".isdigit()
True
>>> "abc".islower()
True
>>> "Add".islower()
False
>>> "Add".isupper()
False
>>> "ADD".isupper()
True