Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert an Integer to Boolean in Python

  • 18 Mar, 2025
  • Com 0
Converting an integer to boolean in Python

You can use the bool() constructor to convert an integer value to a boolean value in Python. The bool() accepts an integer and returns either True or False. 

If your input integer is 0, bool() returns False. For any integer other than zero, it will return True. For example, if you want to convert 1 to a boolean, bool(1) returns True.

Converting integer to boolean

print(bool(0))
# Output: False
print(type(bool(0)))
# Output: <class 'bool'>

print(bool(0.5))
# Output: True
print(type(bool(0.5)))
# Output: <class 'bool'>

print(bool(1))
# Output: True
print(type(bool(1)))
# Output: <class 'bool'>

print(bool(100))
# Output: True
print(type(bool(100)))
# Output: <class 'bool'>

One thing to note here is that a non-zero integer (e.g., 2) is truthy but not equal to True.

print(2 == True)
# Output: False (True is 1)

print(bool(2))
# Output: True

Boolean type is a subclass of integers.

print(isinstance(True, int))  
# Output: True

print(True + True)            
# Output: 2 (since True == 1)

That means True and False are subclasses of int.

In libraries like NumPy, bool(5) behaves like Python. However, array-wide operations may differ.

Negative integers

If you convert a negative integer into a boolean, it will still return True.

Converting negative integer to boolean

print(bool(-21))
# Output: True

print(bool(-1.9))
# Output: True

Implicit Conversion in Boolean Contexts

Condition statements like “if”, Python automatically evaluates integers in conditions using truthiness.

data = 5

if data:
    print("Non-zero")  # Executes because "data" is truthy
    # Output: Non-zero

y = 0

if not y:
    print("Zero")      # Executes because y is falsy
    # Output: Zero

Checking for Specific Values

You can use truthy/falsy logic directly inside the if statement instead of checks like if x != 0 or if x == 1.

# Redundant
if x != 0:
    pass

# Better
if x:
    pass

Inverting Boolean Values

Python provides a “not” operator that you can use to invert the boolean representation:

print(not 0)
# Output: True (0 is falsy)

print(not 5)
# Output: False (5 is truthy)

Conclusion

For explicit conversion, use the bool() constructor to transform from an integer to a boolean. While working with condition statements (if or while), Python automatically infers 0 to False and other integers to True.

Post Views: 152
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert Python Set to Numpy Array
How to Convert a String to Boolean in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend