Skip to content

[Python] Tutorial(5) Boolean, True, False

If we want to discuss the “True” and “False” in Python, we must mention a data type —— “Boolean”.

Boolean is also know “Bool”, which is the “Boolean value” that you maybe often see in books on programming language.

In the program, the simplest bool value is “True” or “False”, that is “1” or “0”, it is just that simple.


bool

bool = True

if bool == True:
    print('True')
else:
    print('False')


Output:

True

The result is also obvious, when we set the bool value to True, it prints “True”.

And then, we change the code to:

bool = 1

if bool == True:
    print('True')
else:
    print('False')


Output:

True

We also print “True”!

As just introduced, the bool value represents the very basic unit:

1 = True
0 = False

So we modify the code, we can confirm whether it actually meets the above two rules:

bool = 5

if bool == True:
    print('True')
elif bool == False:
    print('False')
else:
    print("It's not bool!")


Output:

It's not bool!

That’s right! Obviously, it doesn’t belong type bool !


Common Problem

There is a kind of judgment in Python that is easy to make mislead with bool: True, False, and it is recorded here by the way.

We also change the above code.

bool = 1

if bool:
    print('True')
elif not bool:
    print('False')
else:
    print("It's not bool!")


Output:

True

It looks very reasonable, doesn’t it ? This also makes it possible to write less “== True”.

But in fact, when we only use “if XXX”, this is not a bool value, but whether the variable has a value or calling it exists. (non-zero, False, None) !

bool = 5

if bool:
    print('True')
elif not bool:
    print('False')
else:
    print("It's not bool!")


Output:

True

We changed the program to bool = 5 again.

The answer we printed this time is not on same.

Very strange? Ha?

Determines that bool is present ! Everybody should be careful about this place, don’t write the wrong logic !


and, or

In the sample code we just introduced, there is a “not” in the second conditional judgment.

Those who have learned C# language (or C, I not sure) must know immediately, this is added to the variable ! There are similar functions for judging, but since I recorded this in order to make it easy for people who have never learned a procedural language, so let me talk about it.

a = 0
b = 1

if a and b:
    print('good!')
else:
    print('not good!')


Output:

not good!

“and”, logically means “and” (Sounds like nonsense.). In the code, if a single “if XXX” represents “the value of this variable”, then when we use “and” to string the two together, the meaning becomes:

“a exists ‘and’ b exists at the same time.”

So we said above, this value means that it is not 0, False, None …… but a is equal zero, so what will we print out?

Yes ! As long as a condition fails, the code under else will be executed !

What about “or” ?

Or, in fact, is literally meaning logically represents the meaning of “or”.

We simply rewrite the above code and change and to or:

a = 0
b = 1

if a or b:
    print('good!')
else:
    print('not good!')


Output:

good!

Yes ! Or Here the code means “as long as a or b has a match” … we know from the beginning that b is worth ! Then we naturally take the first set of conditions !


Not

Finally, we return to the “not” part.

“Not”, in fact, is the meaning of “non” in logic.

We will return to the code that just has and doesn’t meet the first condition to print only the part that is not good!

This time we simply modify the program and put the not into it.

a = 0
b = 1

if not a and b:
    print('good!')
else:
    print('not good!')


Output:

good!

We print “good” ! Why?

This is because “not” means “non-“.

The variable a is originally the part of the logical judgment that is judged as “negative”, that is, the part that does not meet the conditions.

Before we add not to a, let us make it “positive and negative”, we can say that we changed the conditional judgment to that we originally wanted to be “not qualified”, so if a was originally “If you don’t meet the conditions”, let us make the whole condition “eligible” … What an I taking about? It seems that the more we talk about it, the more chaotic, hahahahahaha.

In short, let everyone try it out!


Referneces


Read More

Leave a Reply