Boolean Type in Python

basic of python,

Sourabh Somani Sourabh Somani Follow Apr 16, 2018 · 1 min read
Share this

Introduction

In this article we will learn about boolean type of the python

What is Boolean?

In computer science, booleans has two values which can be either true or false. Boolean values represents truth value of the logic and boolean algebra.

In python there are two constants, True and False, that can be used to assign value directly. Python expects an expression to evaluate to a boolean value. These places are called boolean contexts. We can write any expression in boolean contexts.

Example

a=True;
print(a);

Output

Why True/False is capitalized in Python?

I already told you that “True” and “False” all “built-in constants of Python “ are capitalized or [upper] CamelCase.

In Python booleans can be treated as numbers. A True value is 1 and a False value is 0. You can also do any operation between boolean values in Python.

Example

a=True
b=True
print(a+b) 

Output

Output of the above result will be 2.

Following are some examples of boolean operations

How to check the type of any boolean value?

The type function returns the datatype of any arbitrary object.This is useful functions that can handle several types of data.

Example

a=True
print(typeof(a))
b=False
print(typeof(b))

Output

Join Newsletter
Get the latest news right in your inbox. We never spam!
Sourabh Somani
Written by Sourabh Somani Follow
Sourabh Somani is a Full-Stack Engineer with several years of hands-on experience. He has been awarded 4-times Microsoft MVP award consecutively and C# Corner MVP Award 6-times. Most of the time you will find him helping people on C# Corner and StackOverflow on JavaScript, .NET and Python. He is a frequent contributor to open source software development via forums, articles, and blogs. He regularly speaks at user groups and developer conferences. His core technology interests are JavaScript, Python and .NET.