Assignment, Compound, Identity, and Membership Operator in Python | Operators in Python | Digital Community



Types of Operators in Python

 

 

Python provides us 7 popular types of operators:

 

 

·      Arithmetic Operators

 

·      Relational or Comparison Operators

 

·      Logical Operators

 

·      Assignment Operator

 

·      Compound Operator

 

·      Identity Operators

 

·      Membership Operators

 

 

Today we, discuss about Assignment Operator, Compound Operator, Identity Operator, Membership Operator :

 

 

·      Assignment Operators

 

·      Various Types of Assignment Operators

 

·      Compound Operators

 

·      Identity Operators

 

·      Membership Operators

 

Assignment Operators In Python

 

 

—  The Python Assignment Operators are used to assign the values to the declared variables.

 

—  Equals (=) operator is the most commonly used assignment operator in Python.

 

—  For example:

 

a=10

 

—  Shortcut for assigning same value to all the variables

 

x = y = z = 10

 

—  Shortcut for assigning different value to all the variables

 

x,y,z = 10,20,30

 

Guess the Output :

 

a,b,c = 10,20

print(a, b, c)

 

Output:

ValueError : Not enough values to unpack

 

a,b,c =  10,20,30,40

print(a, b, c)

 

Output:

ValueError : Too many values to unpack

 

 

 

Compound Assignment Operators

 

—  Python allows us to combine arithmetic operators as with assignment operator.

 

—  For example: The statement

x  =  x  +  5

 

—  Can also be written as

x  +=  5

 

 

 

Operator

 

Example

Meaning

+=

x+=5

x=x+5

-=

x-=5

x=x-5

*=

x*=5

x=x*5

/=

x/=5

x=x/5

%=

x%=5

x=x%5

//=

x//=5

x=x//5

 

 

Guess the Output :

 

a=10

print(++a)

Output:

10

 

a=10

print(a++)

Output:

SyntaxError : Invalid Syntax

 

Conclusion:

 

Python does not has any increment operator like ++. Rather it is solved as +(+x) i.e +(+10) which is 10.

 

However, the expression a++ is an error as it doesn’t make any sense.

 

Guess the Output :

 

a=10

print(--a)

Output:

10

 

a=10

print(a--)

Output:

SyntaxError : Invalid Syntax

 

 

Conclusion:

 

Python does not have any decrement operator like --.

 

Rather it is solved as -(-x) i.e -(-10) which is 10

 

However, the expression a-- is an error as it doesn’t make any sense.

 

Guess the Output :

 

a=10

print(+++++a)

Output:

10

 

a=10

print(-----a)

Output:

-10

 

 

Try to figure out yourself the reason for these outputs

 

 

Identity Operators

 

—  Identity operators in Python are is and is not

 

—  They serve 2 purposes:

 

·      To verify if two references point to the same memory location or not

 

·      To determine whether a value is of a certain class or type

 

 

Behavior of is and is not

 

—  The operator is returns True if the operands are identical, otherwise, it returns False.

 

—  The operator is not return True if the operands are not identical, otherwise, it returns False.

 

Examples of is Operator :

 

a=2

b=3

c=a is b

print(c)

 

Output:

False

 

Explanation:

 

Since a and b are pointing to 2 different objects, so

the operator is returns False

 

 

a=2

b=2

c=a is b

print(c)

 

Output:

True

 

 

Explanation:

 

Since a and b are pointing to the same objects, so the operator is returns True

 

 

a=2

b=type(a) is int

print(b)

 

Output:

True

 

Explanation:

 

type(a) is int evaluates to True because 2 is indeed an integer number.

 

a=2

b=type(a) is float

print(b)

 

Output:

False

 

Explanation:

 

type(a) is float evaluates to False because 2 is not a float number.

 

Examples of is not Operator :

 

a=“Digital”

b=“Digital”

c=a is not b

print(c)

 

Output:

False

 

Explanation:

 

Since a and b are pointing to the  same object, so

the operator is not return False.

 

a=“Digital”

b=“digital”

c=a is not b

print(c)

 

Output:

True

 

Explanation:

 

Since a and b are pointing to 2 different objects, so

the operator is not return True.

 

 

Membership Operators

 

—  Membership operators are used to testing whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).

 

—  There are 2 Membership operators

 

—  in

—  not in

 

 

The behavior of in and not in

 

— in: The ‘in’ operator is used to check if a value exists in a sequence or not

 

— not in : The ‘not in’ operator is the opposite of ‘in’ operator. So, if a value does not exists in the sequence then it will return a True else it will return a False.

 

 

Examples of in Operator

 

a=“Welcome”

b=“om”

print(b in a)

 

Output:

True

a=“Welcome”

b=“mom”

print(b in a)

 

Output:

False

 

 

primes=[2,3,5,7,11]

x=4

print(x not in primes)

 

Output:

True

 

primes=[2,3,5,7,11]

x=5

print(x not in primes)

 

Output:

False

 

 

And if you have any doubt regarding this topic then, you can ask your doubt in the comment section, I will respond to you as soon as possible.

 

 

 

 

 

DIGITAL COMMUNITY

We Make IT Happen.

No comments:

Post a Comment

If you have any doubts, let me know.