Thursday, March 26, 2015

Booleans In Python - Python Numbers - Python Tutorial

booleans in pythonBooleans In Python


In this Python tutorial, we are going to look at booleans in Python. Booleans are sometimes called truth statements in programming. Booleans are returned only a True or False.  You may be asking why we are covering booleans in the numbers section our Python tutorial. Booleans are indeed part of the integer family. A True boolean is equal to 1 and a False boolean is equal to 0.


Boolean Examples in Python


We will be checking out examples of booleans. Some of the methods, operators or functions may have not been covered yet in our tutorial series.  Just follow along we are going to get to all these very shortly. All these examples will be performed in our Python interpreter.


#boolean Examples in Python

#Boolean Operations
>>> False or True
True
>>> True or False
True
>>> False or False
False
>>> True or True
True
>>> True and False
False
>>> True and True
True
>>> False and False
False
>>> not False
True

#Comparison Operators
>>> 7 < 9
True
>>> 8 > 8
False
>>> 8 != 9
True
>>> 8 == 8
True
>>> 8 <= 9
True


Proof That Booleans are Integers


#Proof That Booleans are Integers

>>> True + False
1
>>> True * False
0
>>> True ** 8
1
>>> type(True)
<class 'bool'>
>>> a = True + False
>>> type(a)
<class 'int'>

We will do a lot of work with Python booleans over the next couple of Python tutorials. If you have any questions about booleans in Python leave, a comment below so we can help you.


 



No comments:

Post a Comment