Python operators:
Operators in general are used to perform operations on values
and variables. These are standard symbols used for the
purpose of logical and arithmetic operations. In this article, we
will look into different types of Python operators.
OPERATORS: These are the special symbols. Eg- + , * , /, etc.
OPERAND: It is the value on which the operator is applied.
Types of Operators in Python:
•Arithmetic Operators
•Comparison (Relational) Operators
•Assignment Operators
•Logical Operators
•Bitwise Operators
•Membership Operators
•Identity Operators
Arithmetic Operators in Python
Python Arithmetic operators are used to perform basic mathematical operations
like addition, subtraction, multiplication, and division.
In Python 3.x the result of division is a floating-point while in Python 2.x division of 2
integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is
used.
Operator Description Syntax
+ Addition: adds two operands x+y
– Subtraction: subtracts two operands x–y
* Multiplication: multiplies two operands x*y
Division (float): divides the first operand by the
/ x/y
second
Division (floor): divides the first operand by the
// second x // y
Modulus: returns the remainder when the first
% operand is divided by the second x%y
** Power: Returns first raised to power second x ** y
Comparison Operators in Python
In Python Comparison of Relational operators compares the values. It either
returns True or False according to the condition.
Operator Description Syntax
Greater than: True if the left operand is greater than the
> x>y
right
< Less than: True if the 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 the left operand is greater
>= than or equal to the right x >= y
Less than or equal to True if the left operand is less than or
<= equal to the right x <= y
= is an assignment operator and == comparison operator.
Logical Operators in Python
Python Logical operators perform Logical AND, Logical OR, and Logical NOT operations.
It is used to combine conditional statements.
Bitwise Operators in Python
Python Bitwise operators act on bits and perform bit-by-bit operations. These are used
to operate on binary numbers.
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<<
Assignment Operators in Python
Python Assignment operators are used to assign values to the variables.
Operator Description Syntax
Assign the value of the right side of the expression
= x=y+z
to the left side operand
Add AND: Add right-side operand with left-side
+= a+=b a=a+b
operand and then assign to left operand
Subtract AND: Subtract right operand from left
-= a-=b a=a-b
operand and then assign to left operand
Multiply AND: Multiply right operand with left
*= a*=b a=a*b
operand and then assign to left operand
Divide AND: Divide left operand with right operand
/= a/=b a=a/b
and then assign to left operand
Modulus AND: Takes modulus using left and right
%= a%=b a=a%b
operands and assign the result to left operand
Divide(floor) AND: Divide left operand with right
//= operand and then assign the value(floor) to left a//=b a=a//b
operand
Exponent AND: Calculate exponent(raise
**= power) value using operands and assign a**=b a=a**b
value to left operand
Performs Bitwise AND on operands and
&= a&=b a=a&b
assign value to left operand
Performs Bitwise OR on operands and
|= a|=b a=a|b
assign value to left operand
Performs Bitwise xOR on operands and
^= a^=b a=a^b
assign value to left operand
Performs Bitwise right shift on operands
>>= a>>=b a=a>>b
and assign value to left operand
Performs Bitwise left shift on operands
<<= a <<= b a= a << b
and assign value to left operand
Identity Operators in Python
In Python, is and is not are the identity operators both are used to check if two values
are located on the same part of the memory. Two variables that are equal do not imply
that they are identical.
is True if the operands are identical
is not True if the operands are not identical
Membership Operators in Python
In Python, in and not in are the membership operators that are
used to test whether a value or variable is in a sequence.
in True if value is found in the sequence
not in True if value is not found in the sequence
Ternary Operator in Python
in Python, Ternary operators also known as conditional
expressions are operators that evaluate something based on a
condition being true or false. It was added to Python in version
2.5.
It simply allows testing a condition in a single line replacing
the multiline if-else making the code compact.
Syntax : [on_true] if [expression] else [on_false]
Operator Precedence and Associativity in Python
The precedence order is described in the table below, starting with the highest
precedence at the top:
Operator Precedence and Associativity in Python
If an expression contains two or more operators with the same precedence then
Operator Associativity is used to determine. It can either be Left to Right or from Right
to Left.
The following code shows how Operator Associativity in Python works:
Precedence of Operators in Python
This is used in an expression with more than one operator with different precedence
to determine which operation to perform first.
The precedence of Logical Operators in python is as follows:
1. Logical not
2. logical and
3. logical or
The precedence of Bitwise Operators in python
is as follows:
1. Bitwise NOT
2. Bitwise Shift
3. Bitwise AND
4. Bitwise XOR
5. Bitwise OR
Non-associative Operators
In Python, most operators have associativity, which means they are evaluated from left to
right or right to left when they have the same precedence. However, there are a few
operators that are non-associative, meaning they cannot be chained together.
Example