/* */ Click to Join Live Class with Shankar sir Call 9798158723

# Python Operators ?

Operators are a useful and powerful feature of the any programming language. Without them, the functionality of python is useless. It makes it easy for a programmer to write codes very efficiently and easily.

“An operator is a symbol that specifies the operation to be performed on various types of operands.”(operand may be a constant or variable or function which return a value). An operator may be associated with one or two or three operands.

Example:-   a + b

a - b

a * b

Note: In these expressions the identifiers a and b are operands and the symbols +, -, *. are operators.

## Classfication based on the types of operation:-

Operators Symbols
Arithmetic Operators +, - , *
Assignment Operators = , +=
Unary minus Operators +, -
Relational Operators < , >= , > ,<=
Logical Operators && , ||, !,
Boolean Operators True, false
Bitwise Operators & , ^
Membership Operators in, not in
Identity Operators is, is not

### Arithmetic Operator

Arithmetic operators are use to perform basic arithmetic operations like Addition, subtraction, division etc.

There are seven arithmetic operators are available in python. So these operators act on two operands, they are called      “ binary operator”. Let`s assume x=13 and y =5

Example :- x+y output will be 18.

=> - is a subtraction operator. Subtracts one value from another.
Example :- x-y output will be 8.

=> * multiplication operator . Multiplies values on either side of the operaor.
Example :- x*y output will be 65.

=> / division operator divides left operand by the right operand.
Example:- x/b output will be 2.6

=> % Modulus operator. Gives remainder of division.
Example :- x%y output will be 3

=> ** Exponent operator. Calculates exponential power value.
Example :- x**y gives the value of x to the power of b output will be 37 1293

=> // integer division this is also called floor division performs division and gives only integer quotient.
Example :- x//y output will be 2

### Expression Evaluation

When an expression contains several arithmetic's operator, we should know which operator is done first and which operation is done next. => First parentheses are evaluated.
=> Exponentiation is done next.
=> Multiplication , division , modulus and floor divisions are at equal priority.
=> Addition and subtraction are done afterwards
=> Finally assignment operation is performed .
Example :- d=(x+y)*z**a//b+c and values of the variable as x=1;y=2;z=3; a=2;b=2;c=3 then the given expression d=(1+2)*3**2//2+3
=> First parentheses are evaluated d=3*3**2//2+3
=> Exponentiation is done next d=3*9//2+3
=> Multiplication division modulus and floor divisions are at equal priority d=27//2+3 and then d=13+3
=> Addition and subtraction are done afterwards d=16
=> Finally assignment is performed the value 16 is now stored into d

### Assignment Operator

These operators are useful to store the right side value into a left side variable. They can also be used to perform simple arithmetic operations like addition subtraction etc. and then store the result into a variable. Let`s x=20, y=10 and s=5

=> = assignment operator Store right side value into left side variable.
Example:- s=x+y output s=30

=> += addition assignment operator adds right operand to the left operand and store the result left operand.
Example :- s+=x i.e s=s+x output 25

=> -= subtraction assignment operator subtracts right operand from left operand and stores the result into left operand.
Example :- s-=x i.e s=s-x output will be -15

=> *= multiplication assignment operator multiplies right operand with left operand and store the result into left operand.
Example :- s*=x i.e s=s*x output 100

=> /= division assignment operator divides left operand with right operand and stores the result into left operand.
Example :- s/=x i.e s=s/x result 0.25

=> %= modules assignment operator divides left operand with right operand and store the remainder into left operand.
Example:- s%=x i.e s=s%x output 5

=> **= Exponentiation assignment operator Performs power value and then stores the result into left operand. Example :- s**=x s=s**x output 9765625

=> //= floor division assignment operator perform floor division and then stores the result into left operand. Example:- s//=x s=s//y output

It is possible to assign the same value to two variables in the same statement in python.
x=y=1 print(x,y)
ouput 1 1

We can assign different values to two variables.
x=1; y=2 print(x,y)
output 1 2
It can also be written as x,y=1,2

Note:- python does not have increment(++) and decrement(--) operator as c and c++ java use.

##### Unary minus Operator

The unary minus operator is denoted by the symbol minus (-) when this operaor is used before a variable its value is negated. That means if the variable value is positive. It will be converted into negative and vice versa.
For example :
n=10
print(-n)    // #display -10 //

num =-10
num=-num print(num)    // #display 10 //

### Bitwise Operator

The operators that are used to manipulate the bits of given data are called bit wise operators.

Bitwise Operators Symbols
Bit-wise Negate ~
left shift <<
Right shift >>
Bit-wise and &
Bit-wise xor ^
Bit-wise or |

#### Bitwise complement operator(~)

This operator give the complement from of a given number. First convert into binary number than it convert into

Example :
65 1000001 binary number
0111110 1st complements

#if x=10
find the ~x value x=10
print(~x)
Output will be -11

#### Bitwise AND Operator

This operator performs AND operation on the individual bits of numbers the symbol for this operator is & which is called ampersand.

x=10=0000 1010
y=11=0000 1011
x&y=0000 1010
Example :#x =10 and y =11 find the value of x&y.

x=10=0000 1010
y=11=0000 1011.
after multiplying the bit we can get x&y = 0000 1010

#### Bit-wise OR operator

This operator performs OR operation on the individual bits of numbers the symbol for this operator is | which is called pipe symbol.

x=10=0000 1010
y=11=0000 1011
X|y=0000 1011
Example : #x =10 and y =11 find the value of x|y

x=10=0000 1010
y=11=0000 1011.
after multiplying the bit we can get x|y = 0000 1011

#### Bitwise XOR(^) Operator

This operator performs exclusive or (XOR) operation on the bits of numbers the symbol for this operator is ^ which is called cap, carat, or circumflex symbol.

x=10=0000 1010
y=11=0000 1011
x^y=0000 0001
Example :#x =10 and y =11 find the value of x^y

x=10=0000 1010
y=11=0000 1011
x^y = 0000 0001

#### Bitwise left shift Operator

This operator shifts the bits of the number to words left a specified number of positions. The symbol for this operator is << , read as double less than if we write x<<n, the meaning is to shift the bits of the towards left n positions.

If x=10, calculate x value if we write x<<2
Shifting the value of x to words left 2 positions will make the leftmost 2 bits to be lost the shifting the value of x 10= 0000 1010 now, x<<2 will be 0010 1000 = 40 (in decimal)the procedure to do this is explained as
X=10

#### Bitwise right shift Operator

This operator shifts the bits of the number to words right a specified number of positions. The symbol for this operator is >>, read as double grater than if we write x>>n the meaning is to shift the bits of the towards right n positions.

If x=10, calculate x value if we write x>>2
Shifting the value of x to words right 2 positions will make the right most 2 bits to be lost the shifting the value of x 10= 0000 1010 now,
x>>2 will be 0010 0010=2 = 40 (in decimal)the procedure to do this is explained as
X=10

### Relational Operators

Sometimes, we need to perform different actions based on different conditions. It can be perform with the help of Relational operator or conditional statement.

 > greater than Example:- a=4 and b= 2; a>b a is greater than b < less than Example:- a=2 and b=4; a a is less than b. >= greater than or equal to <= less than or equal to == equals to Example: a=2 and b=2;a==b a is equals to b != is not equals to Example: a=2 and b=4; a!=b a is not equals to b

### Logical Operator

Logical operators are useful to construct compound conditions. A compound condition is a combination of more than one simple condition. Each of the simple condition evaluated to true or false and then the decision is taken to know whether the total condition is ture or false.

 && AND if both condition true than statement will be execute || OR any one condition true than statement will be execute ! NOT NOT

### Boolean Operator

As we know that there are two type of literals. They are true or false Boolean operator can act upon bool type literals and they provide bool type output it means result provided by Boolean operators will be again either true or false.

and :- example x and y if both x and y are true then it return true otherwise false.
or :- x or y Boolean or operator if either x or y is true then it returns true.
else :- false.
not :- not x Boolean not operator if x is true it return false else true.

### Membership Operator

The membership operators are useful to test the membership in a sequence such as strings lists tuples or dictionaries. Example : if an element is found in the sequences or not can be asserted using these operators.
there are two type of membership operators :
=> in
=> not in

the in operator

This is return true if an element is found in the specified sequence if the selements is not found in the sequence then it returns false.

the not in operator

This works in reverse manner for in operator this operator return true if an elements is not found in the sequence.
If the element is found then it returns false.

Let`s take a group of strings in a list we want to display the member of the list using a for loop where the in operator is use the list of names is given below :
Names=[“palak”,”raj”,”prity”]

Here the list name is “names” it contains a group of names. Suppose we want to retrive all the names from this list we can use a for loop as :
for name in names :
print(name)
Output will be :
Palak
Raj
prity

In the for loop the variable name is used this variable will store each and every value of the list names it means name values will be Palak, raj , prity etc the operator in will check whether each of these names is a member of the list or not. If member is present in list it will return true else false. When it return true then print() function will be executed.

### Identity Operator

These operator compare the memory locations of two objects hence it is possible to know whether the two objects are same or not the memory location of an object can be seen using the id() function these function return an integer number called the identity number that internally represents the memory location of the objects.

For example id(a) gives the identity number of the object referred by the name a.
Example : a=25 b=25

In the first statement we are assigning the name or identitifier a to the object 25 in the second statement we are assigning another name b to the same object 25 in python everything is sonsidered as an object here 25 is the object for which tow name are given if we display an identiy number of these two variables we will get same numbers

Id(a)
16708989
Id(b)
16708989

There are two identity operator
=> is
=> Is not

the is operator

The is operator are use to compare whether two objects are same or not it will internally compare the identity number of the objects if the identity number of the objects are same it will return true otherwise it return false.

the is not operator
this is not operator return ture if the identity numbers of two objects being compared ae not same if they are same then it will return false.
The is and is not operators do not compare the values of the objects they compares the identity numbers or memory location of the objects. if we want ot compare the values of the object we should use equality operator(==)
Example :
a=2
b=2
If (a is b):
print

Example :
a=2
b=2
if (a is b):
print(“ a and b have same identity”)
else:
print(“ a and b do not have same identity”);
Output :
a and b have same identity

Example 2
a=[1,2,3]
b=[1,2,3]
if(a is b)
print(a and b are same”)
else:
print (“a and b are not same”)
Output :
a and b not same.

Since both the list are created at different memeoty lcoation we have their identity number different so is operator will take the list a and b as tow different list even though their values are same it means „is „ is not comparing their values for value == operator.