Python Logical Operators – with example

  • Reading time:18 mins read

Python Logical Operators

Logical operators are used to performing various logical operations in programming. Python logical operators also allow you to check various conditions using multiple conditional operators.

There are three logical operators in Python:

  • and
  • or
  • not

 

 

In the example below we have three variables and a conditional statement comparing two of the variables. The result of the conditional statement is True since the variable x is greater than y.

 

 

Now suppose we wanted to chain together more than one conditional operator comparing the three variables. In such a case we have to use a logical operator.

 

python logical operator

 

The “and” Operator

The and Python logical operator returns True if both conditions being evaluated are True and returns False if either of the statements is not true. In the following example, the and operator allows us to combine conditions checking if the variable x is greater than both variable y and z.

 

 

In this case, we get a True value since both conditions are True. Now suppose we change the value of variable z  to 25. This will render the second condition False since x will be less than z.

 

 

Now that one of the conditions is false the and operator returns a False value even if the other statement is true. The and operator equally returns a False if both statements are not true.

 

python logical operators

 

The “or” Operator

The or Python logical operator also allows you to check multiple conditions. However, unlike the and operator, the or operator returns a True value if only one of the conditions is true or if both conditions are true.

 

 

In the example above although x is not greater than z it is greater than y therefore since the first condition is True we get a True value returned.

 

Python logical operators

 

The “not” operator

The not Python logical operator allows you to reverse a single boolean value. For example, if the boolean value is False we can make it True and vice versa. In the example below the variable, x is greater than y, however, using the not operator before the condition reverses it to False.

 

 

Now that we have covered all the Logical operators we are going to look at slightly different examples using the logical operators above.

In the example below we want to change the variable cooking to True if both the fire and heat variables are true. Since both are not true the cooking variable remains False.

 

 

We can also compare one variable, for instance, we can say that if the variable fire is True change the cooking variable to True. Now since the variable fire is false the cooking variable remains False.

 

 

The and operator requires that both conditions are true to return a True value. Therefore even if we change the fire variable to True the cooking variable remains False since the heat variable is still false.

 

 

However, if we use the or operator and change the fire variable to True while heat stays False, the cooking variable, in this case, will change to True.

 

 

We can also use the not Python logical operator to reverse the variable’s boolean values. In the example below we have reversed the fire variable to True while the heat variable stays False. Since we are using the and operator in this case the cooking variable remains True.

 

 

On the other hand, if we reverse both variables to True then the cooking variable is changed into False. This is because we are using the and operator and both statements are now True.

 

 

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.