Python中的not在前和在后有什么区别

nums = [1, 2, 3]
print(not 4 in nums)
print(4 not in nums)
print(not 3 in nums)
print(3 not in nums)
以上为学习时师长写的代码,本着严谨的态度反向来了一遍,结果一样,既然结果一样为什么还要not 4 in nums,4 not in nums都来一遍,我不理解

4 not in nums,这里not in是同一个语法结构,表示nums不包含4
not 4 in nums,其实是not (4 in nums),表示nums包含4的结果取反
从逻辑上是等价的,你习惯怎么写就怎么写
这其实跟多个变量相与的结果与他们取反的结果相或再取反等价一样
很多逻辑式在逻辑上是等价的
你找个看起来更顺眼,使用表达式更少的写法就好
没有人规定必须怎么写

not在前和在后没有区别,只是一种习惯性的写法,没有任何实际意义。

语法上完全没有问题,也都可以正常运行,但是在英语语法上,习惯 not in 连在一起,而且我们写代码是,如果not在前面,ide也会给出一个波浪线的警告:

img