Introduction Variables Data Types Numbers Casting Strings Boolean Operators Lists Tuples Dictionaries If-Else While Loops For Loops Functions Arrays Scope Modules User Input File Handling Python Maths

Welcome to the lesson on Python Booleans

Booleans represent one of two values: True or False.

Boolean Values

In programming you often need to know if an expression is True or False.

You can evaluate any expression in Python, and get one of two answers, True or False.

When you compare two values, the expression is evaluated and Python returns the Boolean answer:

Example:

syntax
Try it out yourself

Python returns True or False, when we run a condition in an if statement.

Some Examples: Print a message based on whether the condition is True or False:

syntax
Try it out yourself

Evaluate Values and Variables

The bool() function allows you to evaluate any value, and give you True or False in return,

Some Examples: Evaluate a string and a number:

syntax

Some Examples: Evaluate two variables:

syntax
Try it out yourself

Functions can Return a Boolean

You can create functions that returns a Boolean Value:

Some Examples: Print the answer of a function:

syntax

You can execute code based on the Boolean answer of a function:

Some Examples: Print "YES!" if the function returns True, otherwise print "NO!"::

syntax
Try it out yourself

End of Python Booleans

You have learned Booleans in simple terms. Let's proceed on to Quiz.

Quiz Time!
Next Lesson
Back To Top