Assignment Identity and Membership Operators in Python

In this class, we discuss the assignment identity and membership operators in python.

For Complete YouTube Video: Click Here

Assignment Operators in Python

Here, we use the concepts of logical and bitwise operators which were already discussed in our previous class.

These assignment identity and membership operators in python are used mostly in programming.

Let’s take examples and understand Assignment operators.

x=5. = we call assignment operator. we are assigning value to the variable.

x,y,z=1,2,5. we are assigning multiple variables. x is assigned with 1. y is assigned with 2. and z is assigned with 5.

x+=1. This works like x=x+1. += is an assignment operator.

Similarly we have.

x-=1 this works as x = x-1

x*=1 this works as x = x*1.

x/=1 this works as x = x/1.

x%=1 this works as x = x%1.

x//=1 this works as x = x//1.

x&=1 this works as x = x&1.

x|=1 this works as x = x|1.

x^=1 this works as x = x^1

x>>=1 this works as x = x>>1.

x<<=1 this works as x = x<<1.

Identity Operators

is, is not comes under identity operators.

They are used to check the given two variables point to the same object or not. ie to the same memory location or not.

As we have not yet discussed the object. Remember as same memory or not.

We will understand the object in our coming classes.

Example: x=5 , y = 5, z=10.

Take three variables. because of interning concept x and y point to the same memory location.

We discussed the interning concept here.

print(x is y) will display True.

print(x is z) will display False.

print(x is not z) will display True.

Membership Operators

in, not in are the two membership operators.

These operators are used to check values present in the list, string, dictionary, etc.

We have not discussed the list, string.

Let’s understand with an example. we cover those concepts, later classes.

x=” hello world”

In the above example, we take a variable containing the string hello world.

print(“h” in x) will display True.

“h” is in the given string. This is how we use in and not in operators.