O P E R AT O R S
DEPARTMENT OF MECHANICAL ENGINEERING
DEFINITION
An Operator is a symbol that performs an operation.
An operator acts on some variables called operands.
If an operator acts on single variable, it is called unary operators
If an operator acts on two variables, then it is called binary
operator
If an operator acts on three variables, then it is called ternary
operator
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
TYPES OF OPERATORS IN PYTHON
Arithmetic Operators
Relational Operators or Comparison Operators
Assignment Operators
Logical Operators
Bitwise Operators
Special Operators
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
ARITHMETIC OPERATORS
Arithmetic operators are used to perform mathematical
operations like addition, subtraction, multiplication and division.
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
RELATIONAL OPERATORS
Relational operators compares the values. It either returns True or False
according to the condition.
OPERATOR DESCRIPTION SYNTAX
>
Greater than: True if left operand is greater than the x>y
right
< Less than: True if left operand is less than the right x<y
== Equal to: True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
>=
Greater than or equal to: True if left operand is greater x >= y
than or equal to the right
<=
Less than or equal to: True if left operand is less than x <= y
or equal to the right
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
ASSIGNMENT OPERATORS
Assignment operators are used to assign values to the variables.
OPERATOR DESCRIPTION SYNTAX
Assign value of right side of expression to left side
= operand x=y+z
Add AND: Add right side operand with left side
+= operand and then assign to left operand a+=b a=a+b
Subtract AND: Subtract right operand from left
-= operand and then assign to left operand a-=b a=a-b
Multiply AND: Multiply right operand with left
*= operand and then assign to left operand a*=b a=a*b
Divide AND: Divide left operand with right operand
/= and then assign to left operand a/=b a=a/b
Modulus AND: Takes modulus using left and right
%= operands and assign result to left operand a%=b a=a%b
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
OPERATOR DESCRIPTION SYNTAX
Divide(floor) AND: Divide left operand with right operand
//= a//=b a=a//b
and then assign the value(floor) to left operand
Exponent AND: Calculate exponent(raise power) value
**= a**=b a=a**b
using operands and assign value to left operand
Performs Bitwise AND on operands and assign value to left
&= a&=b a=a&b
operand
Performs Bitwise OR on operands and assign value to left
|= a|=b a=a|b
operand
Performs Bitwise xOR on operands and assign value to left
^= a^=b a=a^b
operand
Performs Bitwise right shift on operands and assign value to
>>= a>>=b a=a>>b
left operand
Performs Bitwise left shift on operands and assign value to
<<= a <<= b a= a << b
left operand
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
LOGICAL OPERATORS
Logical operators perform Logical AND, Logical OR and Logical
NOT operations.
OPERATOR DESCRIPTION SYNTAX
Logical AND: True if
and both the operands are x and y
true
Logical OR: True if
or either of the operands x or y
is true
Logical NOT: True if
not not x
operand is false
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
BITWISE OPERATORS
Bitwise operators acts on bits and performs bit by bit operation.
OPERATOR DESCRIPTION SYNTAX
& Bitwise AND x&y
| Bitwise OR x|y
~ Bitwise NOT ~x
^ Bitwise XOR x^y
>> Bitwise right shift x>>
<< Bitwise left shift x<<
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
IDENTITY OPERATORS
is and is not are the identity operators in Python. They are used
to check if two values (or variables) are located on the same part
of the memory. Two variables that are equal does not imply that
they are identical.
Operator Meaning Example
True if the operands are
is identical (refer to the x is True
same object)
True if the operands are
is not not identical (do not x is not True
refer to the same object)
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G
MEMBERSHIP OPERATORS
in and not in are the membership operators in Python. They are
used to test whether a value or variable is found in a sequence
(string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the
value.
Operator Meaning Example
True if value/variable is
in 5 in x
found in the sequence
True if value/variable is
not in not found in the 5 not in x
sequence
D E PA R T M E N T O F M E C H A N I C A L E N G I N E E R I N G