+ 3
list operations
nums=[1,2,3,4] print(4 not in nums) print(not 4 in nums) -------------------------------- here NOT and IN both are list operators.... which one is going to execute first....... not or in?
6 Réponses
+ 9
Usually membership operators don't have any precedence.
in and not in are different operators thus
print (4 not in nums) is obviously False as 4 is in nums
print(not 4 in nums ) would work like print(not True) {since 4 in nums is True } ; which gives us False
https://www.tutorialspoint.com/JUMP_LINK__&&__python__&&__JUMP_LINK/operators_precedence_example.htm
Interesting question tho.. a second opinion would be better.
+ 4
In your first case, "not in" is an operator in its own right. So it checks if the element is not part of the list.
In the second case the operators are independent, and the execution order is deternined by operator precedence.
"in" is evaluated first, resulting True, then not True = False is printed.
More info:
http://www.informit.com/articles/article.aspx?p=459269&seqNum=11
+ 4
Here's the official list of operator precedence in Python:
https://docs.python.org/3/reference/expressions.html#operator-precedence
I'm sharing this because it differs a little from the other two (though both of them explain this particular case correctly).
+ 1
print (not 4un nums) would work like print (not true)
0
#hope it help
data = [
[23, 11, 5, 14],
[8, 32, 20, 5]
]
color = input()
total=23+11+5+14+8+32+20+5
if color == "brown":
print(int((data[0][0]+data[1][0])*100/total))
elif color == "blue":
print(int((data[0][1]+data[1][1])*100/total))
elif color == "green":
print(int((data[0][2]+data[1][2])*100/total))
else:
print(int((data[0][3]+data[1][3])*100/total))
- 1
In python when we use not and in simultaneously even have some int in between they are treated as single keyword... Therefore in this case both have same precedence...