Is There a Difference Between "==" and "Is"

Is there a difference between == and is?

is will return True if two variables point to the same object (in memory), == if the objects referred to by the variables are equal.

>>> a = [1, 2, 3]
>>> b = a
>>> b is a
True
>>> b == a
True

# Make a new copy of list `a` via the slice operator,
# and assign it to variable `b`
>>> b = a[:]
>>> b is a
False
>>> b == a
True

In your case, the second test only works because Python caches small integer objects, which is an implementation detail. For larger integers, this does not work:

>>> 1000 is 10**3
False
>>> 1000 == 10**3
True

The same holds true for string literals:

>>> "a" is "a"
True
>>> "aa" is "a" * 2
True
>>> x = "a"
>>> "aa" is x * 2
False
>>> "aa" is intern(x*2)
True

Please see this question as well.

In SQL, is there a difference between IS and = when returning values in where statements?

All that you want to know you can find it here:

The IS and IS NOT operators work like = and != except when one or both
of the operands are NULL. In this case, if both operands are NULL,
then the IS operator evaluates to 1 (true) and the IS NOT operator
evaluates to 0 (false). If one operand is NULL and the other is not,
then the IS operator evaluates to 0 (false) and the IS NOT operator is
1 (true). It is not possible for an IS or IS NOT expression to
evaluate to NULL. Operators IS and IS NOT have the same precedence as
=.

taken from: SQL As Understood By SQLite.

The important part is: ...except when one or both of the operands are NULL... because when using = or != (<>) and 1 (or both) of the operands is NULL then the result is also NULL and this is the difference to IS and IS NOT.

Is there a difference between != and is not in C#?

Comparison table:













































































Operator!=is not
Original purposeValue inequalityNegated pattern matching
Can perform value inequalityYesYes
Can perform negated pattern matchingNoYes
Can invoke implicit operator on left-hand operandYesNo
Can invoke implicit operator on right-hand operand(s)YesYes1
Is its own operatorYesNo2
OverloadableYesNo
SinceC# 1.0C# 9.03
Value-type null-comparison branch elision4YesNo[Citation needed]5
Impossible comparisonsErrorWarning
Left operandAny expressionAny expression
Right operand(s)Any expressionOnly constant expressions6
Syntax<any-expr> != <any-expr><any-expr> is [not] <const-expr> [or|and <const-expr>]*
and more

is there a difference between is and as in a PL/SQL procedure

Both keywords can be used equivalently. See the documentation:

Sample Image

Is there a difference between !(x is null) and x is object

There is no appreciable difference. They both compile to the same CIL.

Check for yourself here

Note : this is the case even with nullable types as you can see here


From the Standard ECMA-334 C# Language Specification

12.11.11 The is operator

The is operator is used to check if the run-time type of an object is
compatible with a given type. The check is performed at runtime. The
result of the operation E is T, where E is an expression and T is a
type other than dynamic, is a Boolean value indicating whether E is
non-null and can successfully be converted to type T by a reference
conversion, a boxing conversion, an unboxing conversion, a wrapping
conversion, or an unwrapping conversion.

Update

To be completely concise and as pointed out by Alexei Levenkov

!(1 is null) vs. 1 is object shows some difference, the former will not compile due to not being nullable

Javascript + HTML - Difference between ' ' and

Both can be used actually. So if you have a function:

function DoSomething(inputValue) {
alert(inputValue);
}

You can call if in two ways:

DoSomething('test');

or

DoSomething("test");

What is the difference between ' ' and in python?

There is no difference at runtime. The only difference between the two types of quotes is the one you have already pointed out:

  • Single quotes need to be escaped inside single quoted string literals but not inside double-quoted string literals.
  • Double quotes need to be escaped inside double quoted string literals but not inside single-quoted string literals.

Note: If you use triple-quoted strings ("""foo""" or '''bar''') then you dont need to escape either (unless you happen to have a sequence of three quotes in a row inside the string).

Is there a difference between hi and hi!, like hi Visual guibg=#FFEC8B and hi! Visual guibg=#FFEC8B

In the form :hi! Group ctermbg=black, the ! nullifies the whole command, which makes it rather pointless.

In the form :hi! link FromGroup ToGroup, the ! forces the link to be made even if FromGroup already has some attributes set.

See :help :hi-link.

Is there a difference between `assert_eq!(a, b)` and `assert_eq!(a, b, )`?

Rust allows trailing commas in a lot of places, and most built-in macros respect this convention as well. There's no difference between the two assert_eq! calls. Similarly, we can declare a struct like this

struct Example {
foo: i32,
bar: i32, // Note: trailing comma on this line
}

Generally, the trailing comma does nothing useful if the call is a single line. So I would find this weird, but perfectly valid

assert_eq!(a, b, )

On the other hand, if the two expressions are complex enough to be on their own line, it helps with git diffs and readability. I find the following very idiomatic

assert_eq!(
some_complicated_expression(arg1, arg2, arg3),
some_complicated_expression(argA, argB, argC),
)


Related Topics



Leave a reply



Submit