What are logical operators in Python?

Hardy 55 Published: 06/16/2024

What are logical operators in Python?

I'd be happy to explain logical operators in Python!

In Python, logical operators are used to combine conditional statements and return a boolean value indicating whether the condition is true or false. These operators can be used to write more complex conditional statements that involve multiple conditions.

There are three types of logical operators in Python: and, or, and not. Here's what each one does:

and: This operator returns True if both conditions on either side of the operator are true. If either or both conditions are false, it returns False.

Example:

x = 5

y = 3

if x > 2 and y < 4:

print("Both conditions are True")

else:

print("At least one condition is False")

In this example, the statement would return True because both conditions (x > 2) and (y < 4) are true.

or: This operator returns True if either or both conditions on either side of the operator are true. If all conditions are false, it returns False.

Example:

x = 5

y = 3

if x > 2 or y < 4:

print("At least one condition is True")

else:

print("All conditions are False")

In this example, the statement would return True because at least one condition (x > 2) is true.

not: This operator returns the opposite of what the condition on either side of the operator would normally be. In other words, if the condition is true, it returns False, and if the condition is false, it returns True.

Example:

x = 5

if not x < 2:

print("x is greater than or equal to 2")

else:

print("x is less than 2")

In this example, the statement would return True because the condition (x < 2) is false.

Logical operators can be used in various ways in Python programming. Here are some examples:

In conditional statements: You can use logical operators to write more complex conditional statements that involve multiple conditions.

Example:

x = 5

y = 3

if x > 2 and y < 4 or x < 2 and y > 4:

print("At least one condition is True")

else:

print("All conditions are False")

In loops: You can use logical operators to control the flow of a loop.

Example:

x = 1

y = 5

while not x > y or x == y:

x += 1

print(x)

In this example, the loop continues until x is greater than y, at which point it prints out the value of x.

In functions: You can use logical operators to return a boolean value from a function.

Example:

def check_conditions(x, y):

if x > 2 and y < 4 or x < 2 and y > 4:

return True

else:

return False

print(check_conditions(5, 3))

In this example, the check_conditions function returns a boolean value indicating whether at least one condition is true.

Python logical operators list with examples

Here is the list of Python logical operators along with their explanations and examples:

and (Logical And): This operator returns True if both conditions are true.

Example:

x = 5

y = 3

print(x > 2 and y < 4) # Output: True

In this example, both conditions x > 2 and y < 4 are true, so the output is True.

or (Logical Or): This operator returns True if either condition is true.

Example:

x = 5

y = 3

print(x > 2 or y < 4) # Output: True

In this example, at least one condition (x > 2) is true, so the output is True.

not (Logical Not): This operator returns the opposite of the condition.

Example:

x = 5

print(not x < 0) # Output: True

In this example, the condition x < 0 is false, so the output of not is True.

in (Membership Test): This operator checks if a value is present in an iterable.

Example:

my_list = [1, 2, 3]

print(1 in my_list) # Output: True

In this example, the value 1 is present in the list my_list, so the output is True.

not in (Membership Test Negation): This operator checks if a value is not present in an iterable.

Example:

my_list = [1, 2, 3]

print(4 not in my_list) # Output: True

In this example, the value 4 is not present in the list my_list, so the output of not in is True.

is (Identity Test): This operator checks if two objects are the same.

Example:

x = [1, 2, 3]

y = x

print(x is y) # Output: True

In this example, both variables x and y point to the same list, so the output of is is True.

is not (Identity Test Negation): This operator checks if two objects are not the same.

Example:

x = [1, 2, 3]

y = [4, 5, 6]

print(x is not y) # Output: True

In this example, variables x and y point to different lists, so the output of is not is True.

== (Equality Test): This operator checks if two objects are equal.

Example:

x = [1, 2, 3]

y = [1, 2, 3]

print(x == y) # Output: True

In this example, both lists x and y have the same elements, so the output of == is True.

!= (Equality Test Negation): This operator checks if two objects are not equal.

Example:

x = [1, 2, 3]

y = [4, 5, 6]

print(x != y) # Output: True

In this example, lists x and y have different elements, so the output of != is True.

> (Greater Than): This operator checks if one object is greater than another.

Example:

x = 5

y = 3

print(x > y) # Output: True

In this example, the value of x (5) is greater than the value of y (3), so the output is True.

< (Less Than): This operator checks if one object is less than another.

Example:

x = 5

y = 7

print(x < y) # Output: True

In this example, the value of x (5) is less than the value of y (7), so the output is True.

>= (Greater Than or Equal To): This operator checks if one object is greater than or equal to another.

Example:

x = 5

y = 5

print(x >= y) # Output: True

In this example, the value of x (5) is greater than or equal to the value of y (5), so the output is True.

<= (Less Than or Equal To): This operator checks if one object is less than or equal to another.

Example:

x = 5

y = 5

print(x <= y) # Output: True

In this example, the value of x (5) is less than or equal to the value of y (5), so the output is True.

Note that these operators can be combined using parentheses and logical operators (and, or) to create more complex conditional statements.