Python Boolean


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



Also Read:

  • Make money with Python in 2022
    Did you know that data science was the fifth most common job on LinkedIn in 2017? Did you know that the demand for Python programmers is expected to grow by 19% from 2020-2021? Did you know that Python is one of the fastest-growing programming languages, and it will likely continue to expand as its adoption…
  • Top 15 Python Libraries For Data Science in 2022
    Introduction In this informative article, we look at the most important Python Libraries For Data Science and explain how their distinct features may help you develop your data science knowledge. Python has a rich data science library environment. It’s almost impossible to cover everything in a single article. As a consequence, we’ve compiled a list…
  • Top 15 Python Libraries For Machine Learning in 2022
    Introduction  In today’s digital environment, artificial intelligence (AI) and machine learning (ML) are getting more and more popular. Because of their growing popularity, machine learning technologies and algorithms should be mastered by IT workers. Specifically, Python machine learning libraries are what we are investigating today. We give individuals a head start on the new year…
  • File Explorer in Python using Tkinter
    Introduction Welcome to copyassignment.com. In this tutorial, we are going to learn File Explorer in Python using the Tkinter module. What is basically a file explorer? A file explorer is nothing but an application that is used to manage certain files and folders on your device where we can open, edit, copy, delete and move…
  • Drawing Application in Python Tkinter
    Introduction In this article, we will design and construct a basic Drawing Application in Python Tkinter GUI, where we can simply draw something on the canvas using a pencil and erase with an eraser, as well as the ability to change the thickness of a pencil and eraser. We may also modify the canvas’s background…


4 thoughts on “Python Boolean

  1. The output of the 1st example in python boolean section is wrong
    print(10==10)
    print(10=9)
    print(10>9)
    print(109
    false 10<9

    please try to crosscheck it

Comments are closed.