Operators that Support to Python & Relational and Logical Operators with their Precedence.

To perform an specific operation the Operators are used. Python has a lot of operators that are either keywords or special characters. These operators are work on with Operands.

TYPES OF OPERATORS

Operators that support to Python can be classified into the following categories:-
  • Arithmetic Operators:- These are operators for adding, subtracting, multiplication, division, modulus, and exponential operators. All the arithmetic operators are special characters. For example:-   (+ : addition), (- : subtract), (* : multiplication) etc.
  • Comparison Operators:- These operators also called relational operators.These are used to compare two values. The  result is always a Boolean value - True or False. For example:- 
             (= = : return true if both the value are equal
             (>= : return true if the left value is greater or equal to the right value).
  • Bit wise Operators:- These operators also called binary operators and work on integers only. The value of the operand converted into binary and then perform the operation on every bit. For example:- (& bit wise AND operator), (^ : bit wise XOR operator).
  • Logical Operator:- Logical operator work with Boolean operands and return a Boolean value. AND, OR, NOT are the logical operators.
  • Assignment Operators:- Assignment operator (=) is used to assign the left operand value to the right operand.
    There are some compound assignment operators to perform some arithmetic operation between the two operands and then assign the value to the left operand.
    • = : simple assignment operator
    • += : adds the two operands and then assign the value to the right operand
    • -= : subtracts the right operand from the left and then assign the value to the left operand.
  • Membership Operators:- These operators are used to checking the presence of a value in a sequence. There are two types:- IN or NOT IN. These operators are generally used in if-else condition.
  • Identity Operators:- These python's operators are used to check if two variables point to the same memory location or not. There are two types:- NOT or IS NOT.

Relational and Logical Operators with their Precedence.

Relational Operators in Python
They are also known as comparison operators because they compare the values on both sides of the operator and conclude on the relation between the values. After comparison, it returns the Boolean value, i.e., either true or false. The following table contains different types of comparison operators and their descriptions, along with respective examples.
OperatorOperator NameDescriptionExample
==Equal toIf values of two operands are equal, then it returns true.I = 20, J = 20
(I == J) is True
!=Not Equal toIf values of two operands are not equal, then it returns true.I = 20, J = 20
(I == J) is False
Less thanIf the value of the left operand is less than the value of the right operand, then it returns true.I = 40, J = 20
(I < J) is False
>Greater thanIf the value of the left operand is greater than the value of the right operand, then it returns true.I= 40, J = 20
(I > J) is True
<=Less than or equal toIf the value of the left operand is less than or equal to the value of the right operand, then it returns true.I = 40, J = 20
(I <= J) is False
>=Greater than or equal toIf the value of the left operand is greater than or equal to the value of the right operand, then it returns true.I = 40, J = 20
(I >= J) is True
<> Not equal to (similar to !=)If values of two operands are not equal, then the condition becomes true.I=40, J = 20
(I <> J) is True.

Logical Operators in Python
Logical operators are mainly used for conditional statements. There are three types of logical operators, namely, AND, OR, and NOT. The following table contains all logical operators with their descriptions, as well as their respective examples.
OperatorOperator NameDescriptionExample
andLogical ANDWhen both sides’ conditions are true, the result is true; otherwise false.2<1 and 2<3
False
orLogical ORWhen at least one condition is true, then result is true; otherwise false.2<1 or 2<3
True
notLogical NOTReverse the conditionNot (5>4)
False
                           

               Operator Precedence in Python

Sometimes an expression contains multiple operators. In that case, operator precedence is used to determine the order of execution.
  • We can create a group of expression using parenthesis. The expression in the parenthesis is evaluated first before they can take part in further calculations.
  • Some operators have the same level of precedence. In this case, the expression is evaluated from the left to right.
Below lists the Relational and Logical operators precedence in the decreasing order.
==, !=, >, <, >=, <= (Relational(Comparison) Operators)
not, and, or (Logical Operators)

0 Comments