What is Boolean ?

Boolean is a data type, having two values usually denoted True and False, intended to represent the truth values of logic. It is named after "George Boole", who first defined an algebraic system of logic in the mid 19th century. The Boolean data type is the primary result of conditional statements, which allow different actions and change control flow depending on whether a programmer-specified boolean condition evaluates to True or False. In numeric contexts they behave like the integers 1 and 0, respectively. The built-in function bool() can be used to cast any value to a Boolean, if the value can be interpreted as a truth value. They are written as False and True, respectively.

In python boolean is subclass of int, use following example to check this.

```>>> bool.__bases__
(<type 'int'>,)```

An example, the expression “a < b” compares two values a and b, and returns True if a is less than b, False if a is greater than or equal to b.

```>>> a = 5
>>> b = 8
>>> a < b
True
>>> b < a
False
>>> a == b
False```

In python internally, True is represented as 1 and False as 0, and they can be used in numeric expressions as those values. Check following example to evaluate this.

```>>> True + 1
2
>>> True + 45
46
>>> True * 4
4
>>> True / 1
1
>>> False + 45
45
>>> False * 4
0
>>> False / 1
0```

Following table shows what are the True values and Flase values in python.

False True
Any numeric zero: the int value 0, the float value 0.0, the long value 0L, or the complex value 0.0j.   All other values are considered True.
Any empty sequence: the str value '', the unicode value u'', the empty list value [], or the empty tuple value ().
Any empty mapping, such as the empty dict (dictionary) value {}.
The special value None.

With respect to above table, look at the following examples to understand which values are True and False, to check this we will use bool() function of python.

```>>> x = 0       # int Value Numeric Zero
>>> bool(x)
False

>>> y = 0.0     # float Value numeric Zero
>>> bool(y)
False

>>> z = 0L      # long Value Numeric Zero
>>> bool(z)
False

>>> s = ''     # An Empty String
>>> bool(s)
False

>>> u = u''    # An Empty unicode value
>>> bool(u)
False

>>> l = []      # An Empty List
>>> bool(l)
False

>>> t = ()      # An Empty Tuple
>>> bool(t)
False

>>> d = {}      # An Empty mapping, Empty Dictionary
>>> bool(d)
False

>>> n = None    # An special value None
>>> bool(n)
False

>>> a = 23      # An non zero int
>>> bool(a)
True

>>> b = 23.4    # An non zero float value
>>> bool(b)
True

>>> c = 230857363848L   # An long value
>>> bool(c)
True

>>> s = 'Hello'     # An Non Empty String
>>> bool(s)
True

>>> u = u'Hello'    # An Non Empty unicode value
>>> bool(u)
True

>>> l = ['abc', 'xyz']      # An Non Empty List
>>> bool(l)
True

>>> t = ('abc', 'xyz')      # An Non Empty Tuple
>>> bool(t)
True

>>> d = {'abc': 'xyz'}      # An Non Empty mapping, Non Empty Dictionary
>>> bool(d)
True```

## What is None ?

The null keyword is commonly used in many programming languages, such as Java, C++, C# and Javascript. It is a value that is assigned to a variable. The concept of a null keyword is that it gives a variable a neutral, or "null" behavior.

The equivalent of the null keyword in Python is None. None is a special value,  it is a value that indicates no value.

Python is Object oriented programming language and 'None' is an object, to verify this examine the following example;

```>>> x = None
>>> help(x)

Help on NoneType object:

class NoneType(object)
|  Methods defined here:
|
|  __hash__(...)
|      x.__hash__() <==> hash(x)
|
|  __repr__(...)
|      x.__repr__() <==> repr(x)

>>> type(x)
<type 'NoneType'>```

In above example 'None' value is assigned to variable 'x' and when we try get help using builtin help() function the first line shows the "Help on NoneType object". and when we check type of 'x' it shows 'NoneType'. Because 'None' is an object, we cannot use it to check if a variable exists. It is a value/object, not an operator used to check a condition.

There are many scenario where you can use 'None'. For example you want to perform an action that may or may not work. In this case 'None' is one way to check the state of action later.

```>>> z = None
>>> x = "My Account Number is "
>>> y = 2345754

>>> try:
...     z = x + y
... except TypeError:
...     pass
...
>>> if z == None:
...     print "There is an Error"
... else:
...     print "There is no Error"
...
There is an Error```

As you can notice in above example there are three variables x, y, and z. I am trying to concatenate string and integer which throws TypeError as we learned in "Python Print Command (Link to print post)". Initially i created variable z assigned value 'None' which represents no value, 'x' and 'y' variables assigned value string and integer respectively. When i try to concatenate x and y using '+' operator it throws an TypeError in normal scenario but here i asked program to 'pass' except any error and using if condition i am checking value of 'z' if it is 'None' it will print 'There is an Error' else it will print 'There is no Error'.

Note: If you are beginner you might be facing problem to understand the above example because of try except block and if condition, but later in up coming tutorial we will learn both.

The following two tabs change content below.

#### Surendra Anne

Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.