March 18, 2021•3 min read

Boolean Data Types in Python 3

A boolean is a data type that holds only 2 values, true and false. It earns its keep once you work with control structures.

Read in Indonesian

Sipp.. here we go again. Last time we covered the String data type. This time we'll cover the simplest data type in any programming language: the boolean data type in Python.

Why is it the simplest?🤔

Because a boolean data type only takes 2 kinds of values, true and false. Hmm... okay, let's go straight into it...

About the Boolean Data Type

As mentioned before, a boolean is a data type that can only hold 2 values, true and false. This data type is very useful when you arrange a programming algorithm, especially once you start dealing with conditions and loops.

A boolean is also called a logic data type. That's because it accepts logic operations like the ones in math (conjunction, disjunction, negation, etc.), even though they go by different names. Relax... programming isn't as hard as math🙃

The boolean data type has things called comparison operators and logical operators, and what are those now😢. These operators work to get a boolean value out of a condition. Take it easy, we'll cover them further later.

Boolean Data Types in Python 3

To create a boolean variable in Python you just declare a variable with the value True or False.

TEXT
>>> a = True
>>> b = False
>>> type(a)
<class 'bool'>
>>> type(b)
<class 'bool'>

One thing to watch here: writing a boolean value has to start with a capital letter. If you try writing true or false, an error comes out.

TEXT
>>> a = true
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined

You can also fill a boolean variable using comparison operations or logic operations.

TEXT
>>> a = 3 > 1 # 3 is greater than 1 = true (True)
>>> print(a)
True
>>> b = 3 < 1 # 3 is less than 1 = false (False)
>>> print(b)
False

The example above applies a comparison operation. The boolean output holds the truth value of a statement.

Boolean Comparison Operations in Python

The boolean data type has comparison operations. These are simple, exactly like the ones in math. For a clearer picture, see the table below.

Operation Name In Math In Python
Greater than > >
Greater than or equal to ≥ >=
Less than < <
Less than or equal to ≤ <=
Equal to = ==
Not equal to ≠ !=

One interesting thing about the python interpreter. This interpreter isn't only good as a number calculator, it works as a logic calculator too 🤩. Let's try.

TEXT
>>> 20 > 19
True
>>> 20 < 19
False

See, once the comparison operation is written, the result comes out right away (True or False)

Comparison operations aren't only good for comparing numbers either. Numeric operations and strings can both be compared.

TEXT
>>> 9*9 == 3**4
True
>>> "Inva" == "Inva"
True
>>> 900 <= 9**3
False
>>> "Soto" != "Sate"
True

Boolean Logic Operations in Python

Next are boolean logic operations. These compare several logic statements, the math counterparts being conjunction, negation, etc. The operands, or logical operator signs, are in the table below.

Operand Description
and True if and only if both sides are True
or True if one or both sides are True
not Flips the boolean value
TEXT
>>> True and True
True
>>> 2>0 and 9<10 # True AND True = True
True
>>> 2<0 and 9>10 # False AND True = False
False
>>> 2<0 or 9>10 # False OR True = True
True
>>> not(True or False) # not(True) = False
False

Wrapping Up

It's true that boolean use still isn't very visible right now, but the boolean is a very important concept when you arrange programming logic. Especially in loops and conditions.

For now that's enough on the boolean data type in Python. Next time we'll cover list data types in Python 3.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.