Python——逻辑运算符

众所周知,每一门语言都有一些条件判断的语句
Python也不例外

三大运算符:

  • and(与)
  • or (或)
  • not (非)

通过其中的含义和配合中文意思就能明白其中的作用


and:

作用:判断and左右两边的值或者表达式是否为 True
返回值:如果左右两边为True,那么and也会返回True,不符合则返回False

代码演示:
a = 5
if a == 10 - a and 10 == a*2:
	print("True")
else:
	print("Fales")
# 左边的返回值为True
# 右边的返回值同样为True

所以左右两边都为True时,and也会返回一个True,并进行if判断,最后返回"True"


or:

作用:判断左右两边是否有一边为***True***
返回值:

  • 当左边为True,右边为False时 返回值:True
  • 当右边为True,左边为False时 返回值:True
  • 当左右两边均为True, 返回值:True
  • 当左右两边均为False, 返回值:False

这里就不进行代码演示了


not:

作用: 判断not后面的返回值是否为: False
返回值:

  • 如果not后面的返回值为:False;返回值:True
  • 如果not后面的返回值为:True; 返回值:False
简单来讲就是:

你True则False
你False则True

代码演示:
a = False
b = True
print(not a)
print(not b)

输出结果为:
True
False

第一次写博客,多支持啊!

你可能感兴趣的:(Python基础知识,python)