0
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.