Booleans

Boolean is a data type in python which can have one of the two values;

  1. True
  2. False

A logical expression in python returns True or False. This True or False is stored in a variable. The data type of this variable is boolean.

True / False

  • If the variable has some content and it is not empty, then it is True.
  • If the variable is empty, then it is False.
  • 1 is True and 0 is False.

Why Booleans

There are some conditions when we need to execute some piece of code and skip some other part of code. In this situation, we use boolean variables. If they are True, we execute a piece of code. And if flase, we skip that code.

We know that 10 is always greater than 5. So, the expression "10 > 5" is true and the code written in if condition is executed. Else block will not be executed, because 10 is not less than 5.

Practice Exercises

Complete these exercises to reinforce your learning and earn XP

Sign in to track your progress and earn XP!
Exercise 1 of 2Easy

Which of the following is a best practice in programming?

10 XP~2 min
Exercise 2 of 2Easy

Code that is easy to read and understand is called ___ code.

10 XP~2 min