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.
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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
- How to use Python decorator
- How to use the Python argparse module
- How to use Python queue
- How to use Python threading
- How to concatenate strings in Python
- How to use the Python absolute value
- How to use the Python comparison operators
- How to use the Python logical operators
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.
