大爽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):
ReturnTrueif theobjectargument is an instance of theclassinfoargument, or of a (direct, indirect or virtual) subclass thereof. Ifobjectis 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):
ReturnTrueif string starts with theprefix, otherwise returnFalse.
字符串以prefix变量值开头,则返回True,否则返回False。str.endswith(suffix):
ReturnTrueif the string ends with the specifiedsuffix, otherwise returnFalse.
字符串以suffix变量值结尾,则返回True,否则返回False。str.isdigit():
ReturnTrueif all characters in the string are digits and there is at least one character,Falseotherwise.
字符串中的所有字符都是数字,且至少有一个字符,则返回True,否则返回False。str.islower():
ReturnTrueif all cased characters in the string are lowercase and there is at least one cased character,Falseotherwise.
字符串中的所有字符都是小写,且至少有一个字符,则返回True,否则返回False。str.isupper():
ReturnTrueif all cased characters in the string are uppercase and there is at least one cased character,Falseotherwise.
字符串中的所有字符都是大写,且至少有一个字符,则返回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