Python Booleans

Python Booleans

In programming, sometimes we want values to be either True or False which has assigned in programming as Boolean values.

We can compare two values directly which will return True or False accordingly as a Boolean value.



Example


print(10 == 10)
print(10 == 9)
print(10 > 9)
print(10 < 9)

Output

True
False
True
False

In Python, we use bool() function to check True or False.

bool() function always returns True until we don’t pass something from the following–

  • 0
  • empty string
  • empty list, tuple, dictionary
  • other empty objects like a set
  • anything like a function which returns a function

Example

print(bool(0))  # passing 0
print(bool(""))  # passing an empty string
print(bool([]))  # passing an empty list
print(bool({}))  # passing an empty dict
print(bool(()))  # passing an empty set

Output

False
False
False
False
False


bool() will return True if the value is not an empty one.


Example

print(bool(1))
print(bool(12.2))
print(bool(23j))
print(bool("aString"))
print(bool([1, 2, "Hello"]))
print(bool({"Key":"value"}))

Output

True
True
True
True
True
True


Keep Learning


Also Read:

  • Bakery Management System in Python | Class 12 Project
    In this article, we are going to build one such application that can manage a bakery. Yes! The bakery products are everyone’s favorite, right? We are going to build a console-based Bakery Management System in Python that can manage our bakery which cooks delicious kinds of stuff! This article is your end of wander for…
  • Filter List in Python | 10 methods
    Filter List in Python is the process of selecting specific items from a list that meet certain criteria. This is a task that you will encounter every now and then if you are working with python. So here you will learn 10 different ways to filter List in Python. 1. Using for loop We start…
  • Top 25 Pattern Programs in C++
    In this article, I will show you the Top 25 Pattern Programs in C++ that you must learn which will help you to understand the usage of nested loops. We will learn to create different geometrical shapes like Squares, triangles, Rhombus, etc. We have provided the code as well as output for all the patterns…
  • Currency Converter in C++
    In this article, we will build a simple program for Currency Converter in C++. The user will be able to convert currencies like Dollar, Euro, Pound, and Rupee. This is a console application and very simple to understand as we only use a bunch of conditionals to write the entire program. Features Users can convert…
  • SQLite | CRUD Operations in Python
    CRUD stands for Create Read Update Delete. I will show you how to perform CRUD Operations in Python. You need basic Tkinter and SQLite knowledge before you read further. This app is straightforward, when you will open this app, a GUI with 4 green colored buttons will open to perform CRUD(create read update delete) operations….
Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you