£199.99 £139.99 for one year of Unlimited learning. Offer ends on 28 February 2023 at 23:59 (UTC). T&Cs apply

# 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.
3.3
Bolean logic is a form of mathematics developed by George Boole in the 19th century. But its most important application came in the 20th century when digital computers were invented. We use Boolean logic to design the circuitry of computer hardware and when writing software to control that hardware. Fundamentally, Boolean logic is a way of checking if a statement or value is true or false. Boolean logic uses operators to express the relationship between values and variables. You might already know some of these from general mathematics. For example, 11 is greater than nine is a true statement, but pink equals blue is a false statement. Boolean logic also has its own special operators.
50.1
Let’s look at two of them: AND and OR. AND means that all elements in the statement must be true for the overall outcome to be true; and even if one element is false, the entire statement is false.
64.9
For example: imagine if you go to a car dealership to buy a new car. The dealer tells you, “We only sell new cars here.” But if you check the cars in stock and discover one car is used, the dealer statement is false. This car and this car and that car need to be new for this statement to be true. In contrast, OR means that at least one element of the statement must be true for the entire statement to be true. You can have a mix of true and false elements and have an overall true statement.
98.2
For example: the car dealer tells you we have blue options in stock. If you check and you find at least one blue car, the dealer statement is true. Either this car or this car or that car need to be blue for this statement to be true. Boolean logic is a powerful tool with many applications. Try it for yourself.

## 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
...


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