Just like they sound, logical operators allow you to perform logical tests on values. For example, you might want to check if a value is greater than or equal to 2 and less than 5. These operators use to combine multiple relational operators.
Table of Contents
Just like they sound, logical operators allow you to perform logical tests on values. For example, you might want to check if a value is greater than or equal to 2 and less than 5. These operators use to combine multiple relational operators.
Types of Logical Operators
There are three types of logical operators in python language.
- and
- or
- not
i) and
The keyword “and” is used for this logical operator in python language.
Value_1 | Value_2 | Value_1 and Value_2 |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Example 1:
value_1 = 63
value_2 = 20
print(value_1>50 and value_2<10)
Output:
False
Example 2:
age = 25
income = 40000
if age>18 and income>30000:
print("Congratulations, You can register.")
else:
print("You are not eligible.")
Output:
Congratulations, You can register.
ii) or
The keyword “or” is used for this logical operator in python language.
Value_1 | Value_2 | Value_1 or Value_2 |
True | True | True |
True | False | True |
False | True | True |
False | False | False |
value_1 = 63
value_2 = 20
print(value_1>50 or value_2<10)
Output:
True
iii) not
The keyword “not” is used for this logical operator in python language.
Value_1 | not Value_1 |
True | False |
False | True |
value_1 = 63
print(not value_1>110)
Output:
True
Read the full details of Python Operators in this article: