29

I have seen similar questions asked, but none have answered my question. I am relatively new to python, and have no idea what i'm doing.

3
  • Unless you don't want to explicitly use Boolean type variable you don't need to. Python accepts it as True in many expression.
    – Geeocode
    Nov 9, 2018 at 1:38
  • bool(1) returns True
    – Joel
    Nov 9, 2018 at 2:08
  • 1
    Possible duplicate of python: convert a list of 1/0s to a list of boolean
    – user6655984
    Nov 9, 2018 at 2:09

5 Answers 5

43

Use:

>>> bool(1)
True
>>> bool(0)
False
>>> int(bool(1))
1
>>> int(bool(0))
0

Can convert back too.

Or a clever hack that could be quicker would be:

>>> not not 1
True
>>> not not 0
False
>>> 

Converting back:

>>> int(not not 1)
1
>>> int(not not 0)
0
>>> 
9

Only the following values will return False when passed as a parameter to bool()

  • None
  • False
  • Zero of any numeric type. For example, 0, 0.0, 0j
  • Empty sequence. For example, (), [], ''.
  • Empty mapping. For example, {}
  • objects of Classes which has bool() or len() method which returns 0 or False

Everything else returns True

Source1 Source2

4

Unless you don't want to explicitly use Boolean type variable you don't need to. Python accepts it as True in many expression:

print(True == 1)
print(False == 0)

Out:

True
True

In other cases you can use bool(1) of course.

print(bool(1))
print(bool(0))

Out:

True
False
3

Very simple:

bool(1)

Here are a few scenarios, to show:

print(bool(1))

Will return: True

print(bool(0))

Will return: False

1
  • Nitpick: bool(1) returns True, print(bool(1)) prints "True"
    – tedtanner
    Jul 25, 2022 at 23:16
1

To convert 1 to boolean type:

print(bool(1))

Which returns True.

Not the answer you're looking for? Browse other questions tagged or ask your own question.