Skip main navigation

Boolean and None in Python

Python has several basic numeric, string and Boolean types built into it. Objects of the Boolean data type may have one of two values: True or False. The most common (and simplest) example is a filter where all the records that meet a condition (true) are returned, like when using Excel and you filter for a specific value in a field. Watch the video to get an overview of the Boolean logic.

Booleans

Python has several basic numeric, string and Boolean types built into it. Objects of the Boolean data type may have one of two values: True or False. The most common (and simplest) example is a filter where all the records that meet a condition (true) are returned, like when using Excel and you filter for a specific value in a field. Watch the video to get an overview of the Boolean logic.

Now, let’s look at how the Boolean logic takes form in Python.

For example:

Code:

is_true = True
is_false= False
print(is_true, is_false)

 

Output:
True False

 

 

    • Boolean operators are combined with the and and or keywords.

 

 

For example:

 

Code:

 

is_true and is_false

 

Output:
False

 

Code:

 

is_true or is_false

 

Output:
True

 

 

    • Comparisons and other conditional expressions evaluate to either True or False.

 

 

For example:

 

Code:

 

1>2

 

Output:
False

 

Code:

 

2>1

 

Output:
True

 

 

    • Most objects in Python have a notion of True or False.

 

 

For example, empty sequences (list, dict, tuples) are treated as False if used in the control flow expressions. We can also see exactly what boolean value an object relates to by calling a method bool and passing that object parameter to this method.

 

For example:

 

Code:

 

bool([ ]), bool([1,2,3])

 

Output:
(False, True)

 

Code:

 

bool('Some String'), bool('')

 

Output:
(True, False)

 

Code:

 

bool(0), bool(1)

 

Output:
(False, True)

 

None

 

None is a Python’s null value type. Null value is used in various scenarios:

 

 

    • If a function doesn’t explicitly return a value, it implicitly returns the null value.

       

 

    • It is used in conditional expressions where we need to check if the object exists or not, or has a value or not.

       

 

 

For example:

 

Code:

 

a = None
b = 5
a is None

 

Output:
True

 

Code:

 

b is not None

 

Output:
True

 

 

    • It is also a common default value for optional functional arguments. Default values are assigned by Python to indicate that the function will take the default value if no value is passed during function call.

 

 

For example:

 

Code:

 

def add_and_optional_multiple(a,b,c=None):
... result = a+b
... if c is not None:
result = result *c
... return result
...
add_and_optional_multiple(2,3)

Output: 5 add_and_optional_multiple(2,3,5)

Output: 25

Datetime

Datetime functionality is available in Python as a module. The built-in Python datetime provides date and time types.

Resources

If you want to stretch your boundaries and use the extended list of all the data types, check out the official Python documentation that provides all the information on the Python data model.

Access: Python Data Model [1]


References

1. Python keywords [Internet]. Python; [date unknown]. Available from: https://docs.python.org/2.5/ref/keywords.html

This article is from the free online

Data Analytics and Python Fundamentals

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now