Sunteți pe pagina 1din 1

Discus the differences between the operator == and =. When would each be used?

Why
would each be used?

The = is used by Python to indicate an assignment statement while the ==is used for
purposes of equality. The = is used to assign a value to a variable since it is an assignment
operator whilst the == being a comparison operator checks to see if the two expressions give
the same value. Equality check returns true if it succeeds and else return false

Numbers, strings and Boolean values are compared by value and are considered equal if they
have the same value for example two strings are considered equal if they have the same number
of characters.

Variables, arrays, functions and objects are compared by reference for example two variables are
considered equal if they have they refer both to the same array or function.

Examples can be of the form:

b = 6 # the value of b is 6

a=3 b=2 c=3

a == c (# true because 2 is equal to 2)

a==b (# false because 3 is not equal to 2)

References

www.codechef.com.

S-ar putea să vă placă și