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:

  • Unveiling the Future of AI Detector
    The rise of AI (Artificial Intelligence) content creation tools, especially ChatGPT, Bard, Jasper, etc., has imposed multiple threats to academia and the search engine world. Now, many students and researchers spend less time on research. Instead, they create content from different AI tools, paraphrase them, find a few relevant studies, and submit them as their … Read more
  • CodeWithHarry Earns 20 Lakhs per month from YouTube?
    CodeWithHarry is a YouTube channel owned by Haris Ali Khan, hailing from Rampur, Uttar Pradesh, India. In 2013, he embarked on his academic journey at the Indian Institute of Technology Kharagpur in West Bengal, where he pursued Industrial & Systems Engineering (ISE), a 5-Year Dual Degree Course. He initiated his YouTube channel on 28 April … Read more
  • Cleaning Service Booking System in Python Tkinter
    Introduction Hello friends, in this article we will see a very simple cleaning service booking system in Python tkinter. This project can not be used in the real world, this just tries to mimic real-world online service applications and that’s why the features of this app are limited. In simple words, you can use this … Read more
  • Farmers Ecommerce App using Python Tkinter
    Introduction Hello friends, I will show you how to create a simple e-commerce app for farmers using Python and the tkinter library in this tutorial. Before anything, let me tell you that this is not a real e-commerce app where anyone can list and buy products like we do on Amazon or Flipkart.Technologies used-> Python, … Read more
  • Guidelines for Project Collaboration Process
    Hi, Thanks for contacting Please send complete details about your project. Whatever features/requirements you want in your project/application/software, please write now, changes/updates later will/may not be entertained Necessary – Tell your budget Payments – You will need to pay according to milestones, let me explain. Let us say, there are 5 tasks in your project, … Read more
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