Boolean in Python

Boolean in Python

What is Boolean in Python?

Do you ever answer your friends like yes and no? That's it you know booleans, But here we will learn how to say that it is a python way. True and False with the capital 'T' and capital 'F' Boolean values are used where we need to compare some expressions in python. We can compare the lists,tuples,sets using(==) and(!=)operators to get booleans.

Example

5==5   
# true

'john'=='doe'
# false

[1,2]==[2,1]   
# false as order matters in list.

(1,2)==(2,1)   
# false as order matters in tuples.

{1,2}=={2,1}   
# true as order not matters in sets.

Booleans can be converted into strings and numbers using built-in(str) and(int) functions. simple remember True=1 & False=0

Strings and Numbers can also be converted to booleans using the built-in (bool) function.

In numbers only (0) converts to false remaining all are True.

In strings only empty string('') converts to false remaining all are True.

Booleans can also be added and multiplied by numbers. when we do so the Python converts the boolean value into its number represents and then does the arithmetic.

Example

5+True#6

False-2#-2

3*true#3

False/5#0