Computer Science, asked by Anonymous, 9 months ago

Pleasssssssssssseee answer this - Explain Membership operators & Identity operators in PYTHON.

Answers

Answered by AdorableMe
36

Membership operators  :-

These operators are used check whether the value/variable exist in a sequence or not.

• in : This operator returns True if a value exists in a sequence, False  otherwise.

\tt{In []: stock\_list = ['GOOG', 'MSFT', 'AMZN', 'NFLX']}

\tt{In []: 'GOOG'\ in\ stock\_list}\\\tt{Out[]: True}

\tt{In []: 'AAPL'\ in\ stock\_list}

\tt{Out[]: False}

• not in : This operator returns True if a value does not exists in a sequence, False otherwise.

\tt{In []: 'AAPL'\ not\ in\ stock\_list}

\tt{Out[]: True}

\tt{In []: 'GOOG'\ not\ in\ stock\_list}

\tt{Out[]: False}

______________________

Identity operators  :-

These operators are used to check if two values or objects belong to same  memory location or refer to same instance in Python. They can be used in  the following way :

• is : This operator returns True if both operands are identical, False  otherwise.

\tt{In []: a = 3}\\\tt{In []: b = a}

# True as both variables refers to same value in

# the memory

\tt{In []: a\ is\ b}

\tt{Out[]: True}

\tt{In []: x = 3}

# True as Python will create new reference of variable 'x'

# to value 3 on the same memory location

\tt{In []: x\ is\ a}\\\\\tt{Out[]: True}

• is not : This operator returns True if both operands are not on same  memory location, False otherwise.

\tt{In []: stock\_list = ['AMZN', 'NFLX']}

\tt{In []: my\_list = ['AMZN', 'NFLX']}

\tt{In []: stock\_list\ is\ my\_list}

\tt{Out[]: False}

# Though both lists are identical, they will not be stored

# at the same memory location as lists are mutable.

\tt{In []: stock\_list\ is\ not\ my\_list}

\tt{Out[]: True}


Vamprixussa: Awesome!
Answered by Anonymous
8

Answer:

Membership operators are operators used to validate the membership of a value. It test for membership in a sequence, such as strings, lists, or tuples. in operator : The 'in' operator is used to check if a value exists in a sequence or not.

Similar questions