python布尔运算符_Python非布尔运算符教程

本文介绍了Python中的not布尔运算符,用于检查数据或变量的不等式。通过示例展示了如何检查变量是否不等于特定值,使用!=运算符进行不等式判断,以及在列表或集合中检查值是否存在。此外,还讨论了对变量进行not操作的情况。
摘要由CSDN通过智能技术生成
python布尔运算符

python布尔运算符

Python provides wide range of the logical operators. One type of logical operator is boolean operators where we can check the similarity or equality of the given data or variables. In this tutorial we will examine the not Boolean operator which can be used inequality of the provided values or data.

Python提供了广泛的逻辑运算符。 逻辑运算符的一种类型是布尔运算符,我们可以在其中检查给定数据或变量的相似性或相等性。 在本教程中,我们将研究not布尔运算符,该运算符可以用于提供的值或数据的不等式。

检查是否不相等 (Check If Not Equal)

We will start with a simple example where we will check whether given variable age is 10 . We will use not operator. We should use not operator with is operator like below.

我们将从一个简单的示例开始,我们将检查给定的变量age是否为10 。 我们将使用not运算符。 我们不应该在is运算符中使用not运算符,如下所示。

age=9 
if(age is not 10):      
  print("Age is not 10")
Check If Not Equal
Check If Not Equal
检查是否不相等

不是操作员标志!=(Not Operator Sign !=)

We have alternative way to express not logic. We can use != operator sign the same fashion as not . We will use previous example again with != which check if the age variable is not 10.

我们有另一种表达方式, not逻辑。 我们可以使用!=运算符签署与not相同的方式。 我们将再次使用前面的示例使用!= ,它检查age变量是否不是10。

age=9 
if(age != 10):           
  print("Age is not 10")
Not Operator Sign !=
Not Operator Sign !=
不是操作员标志!=

检查给定列表或数组中是否不存在 (Check If Not Exist In Given List or Array)

Another useful usage case for not operator is checking given value or variable against a list or set. In this example we will check whether variable a value exist in the given list.

not操作员的另一个有用用法是对照列表或集合检查给定的值或变量。 在此示例中,我们将检查给定列表中是否存在变量值。

numbers=[1,3,5,7] 
if 2 not in numbers: 
  print("2 is not in numbers list")
Check If Not Exist In Given List or Array
Check If Not Exist In Given List or Array
检查给定列表或数组中是否不存在

不是这样的(Not Not)

As not is logical operation we can use it multiple times in a single expression. In this example we will not two times for single value. In this case we will not True value two times. At first it will be false but with the second not it will be True again.

not逻辑运算,我们可以在单个表达式中多次使用它。 在此示例中,单个值将not两次。 在这种情况下,我们将不会两次获得True值。 起初它是假的,但第二次它不是True

status=not not True 
print(status)
Not Not
Not Not
不是这样的

注意变量(Noting Variable)

We can also not given data or variable. In this case we will not number 10 and check if equal with 10 .

我们也not给出数据或变量。 在这种情况下,我们将not编号10并检查是否等于10

if not 10 == 10:                       
  print("10 is equal with not 10") 
else: 
  print("10 is not equal with not 10")
Noting Variable
Noting Variable
注意变量
LEARN MORE  Comparing Strings In Python
了解更多比较Python中的字符串

翻译自: https://www.poftut.com/python-not-boolean-operator-tutorial/

python布尔运算符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值