In python, there are many operators which we use in our program. Operators are used in manipulating a particular value or operand. Logical operators are used mainly to evaluate an expression that makes the decision.
Operators play an important role in many programming languages like C, Python, C++, etc. Operators are mainly used for performing operations on variables and values.
Operators are special symbols that perform logical, mathematical, relational, and arithmetic computations to get the final result. In other words, operators in python are used to perform operations between variables.
Example: Consider an equation 5+6=11. From the example, the operators are + and =; these operators perform operations like addition, subtraction, multiplication, etc.
There are two operands from the above flowchart, namely operand 1 and operand 2; when we add an operator between them, we can get the result according to the type of operator we are using here.
Operands are nothing but variables on which operations are done.
Example: Consider an equation c + d – e = f, here c, d, e, f are operands and +, -, = are operators.
Python has different operators. They are:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Bitwise operators
- Membership operators
Here we are discussing logical python operators.
To combine conditional statements, we use logical operators. We use logical operators to check multiple conditions at the same time. Python supports three logical operators, they are:
Logical And Operator
The “LOGICAL AND OPERATOR" checks whether the given condition is TRUE. It checks the given conditions simultaneously.
Example: consider two operands, 'a' and 'b'. This shows that the AND OPERATOR will return TRUE only if both conditions are TRUE, and it returns FALSE only if any of the conditions is FALSE.
Let us take an example program that uses AND OPERATOR to check whether the conditions are TRUE or FALSE.
Book cost = 200
Book cost > 100 and Book cost < 300
This is the way to write AND condition between statements or conditions.
The above example program shows how "OR OPERATOR" works.
Here Book cost>100 and the Book cost<300 returns TRUE because both the conditions return TRUE. From the given condition Book cost =200, both conditions check this statement and return the final result.
|x||y||x and y|
From the above table, we understand that if both x and y are TRUE, then x and y return TRUE. If any of the conditions of both x and y are false, then it returns FALSE.
Logical Or Operator
As discussed, AND OPERATOR, the “OR OPERATOR” also check multiple conditions simultaneously. Still, there is a small change: the OR OPERATOR returns true if the individual condition or both conditions are True. This is the way to write the "OR" condition between statements.
Book cost > 400 or Book cost < 200
The above example shows us how to use “OR OPERATOR”.
From the above example, the Book cost >400 and the Book cost<200 return FALSE because both conditions evaluate FALSE. Both conditions check the given condition: Book cost=300 and returns result.
|x||y||x or y|
From the above table, if both x and y are TRUE, then it returns TRUE; if the individual condition is TRUE, it returns TRUE. It will return FALSE when both statements need to be FALSE.
Logical Not Operator
This operator is another type of logical operator. This LOGICAL NOT OPERATOR only applies to one condition and will reverse the given condition.
It works with a single Boolean value. If the Boolean value is TRUE, then this operator returns FALSE.
From this, we understood that if the condition is TRUE, it returns FALSE, and if the condition is FALSE, then it returns TRUE.
Book cost =300
not Book cost > 500
This is the way to write LOGICAL NOT OPERATOR for a given condition.
The below example shows us how to write NOT OPERATOR.
From the above example, we observe that when Book cost = 300 and it returns TRUE when NOT OPERATOR was involved. Indeed, the Book cost is not greater than 500, so it returns TRUE.
From the above table, we observe that if the condition is TRUE, then not x becomes FALSE, and if the condition is FALSE, then not x becomes TRUE.
Rather, we can also use the combination of AND and NOT OPERATORS simultaneously.
Here is the example program
Program with Output
From the above example, we noticed that the output is FALSE because we combined NOT AND OPERATORS. From this example, we observed that Book cost = 500, and the conditions Book cost > 300 and Book cost < 600 are TRUE. The result becomes FALSE when we apply NOT OPERATOR because it returns the reverse of the given condition.
Precedence of Logical Operators in Python
In python, an expression combines operands, operators, and function calls. Python interpreter can evaluate a valid expression.
The word precedence shows how to follow the order of operations if there are many operators. It mainly tells the order of operators and which operations need to perform first.
In many cases, one expression can contain two or more operators. At that time, the operator precedence takes place.
The above figure shows that NOT OPERATOR has the highest precedence. The AND OPERATOR has the second precedence, and OR OPERATOR has the lowest precedence.
#Precedence of ‘OR’ and ‘AND’ OPERATORS name= “ABC” age= 16 if (name == “ABC” or name ==” XYZ”) and age<=17: Print (“Your name is correct”) else: Print (“Your name is incorrect”)
Your name is correct.
In the above example, the precedence of AND operator is higher than the precedence of the OR operator. So first, it checks the precedence, then returns the desired output rather than checking the if and else block.
Associativity for Python Operators
Associativity in python maintains the order for an expression when multiple operators have the same operator precedence, as we know that operators have associativity from left to right.
For suppose, associativity will let you know the order of operations if two operators have the same precedence.
Let us consider multiplication and floor division which have the same precedence. It should be evaluated from left to right if it occurs in an expression. That is left operator will be evaluated first.
#This program shows the left-right associativity of operators Print ( 7 * 8 // 4)
Here OR and FLOOR DIVISION have the same precedence, so OR is evaluated first because associativity checks from left to right.
# This program shows the left-right associativity of operators Print ( 8 // 4*2)
Here OR and FLOOR DIVISION have the same precedence, so FLOOR DIVISION is evaluated first because associativity checks from left to right.
Applications of Operators
- These are used for manipulating each data item and returning a result.
- Operators are represented using keywords and special characters.
- Operations on operands and operators will give the result.
- It also shows the precedence of operators, like which operator should be evaluated first.