Python Tutorials · Python If...Else

Logical Operators

Learn all about Logical Operators in this comprehensive tutorial.

5 min read beginner
  • Logical operators are used to combine conditional statements.
  • The and keyword is a logical operator, and is used to combine conditional statements.
  • The or keyword is a logical operator, and is used to combine conditional statements.
  • The not keyword is a logical operator, and is used to reverse the result of the conditional statement.
  • You can combine multiple logical operators in a single expression.
  • Understanding how logical operators work with different values:
  • When combining multiple logical operators, use parentheses to make your intentions clear and control the order of evaluation.

Python Logical Operators

Logical operators are used to combine conditional statements. Python has three logical operators:

  • and - Returns True if both statements are true
  • or - Returns True if one of the statements is true
  • not - Reverses the result, returns False if the result is true

The and Operator

The and keyword is a logical operator, and is used to combine conditional statements. Both conditions must be true for the entire expression to be true.

python

The or Operator

The or keyword is a logical operator, and is used to combine conditional statements. At least one condition must be true for the entire expression to be true.

python

The not Operator

The not keyword is a logical operator, and is used to reverse the result of the conditional statement.

python

Combining Multiple Operators

You can combine multiple logical operators in a single expression. Python evaluates not first, then and, then or.

python

Truth Tables

Understanding how logical operators work with different values:

Condition 1Condition 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse
Condition 1Condition 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Using Parentheses for Clarity

When combining multiple logical operators, use parentheses to make your intentions clear and control the order of evaluation.

python

More Examples

python
python

Module quiz

2 questions
1

Which of the following is true about Logical Operators?

2

What is the most common pitfall when working with Logical Operators?

Answer all questions to submit.