-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython 101 - Boolean Operations and None - by Mike Driscoll.txt
132 lines (92 loc) · 3.78 KB
/
Python 101 - Boolean Operations and None - by Mike Driscoll.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

The Python Papers
Subscribe
Sign in
Python 101 - Boolean Operations and None

MIKE DRISCOLL
AUG 10, 2023
4
Share
You will find that you often need to know if something is True or False. For example, you might want to know if someone is old enough to create a bank account. If they are, that is usually represented as True. These values are known as Booleans or bool for short.
In Python, False maps to 0 (zero) and True maps to 1 (one).
You can easily see this is true using Python's interpreter:
>>> True == 1
True
>>> False == 0
True
>>> False == True
False
When you want to compare two values in Python, you need to use == instead of a single =. A single = is known as the assignment operator. It assigns the value on the right to the variable on the left.
Let's try to assign a value to True and see what happens:
>>> True = 1
Traceback (most recent call last):
Python Shell, prompt 4, line 1
Syntax Error: can't assign to keyword: <string>, line 1, pos 0
Python doesn't allow that!
You can't assign anything to keywords in Python.
The bool() Function
Python also provides the bool() function, which allows you to cast other types to True or False.
Let's give it a try:
>>> bool('1')
True
>>> bool('2')
True
>>> bool('0')
True
Anything greater than zero should be cast as True. But wait, that third one is a string with a zero in it and it returned True as well! What's going on here?
Python has the concept of "truthy" and "falsey". What that means is that when you are dealing with non-Numeric types, True will map to sequences with one or more items and False will map to sequences with zero items.
In this case, the string, '0', has one character, so it maps to True. Let's try it with an empty string:
>>> bool('')
False
Since the string is empty (i.e. it has no characters in it), it will cast to False.
Let's see what happens when we try casting some of Python's other types:
>>> bool([])
False
>>> bool(['something'])
True
>>> bool({})
False
>>> bool({1: 'one'})
True
>>> bool(12)
True
Here you try casting an empty list, a list with one item, an empty dictionary, a dictionary with one key/value pair and an integer. Empty lists and dictionaries map to False, while lists and dictionaries with one or more items map to True. Integers or floats that are 0 or 0.0 will map to False, while any other value will map to True.
What About None?
Python also has the concept of None, which is Python's null value. None is a keyword in Python and its data type is NoneType. None is not the same as 0, False or an empty string. In fact, comparing None to anything other than itself will return False:
>>> None == 1
False
>>> None == []
False
>>> None == ''
False
>>> None == None
True
You can assign None to a variable. Note that all instances of None point to the same object, though:
>>> x = None
>>> y = None
>>> x
>>> id(x)
4478513256
>>> id(y)
4478513256
When you want to check if a variable is None, you should use Python's is operator. The reason for that is that is will check the variable's identity and verify that it is None.
Wrapping Up
The bool or Boolean type is important in programming as it is a very simple way to test if something is True or False. You also learned a little about Python's None type, which is similar to null in other languages. You will be using the Boolean values True and False, as well as None, often when you are programming in Python.
The Python Papers is a reader-supported publication. To receive new posts and support my work, consider becoming a free or paid subscriber.
Subscribe
4
Share
Comments

Top
New
Community
No posts
Ready for more?
Subscribe
© 2023 Mike Driscoll
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing
This site requires JavaScript to run correctly. Please turn on JavaScript or unblock scripts