You are currently viewing Logical Operators in Python with Examples

Logical Operators in Python with Examples

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.

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_1Value_2Value_1 and Value_2
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Table 1. Different Combinations of “and” operator

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_1Value_2Value_1 or Value_2
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse
Table 2. Different Combinations of “or” operator
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_1not Value_1
TrueFalse
FalseTrue
Table 3. Different Combinations of “not” operator
value_1 = 63

print(not value_1>110)

Output:

True

Read the full details of Python Operators in this article:

Leave a Reply