Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis0
what are the different context of != and not operator in real life problems
!= and not operator
3 Respostas
+ 9
# Specify a language in Relevant Tags to narrow down question scope. I'm proposing an answer in Python because I see from your profile that you are learning it.
# The `!=` operator is used to compare values of its operands. It returns a boolean state, True when both operands' values equals, False otherwise.
a, b = 1, 2
print("a == b ->", a == b)
# `==` operator tests for equality
print("a != b ->", a != b)
# `!=` operator tests for inequality
# The `not` operator returns an inverted logical expression.
# It returns True when the given operand was False; otherwise it return False when the given operand was True.
# It only accepts one operand to work.
# Basically, this is a logical operator, so its operand ideally should be something that can be evaluated as boolean expression.
young = True
print("young ->", young, "not young ->", not young)
if not young:
print("You are old")
else:
print("You are young")
Hth, cmiiw
+ 3
Ipang couldnt have done a better job myself
+ 3
(A) 'Not equal toā vs. (B) āNot trueā
Examples:
(A) sky = ādarkā
if sky != ālightā then sun = 0
(B) sun = 0
if not sun then sky = ādarkā
In (A), you compare the value of sky to the value ālightā. If those two values are not equal, then you proceed with the action.
In (B), you check whether the expression sun is true. If that expression is not true, then you proceed with the action.
Combined Example:
sky = ādarkā
sun = 1
if not sun or sky != ālightā then lamp_flag = 1
In that example, the expression not sun is false, but the second condition is true, so the action occurs.