Python中的布尔数据类型
Python boolean类型是Python提供的内置数据类型之一,它表示两个值之一,即 True 或 False。一般用于表示表达式的真值。例如,1== 0 为真,而 2<1 为假。
Python布尔类型
布尔值只能是两种类型,即 True 或 False。输出
示例:布尔类型
Python3
a = True
type(a)
b = False
type(b)
Python3
# Python program to illustrate
# built-in method bool()
# Returns False as x is not equal to y
x = 5
y = 10
print(bool(x==y))
# Returns False as x is None
x = None
print(bool(x))
# Returns False as x is an empty sequence
x = ()
print(bool(x))
# Returns False as x is an empty mapping
x = {}
print(bool(x))
# Returns False as x is 0
x = 0.0
print(bool(x))
# Returns True as x is a non empty string
x = 'GeeksforGeeks'
print(bool(x))
Python3
# Declaring variables
a = 10
b = 20
# Comparing variables
print(a == b)
Python3
var1 = 0
print(bool(var1))
var2 = 1
print(bool(var2))
var3 = -9.7
print(bool(var3))
Python3
# Python program to demonstrate
# or operator
a = 1
b = 2
c = 4
if a > b or b < c:
print(True)
else:
print(False)
if a or b or c:
print("Atleast one number has boolean value as True")
Python3
# Python program to demonstrate
# and operator
a = 0
b = 2
c = 4
if a > b and b
Python3
# Python program to demonstrate
# not operator
a = 0
if not a:
print("Boolean value of a is False")
Python3
# Python program to demonstrate
# equivalent an not equivalent
# operator
a = 0
b = 1
if a == 0:
print(True)
if a == b:
print(True)
if a != b:
print(True)
Python3
# Python program to demonstrate
# is keyword
x = 10
y = 10
if x is y:
print(True)
else:
print(False)
x = ["a", "b", "c", "d"]
y = ["a", "b", "c", "d"]
print(x is y)
Python3
# Python program to demonstrate
# in keyword
# Create a lits
animals = ["dog", "lion", "cat"]
# Check if lion in list or not
if "lion" in animals:
print(True)
输出:
评估变量和表达式
我们可以使用Python bool()函数评估值和变量。此方法用于使用标准真值测试程序将值返回或转换为布尔值,即 True 或 False。
句法:
bool([x])
示例: Python bool() 方法
蟒蛇3
# Python program to illustrate
# built-in method bool()
# Returns False as x is not equal to y
x = 5
y = 10
print(bool(x==y))
# Returns False as x is None
x = None
print(bool(x))
# Returns False as x is an empty sequence
x = ()
print(bool(x))
# Returns False as x is an empty mapping
x = {}
print(bool(x))
# Returns False as x is 0
x = 0.0
print(bool(x))
# Returns True as x is a non empty string
x = 'GeeksforGeeks'
print(bool(x))
False
False
False
False
False
True
我们也可以不使用 bool()函数来计算表达式。布尔值将作为某种比较的结果返回。在下面的示例中,变量 res 将在相等比较发生后存储 False 的布尔值。
示例:表达式中的布尔值
蟒蛇3
# Declaring variables
a = 10
b = 20
# Comparing variables
print(a == b)
输出:
False
整数和浮点数作为布尔值
通过使用 Python 的内置bool()方法,数字可以用作 bool 值。任何以零为值的整数、浮点数或复数都被视为 False,而如果它们的值为任何正数或负数,则将其视为 True。
蟒蛇3
var1 = 0
print(bool(var1))
var2 = 1
print(bool(var2))
var3 = -9.7
print(bool(var3))
输出:
False
True
True
布尔运算符
布尔运算是 True 和 False 值的简单算术。这些值可以通过使用包括AND、Or 和 NOT在内的布尔运算符来操作。常见的布尔运算是——
- 或者
- 和
- 不是
- ==(等效)
- !=(不等价)
布尔或运算符
如果任一输入为 True,则布尔或运算符返回 True,否则返回 False。A B A or B True True True True False True False True True False False False
示例: Python布尔 OR 运算符
蟒蛇3
# Python program to demonstrate
# or operator
a = 1
b = 2
c = 4
if a > b or b < c:
print(True)
else:
print(False)
if a or b or c:
print("Atleast one number has boolean value as True")
True
Atleast one number has boolean value as True
在上面的例子中,我们使用了带有 if 语句和 OR运算符的Python布尔值,用于检查 a 是否大于 b 或 b 是否小于 c,如果任何条件为真,则返回 True(在上例中为 b 如果任一输入为 False,则布尔和运算符返回 False,否则返回 True。 Boolean Not运算符只需要一个参数并返回参数的否定,即为 False 返回 True,为 True 返回 False。 这两个运算符都用于压缩两个结果。 ==(如果两个结果相等,则等效运算符返回 True,而 !=(如果两个结果不同,则不等效运算符返回 True。 is 关键字用于测试两个变量是否属于同一个对象。如果两个对象相同,则测试将返回 True,否则即使两个对象 100% 相等,测试也将返回 False。 in运算符检查成员资格,即检查值是否存在于列表、元组、范围、字符串等中。布尔与运算符
A B A and B True True True True False False False True False False False False 示例: Python布尔与运算符
蟒蛇3
# Python program to demonstrate
# and operator
a = 0
b = 2
c = 4
if a > b and b
False
Atleast one number has boolean value as False
布尔非运算符
A Not A True False False True 示例: Python布尔非运算符
蟒蛇3
# Python program to demonstrate
# not operator
a = 0
if not a:
print("Boolean value of a is False")
Boolean value of a is False
布尔 ==(等价)和 !=(不等价)运算符
示例: Python Boolean ==(等价)和 !=(不等价)运算符
蟒蛇3
# Python program to demonstrate
# equivalent an not equivalent
# operator
a = 0
b = 1
if a == 0:
print(True)
if a == b:
print(True)
if a != b:
print(True)
True
True
是运算符
示例: Python是运算符
蟒蛇3
# Python program to demonstrate
# is keyword
x = 10
y = 10
if x is y:
print(True)
else:
print(False)
x = ["a", "b", "c", "d"]
y = ["a", "b", "c", "d"]
print(x is y)
True
False
在运算符中
示例:在运算符中
蟒蛇3
# Python program to demonstrate
# in keyword
# Create a lits
animals = ["dog", "lion", "cat"]
# Check if lion in list or not
if "lion" in animals:
print(True)
True