Menu

Logical Operators

Logical Operators

Logical operators are the operators in an algorithm that combine conditions together, or reverse a condition. You need three of them: AND, OR and NOT. A logical operation always produces a Boolean result: either true or false, never a number. Because of this, logical operators are also called Boolean operators.

They are a separate operator type from arithmetic operators, which calculate with values, and from relational operators, which compare two values. Each type does a different job, so each gives you a different kind of answer back.

  • Arithmetic operators calculate with values and give a number, such as + and /.
  • Relational operators compare two values and give true or false, such as != and >.
  • Logical operators combine or reverse conditions and give true or false, such as OR and NOT.

Because logical operators work on conditions, you use them when a decision depends on more than one test, or on the opposite of a test. A decision such as whether a cyclist may enter a race needs a straight yes or no, so the final test must produce true or false: AND and OR join two conditions into one, and NOT turns a condition round. Each of the three has its own rule for when it gives true.

In Python the three are written in lowercase as and, or and not, but they do the same job as AND, OR and NOT.

minutes_practised = 45
laps_completed = 12
print(minutes_practised + 15)
print(minutes_practised >= 40)
print(minutes_practised >= 40 and laps_completed >= 10)

Output:

60
True
True

Practise this