Booleans & Operators

This is part of “An intro to Python” series of posts, with content from the Enki app. If you stumbled upon this, you can start from the beginning.

Beneath the magic of an if statement is another Python data type, bool, the boolean!

Python uses booleans to evaluate conditions.

A boolean variable can take on one of two values, True or False.

Using a comparison operator to check whether a condition has been met, such as x > 2, returns a boolean value.

This means that once an if statement condition evaluates to True, the indented code block will run.

Here, ’Get some food!’ is printed because the condition above evaluates to True.

Python booleans must be capitalized, and they belong to the bool class.

Boolean Operators

There are two keywords in Python that allow us to write more complex boolean conditions.

  • The and operator

When using the and operator, all conditions must evaluate to True for the code block to run.

  • The or operator

When using the or operator, at least one of the conditions must evaluate to True.

Andrei Calabangiu

Content Manager

About Enki

  • AI-powered, customized 1:1 coaching
  • Fully customized to team needs
  • Pay only for active learners and results

More articles