Python Operators
Python Operators
In Python programming, Operators in general are used to perform
operations on values and variables.
These are standard symbols used for logical and arithmetic operations.
•OPERATORS: These are the special symbols. Eg- + , * , /, etc.
•OPERAND: It is the value on which the operator is applied.
Types of Operators
1. Arithmetic Operators
2. Comparison Operators
In Python, Comparison (or Relational) operators compares values. It
either returns True or False according to the condition.
3. Logical Operators
Python Logical operators perform Logical AND, Logical
OR and Logical NOT operations. It is used to combine conditional
statements.
The precedence of Logical Operators in Python is as follows:
[Link] not
[Link] and
[Link] or
4. Bitwise Operators
Python Bitwise operators act on bits and perform bit-by-bit operations.
These are used to operate on binary numbers.
Bitwise Operators in Python are as follows:
[Link] NOT
[Link] Shift
[Link] AND
[Link] XOR
[Link] OR
print(bin(10)) # Output: 0b101, 0b used for binary format
5. Assignment Operators
Python Assignment operators are used to assign values to the variables.
This operator is used to assign the value of the right side of the
expression to the left side operand.
6. Identity Operators
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
7. Membership Operators
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
8. Ternary Operator
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.
9. Precedence and Associativity of Operators
Operator Precedence
This is used in an expression with more than one operator with different
precedence to determine which operation to perform first.
Operator Associativity
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.