close

How to convert number 1 to a Boolean in python

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to convert number 1 to a Boolean in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to convert number 1 to a Boolean in python?

  1. How to convert number 1 to a Boolean in python?

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

  2. convert number 1 to a Boolean in python

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

Method 1

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
>>> 

Method 2

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

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read