Welcome to this tutorial as you know in the previous article we know all about conditional statements in python. In this tutorial, we are going to learn about logical operators in python.
Here are three logical operators in python having different operations including:
- and
- or
- not
Using “and” operator:
The “and” operator returns True if both conditions are true. In the following code, we check the condition by using the if statement, if x is less than y and if y is less than z.
Since both conditions are true, the code inside the if statement block will execute and print “Both conditions are true”.
x = 5
y = 10
z = 15
if x < y and y < z:
print("Both conditions are true")
Using “or” operator:
The “or” operator returns True if at least one condition is true. In this code, we check by if statement, if x is greater than y OR if y is less than z.
Since the second condition is true, the code inside the if statement block will execute and print “At least one condition is true”.
x = 5
y = 10
z = 15
if x > y or y < z:
print("At least one condition is true")
Using “not” operator:
The “not” operator returns True if the condition is false. In the last code, the if statement, we check if x is not equal to y. Since x is not equal to y, the code inside the if statement block will execute and print “x is not equal to y”.
x = 5
y = 10
z = 15
if not x == y:
print("x is not equal to y")
In this article we learn about logical operators in python, we learn “how logic operators work in python” and also “how to use logic operators in python”.
In the next tutorial, we are going to describe membership and identity operators in python. Make sure to subscribe to us to learn python. If you have any questions, you can ask I a comment.
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.