## Python Booleans || Python Tutorial || Learn Python Programming

#### Sharing buttons:

Computers are funny creatures. They think in terms of 1’s and 0’s, True and False.

While Python has several numeric types, there is only one logical type: booleans. A boolean

can only take 2 values: True or False. And this is all you need, IF you are logical...

Booleans are a built-in data type in Python. There are only two values: True and False.

Take care to note that “true” and “false” are both capitalized. If you type “True”

correctly into the console, Python echoes it back to you.

But if you type it incorrectly, you will receive a NameError.

The same thing happens with False. If you capitalize it, you get a friendly echo. Uncapitalized

returns an error.

Booleans are commonly encountered when comparing two objects. For example, suppose “a=3”

and “b=5”.

To compare two numbers, use the double equals operator:

We get “False” since “a” and “b” are different integers.

Notice that you use one equals sign to assign numbers to a variable,

and a double equals sign to compare them.

In addition to testing if two numbers are the same, you can test if they are different

using the “not equal” operator. This comparison returns “True” because it is true that

“a” does not equal “b”. By the way, the exclamation mark is commonly used as a

logical “not” symbol in programming languages. So this symbol literally reads as “not equal”.

And finally, in addition to comparing two numbers for equality or inequality, you can

test to see if one is larger than the other.

Is “a” greater than “b”? No. This is a false statement.

Is “a” less than “b” ? Yes. This is a true statement.

If you inspect the type of True… and False, you see the type is “bool”.

This suggests another way to create booleans: by passing values to the boolean constructor.

For example, let’s convert some numbers to booleans: 28…

– e…

0…

In Python, 0 is converted to False, while every other number is converted to True.

We can also convert strings to booleans. For example,

“Turing”...

A space...

And the empty string…

In Python, the empty string is converted to False, while every other string is converted

to True.

This is a general principle in Python. When converting a value to a boolean, trivial values

are converted to False, while non-trivial values are converted to True.

Just as you can convert objects to booleans, you can convert booleans to other types of

objects.

If you convert “True” to a string, it returns… “True”. But notice this is

a string, since it is surrounded by quotes. The boolean value does not have quotes.

A similar thing occurs when you convert “False” to a string. The string has quotes, the boolean

does not.

You can also convert booleans to numbers. If you convert “True” to an integer, you

get 1. And if you convert “False” to an integer,

you get 0.

Look what happens if you add a number and a boolean:

Python recognizes that you are trying to add “True” to an integer, so it first converts

it to an integer then adds. What do you think 10 times “False” will be?

Zero. Like before, Python recognizes you are

trying to perform an arithmetic operation, so it converts “False” to the number 0

then multiplies.

Is this something you will use? Probably not. But it does highlight that Python treats 1

as True and 0 as False, and vice-versa. In computer science, this is a fundamental fact.

True or False: Booleans come in only two values… True!

True or False: We will be making another Python video soon… Definitely true!

True or False: Subscribers make fewer errors in their code… Very, very True…