Skip to main content

Python Boolean


In this tutorial you will learn:

  • Boolean in Python
  • Boolean expressions
  • Importance of Boolean in programming.

Boolean in Python

Boolean is perhaps one of the most simple yet the most powerful data type not only in Python but in other programming languages as well. Boolean data type has either a value of True or a value of False. Even the keywords used in Python to define Booleans are True and False.

  1. myTrueBool = True
  2. myFalseBool = False

They can also act like numbers in some scenarios such as in arithmetic operations. In that case 0 represents false and 1 represents true.

Boolean expressions

An expression which can result to a Boolean value is called a Boolean expression.

For example:

  1. 9 == 1 #False
  2. 4 == 4 #True
  3. 3 == 0 #False

Booleans work using truth tables and we can determine what value of boolean to expect after checking the truth table. Mostly used truth tables are of AND, OR and XOR.

Importance of Boolean in programming

If it weren’t for Booleans we won’t have conditional statements in programming. Whenever we want to make a decision based on if a certain criteria is true or false then we use conditional statements. For making conditional statements we make use of the truth tables.

A

B

not A

not B

A == B

A =! B

A or B

A and B

T

F

F

T

F

T

T

F

F

T

T

F

F

T

T

F

T

T

F

F

T

F

T

T

F

F

T

T

T

F

F

F

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.