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:

  • Artificial Intelligence Showdown: Weighing OpenAI Against Mistral Capabilities
    Overview As a developer using Python, choosing the right model for your project can be a daunting task. Two popular models that have gained significant attention in recent years are OpenAI GPT and Mistral. In this article, we will compare and contrast these two models, highlighting their strengths, weaknesses, and use cases to help you…
  • Codex Security enters experimental phase for testing and evaluation purposes
    Codex, the AI model developed by Microsoft, has made significant strides in recent times. The latest development in this space is the introduction of Codex Security, which is now available in research preview. This announcement has the potential to greatly impact the way developers approach AI-powered applications, particularly in terms of security and reliability. What…
  • How Balyasny Asset Management built an AI research engine for investing
    Balyasny Asset Management has developed an AI research engine for investing, marking a significant advancement in the use of artificial intelligence in finance. This engine is designed to improve investment decisions by analyzing vast amounts of data and identifying patterns that may not be apparent to human researchers. For more information, visit the source to…
  • Why All AI Models Are Slowly Becoming the Same Model
    Artificial Intelligence has progressed at an astonishing speed over the last few years. In a short period of time, the world moved from simple chatbots and rule-based systems to powerful language models capable of writing code, solving complex problems, and assisting in research. At first glance, it appears that many different companies are building very…
  • Aam Aadmi vs Corrupt System: How ChatGPT Helped One Guy Expose Govt Fraud, The Story: “Ravi and The Missing Light Pole”
    Ravi was just a normal guy from a small town in Uttar Pradesh. He worked in a private IT support company, nothing fancy, and earned enough to keep his home running. He lived in a street that had one big problem. The street was dark. No street light. At night, people were scared to walk….
Share:

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

Related Articles