You are on page 1of 14

Python Operators

Python Operator List


• Arithmetic Operator
• Relational Operator / Comparison Operator
• Logical Operators
• Bitwise Operator
• Assignment Operator
• Special Operator
Assignment Operators
• Addition (+)
• Subtraction (-)
• Multiplication (*)
• Division (/)
• Modular (%)
• Floor Division (//)
• Exponentiation (**)
Relational Operator
• > Greater Than (a>b)
• < Less than (a<b)
• >= Greater than and equal to (a>=b)
• <= Less than and equal to (a<=b)

Equality Operator
• == equal to (a==b)
• != not equal to (a!=b)
Logical Operator
• and
• or
• not
Logical Operator
For boolean type behaviour
• and : If both arguments are true then only result is
true.
• or : if at least one argument in true then result is true.
• not : Complement

• True and False => False


• True or False => True
• Not False => True
For Non-boolean types behaviour
0 means False
Non-zero means True
Empty string is always treated as False
x and y:
If x is evaluates to false return x otherwise return y

10 and 20 => 20
0 and 20 => 0
X or y:
If x evaluates to True than result is x otherwise result is y

10 or 20 =>10
0 or 20 =>20

Not x:
• If x is evaluates to False then result is True otherwise False
• not 10 => False
• Not 0 => True
Bitwise Operator
• We can apply these operators bitwise.
• These operator are applicable only for int and boolean types.

• & - Bitwise AND


• | - Bitwise OR
• ^ - Bitwise XOR
• ~ - Bitwise Complement Operator
• << - Left Shift Operator
• >> - Right Shift Operator
Assignment Operator
• x+=10 => x=x+10

• +=
• -=
• *=
• /=
• //=
• **=
• &=
• |=
• ^=
• >>=
• <<=
Ternary Operator
Syntax:
X= firstvalue if condition else secondvalue
Eg.
X= 30 if a<b else 40

If condition is true then first value will be considered otherwise else


secondvalue will be considered.

ex: a,b=10,20
x=30 if a<b else 40
print(x) => 30
Identity Operators
is
is not

A is b returns True if both a and b are pointing to the same object.


A is not b returns True if a and b are not pointing to the same
object.

a=10
b=10
Print(a is b)
Membership Operator
in
not in

In: Return True if the given object present in the specified


collections
Not in: Returns True if the given object not present in the
specified collections.
Operator Precedence
() Paranthesis
** Exponential Operator
~ Bitwise Complement
* , / , %, // Multiplication, Division, Modular, Floor Division
+, - Addition , Subtraction
<< ,>> Left Shift, Right Shift
& Bitwise AND
| Bitwise OR
>,>==,<,<=,==,!=
= , +=, -=, *= …
is, is not Identity Operator
in, not in Membership Operator

You might also like