-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBooleans.html
More file actions
348 lines (329 loc) · 27.5 KB
/
Copy pathBooleans.html
File metadata and controls
348 lines (329 loc) · 27.5 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Boolean Expressions — Programming in Python 7.0 documentation</title>
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!--[if lt IE 9]>
<script src="../_static/js/html5shiv.min.js"></script>
<![endif]-->
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Recursion" href="Recursion.html" />
<link rel="prev" title="3. Booleans and Recursion" href="../topics/03-recursion_booleans/index.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" style="background: #4b2e83" >
<a href="../index.html">
<img src="../_static/UWPCE_logo_full.png" class="logo" alt="Logo"/>
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Boolean Expressions</a></li>
<li class="toctree-l2"><a class="reference internal" href="Recursion.html">Recursion</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/series/fib_and_lucas.html">Fibonacci Series Exercise</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home"></a> »</li>
<li><a href="../topics/03-recursion_booleans/index.html"><span class="section-number">3. </span>Booleans and Recursion</a> »</li>
<li>Boolean Expressions</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Booleans.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="../topics/03-recursion_booleans/index.html" class="btn btn-neutral float-left" title="3. Booleans and Recursion" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="Recursion.html" class="btn btn-neutral float-right" title="Recursion" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="boolean-expressions">
<span id="booleans"></span><h1>Boolean Expressions<a class="headerlink" href="#boolean-expressions" title="Permalink to this headline"></a></h1>
<p>“Boolean” logic is the logic of binary values – things that can be ony one of two values. Usually, the two values are considered to be true or false.</p>
<p>In programming languages, “booleans” are often a data type – one that captures this notion of true and false.</p>
<p>Python has a boolean type as well: the singletons <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>Booleans are used in <code class="docutils literal notranslate"><span class="pre">if</span></code> statements, as well as the boolean operators, <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code>.</p>
<p>But Python is not limited to using the actual boolean type in logic expressions – in the spirit of dynamic languages, virtually any type can have values that are considered True or False.</p>
<p>Some like to refer to this concept by the moniker given by Stephan Colbert: “Truthiness”</p>
<div class="section" id="truthiness">
<h2>Truthiness<a class="headerlink" href="#truthiness" title="Permalink to this headline"></a></h2>
<p>What is true or false in Python?</p>
<ul class="simple">
<li><p>The Booleans: <code class="docutils literal notranslate"><span class="pre">True</span></code> and <code class="docutils literal notranslate"><span class="pre">False</span></code></p></li>
<li><p>“Something or Nothing”; that is the presence or absence of a value.</p></li>
<li><p><a class="reference external" href="http://mail.python.org/pipermail/python-dev/2002-April/022107.html">http://mail.python.org/pipermail/python-dev/2002-April/022107.html</a></p></li>
</ul>
<p>Determining Truthiness: If you want to know if a value is “truthy”, you can use the bool constructor to make a boolean out of it:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nb">bool</span><span class="p">(</span><span class="n">something</span><span class="p">)</span>
</pre></div>
</div>
<p>This is similar to making an integer out of another value, such a float or a string:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="nb">int</span><span class="p">(</span><span class="mf">4.5</span><span class="p">)</span>
<span class="gh">Out[1]: </span><span class="go">4</span>
<span class="gp">In [2]: </span><span class="nb">int</span><span class="p">(</span><span class="s2">"345"</span><span class="p">)</span>
<span class="gh">Out[2]: </span><span class="go">345</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">bool(something)</span></code> will always evaluate to either <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
</div>
<div class="section" id="what-is-falsy">
<h2>What is Falsy?<a class="headerlink" href="#what-is-falsy" title="Permalink to this headline"></a></h2>
<ul>
<li><p><code class="docutils literal notranslate"><span class="pre">None</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">False</span></code></p></li>
<li><p><strong>Nothing:</strong></p>
<blockquote>
<div><ul>
<li><p>The zero value of any numeric type: <code class="docutils literal notranslate"><span class="pre">0,</span> <span class="pre">0.0,</span> <span class="pre">0j</span></code>.</p></li>
<li><p>Any empty sequence, for example, <code class="docutils literal notranslate"><span class="pre">"",</span> <span class="pre">(),</span> <span class="pre">[]</span></code>.</p></li>
<li><p>Any empty mapping, for example, <code class="docutils literal notranslate"><span class="pre">dict()</span></code>.</p></li>
<li><p>Instances of user-defined classes:</p>
<blockquote>
<div><ul class="simple">
<li><p>for which <code class="docutils literal notranslate"><span class="pre">__bool__()</span></code> returns False</p></li>
<li><p>for which <code class="docutils literal notranslate"><span class="pre">__len__()</span></code> returns 0</p></li>
</ul>
</div></blockquote>
</li>
</ul>
</div></blockquote>
</li>
<li><p><a class="reference external" href="http://docs.python.org/library/stdtypes.html">http://docs.python.org/library/stdtypes.html</a></p></li>
</ul>
<p>(Don’t worry about that last one – what that means is that user-defined types can control their truthiness behavior).</p>
</div>
<div class="section" id="what-is-truthy">
<h2>What is Truthy?<a class="headerlink" href="#what-is-truthy" title="Permalink to this headline"></a></h2>
<p>Everything else.</p>
</div>
<div class="section" id="pythonic-booleans">
<h2>Pythonic Booleans<a class="headerlink" href="#pythonic-booleans" title="Permalink to this headline"></a></h2>
<p>Any object in Python, when passed to the <code class="docutils literal notranslate"><span class="pre">bool()</span></code> type object, will
evaluate to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p>When you use the <code class="docutils literal notranslate"><span class="pre">if</span></code> keyword, it automatically does this to the expression provided.</p>
<p>Which means that this is redundant, and not Pythonic:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">xx</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
<span class="n">do_something</span><span class="p">()</span>
<span class="c1"># or even worse:</span>
<span class="k">if</span> <span class="nb">bool</span><span class="p">(</span><span class="n">xx</span><span class="p">)</span> <span class="o">==</span> <span class="kc">True</span><span class="p">:</span>
<span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
<p>Instead, use what Python gives you:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">xx</span><span class="p">:</span>
<span class="n">do_something</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="and-or-and-not">
<h2><code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span></code><a class="headerlink" href="#and-or-and-not" title="Permalink to this headline"></a></h2>
<p>Python has three boolean operators: <code class="docutils literal notranslate"><span class="pre">and</span></code>, <code class="docutils literal notranslate"><span class="pre">or</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code> are binary expressions, and evaluate from left to right.</p>
<p><code class="docutils literal notranslate"><span class="pre">and</span></code> will return the first operand that evaluates to False, or the last
operand if none are True:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [35]: </span><span class="mi">0</span> <span class="ow">and</span> <span class="mi">456</span>
<span class="gh">Out[35]: </span><span class="go">0</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">or</span></code> will return the first operand that evaluates to True, or the last
operand if none are True:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [36]: </span><span class="mi">0</span> <span class="ow">or</span> <span class="mi">456</span>
<span class="gh">Out[36]: </span><span class="go">456</span>
</pre></div>
</div>
<p>On the other hand, <code class="docutils literal notranslate"><span class="pre">not</span></code> is a unary expression (takes one operand) and inverts the boolean value
of this operand:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="ow">not</span> <span class="kc">True</span>
<span class="gh">Out[39]: </span><span class="go">False</span>
<span class="gp">In [40]: </span><span class="ow">not</span> <span class="kc">False</span>
<span class="gh">Out[40]: </span><span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="shortcutting">
<h2>Shortcutting<a class="headerlink" href="#shortcutting" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code> returning teh first value that determines the result is known as “shortcutting”. If you think about it, what <code class="docutils literal notranslate"><span class="pre">and</span></code> and <code class="docutils literal notranslate"><span class="pre">or</span></code> are doing is as little work as possible. They will only evaluate as much as they need to get the answer.</p>
<p>Think about <code class="docutils literal notranslate"><span class="pre">and</span></code>: it is testing if <em>both</em> the operands are True. If the first one is False, there is no need to bother checking the second.</p>
<p>Alternatively, <code class="docutils literal notranslate"><span class="pre">or</span></code> is trying to see if only one of the operands is True. So if the first one is True, it can stop, and does not need to evaluate the second.</p>
<p>Also key is that if an operation is “shortcut” – the second part of the expression will not be evaluated – so it could be an invalid expression that will never raise an error:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [3]: </span><span class="mi">34</span> <span class="ow">or</span> <span class="p">(</span><span class="mi">10</span><span class="o">/</span><span class="mi">0</span><span class="p">)</span>
<span class="gh">Out[3]: </span><span class="go">34</span>
</pre></div>
</div>
<p>Since the expression was known to be true after the first value was checked (a number that is nonzero), the second was never evaluated.</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="mi">34</span> <span class="ow">and</span> <span class="p">(</span><span class="mi">10</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">ZeroDivisionError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="nn"><ipython-input-4-cef0e50bb96d></span> in <span class="ni"><module></span><span class="nt">()</span>
<span class="ne">----> </span><span class="mi">1</span> <span class="mi">34</span> <span class="ow">and</span> <span class="p">(</span><span class="mi">10</span> <span class="o">/</span> <span class="mi">0</span><span class="p">)</span>
<span class="ne">ZeroDivisionError</span>: division by zero
</pre></div>
</div>
<p>In this case, the second expression needs to be evaluated – so it DID raise an error.</p>
<p>This can be exploited to provide compact logic – but it can also hide bugs!</p>
<p>Because of the return value of the boolean operators, you can write concise
statements, rather than a full <code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">--</span> <span class="pre">else</span></code> block like so:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="k">if</span> <span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
<span class="n">x</span> <span class="ow">or</span> <span class="n">y</span> <span class="k">return</span> <span class="n">y</span>
<span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">x</span>
<span class="k">if</span> <span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
<span class="n">x</span> <span class="ow">and</span> <span class="n">y</span> <span class="k">return</span> <span class="n">x</span>
<span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">y</span>
<span class="k">if</span> <span class="nb">bool</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
<span class="ow">not</span> <span class="n">x</span> <span class="k">return</span> <span class="kc">True</span>
<span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="kc">False</span>
</pre></div>
</div>
</div>
<div class="section" id="chaining">
<h2>Chaining<a class="headerlink" href="#chaining" title="Permalink to this headline"></a></h2>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="ow">or</span> <span class="n">b</span> <span class="ow">or</span> <span class="n">c</span> <span class="ow">or</span> <span class="n">d</span>
<span class="n">a</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">and</span> <span class="n">c</span> <span class="ow">and</span> <span class="n">d</span>
</pre></div>
</div>
<p>The first value that defines the result is returned</p>
<blockquote>
<div><p>(take a moment to experiment…)</p>
</div></blockquote>
</div>
<div class="section" id="conditional-expressions">
<h2>Conditional Expressions<a class="headerlink" href="#conditional-expressions" title="Permalink to this headline"></a></h2>
<p>This is a fairly common idiom:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">something</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">a_value</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">another_value</span>
</pre></div>
</div>
<p>In other languages, this can be compressed with a “ternary operator”:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>result = a > b ? x : y;
</pre></div>
</div>
<p>(this is the syntax from the C family of languages)</p>
<p>In Python, the same is accomplished with the conditional expression:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="mi">5</span> <span class="k">if</span> <span class="n">x</span> <span class="o">></span> <span class="mi">2</span> <span class="k">else</span> <span class="mi">3</span>
</pre></div>
</div>
<p>It’s pretty self explanatory</p>
<p>PEP 308:
(<a class="reference external" href="http://www.python.org/dev/peps/pep-0308/">http://www.python.org/dev/peps/pep-0308/</a>)</p>
</div>
<div class="section" id="boolean-return-values">
<h2>Boolean Return Values<a class="headerlink" href="#boolean-return-values" title="Permalink to this headline"></a></h2>
<p>Remember this puzzle from the CodingBat exercises?</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">sleep_in</span><span class="p">(</span><span class="n">weekday</span><span class="p">,</span> <span class="n">vacation</span><span class="p">):</span>
<span class="k">if</span> <span class="n">weekday</span> <span class="o">==</span> <span class="kc">True</span> <span class="ow">and</span> <span class="n">vacation</span> <span class="o">==</span> <span class="kc">False</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">True</span>
</pre></div>
</div>
<p>Though correct, that’s not a particularly Pythonic way of solving the problem.</p>
<p>Here’s a better solution:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">sleep_in</span><span class="p">(</span><span class="n">weekday</span><span class="p">,</span> <span class="n">vacation</span><span class="p">):</span>
<span class="k">return</span> <span class="ow">not</span> <span class="p">(</span><span class="n">weekday</span> <span class="o">==</span> <span class="kc">True</span> <span class="ow">and</span> <span class="n">vacation</span> <span class="o">==</span> <span class="kc">False</span><span class="p">)</span>
</pre></div>
</div>
<p>And here’s an even better one:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">sleep_in</span><span class="p">(</span><span class="n">weekday</span><span class="p">,</span> <span class="n">vacation</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="ow">not</span> <span class="n">weekday</span><span class="p">)</span> <span class="ow">or</span> <span class="n">vacation</span>
</pre></div>
</div>
</div>
<div class="section" id="bools-are-integers">
<h2>bools are integers?<a class="headerlink" href="#bools-are-integers" title="Permalink to this headline"></a></h2>
<p>In Python, the boolean types are subclasses of integer:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="kc">True</span> <span class="o">==</span> <span class="mi">1</span>
<span class="gh">Out[1]: </span><span class="go">True</span>
<span class="gp">In [2]: </span><span class="kc">False</span> <span class="o">==</span> <span class="mi">0</span>
<span class="gh">Out[2]: </span><span class="go">True</span>
</pre></div>
</div>
<p>And you can even do math with them (though it’s a bit odd to do so):</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="mi">3</span> <span class="o">+</span> <span class="kc">True</span>
<span class="gh">Out[6]: </span><span class="go">4</span>
</pre></div>
</div>
<p>This is left over from history – in early versions of Python, there were no boolean types – folks used integers, with zero as false. And this is true of other languages as well, like classic C. To keep backward compatibility and allow some nifty tricks to still work, bools are subclassed from integers.</p>
<p>It’s good to know this if you read others’ code, but I do NOT recommend you use this feature!</p>
</div>
<div class="section" id="try-it-out">
<h2>Try it out:<a class="headerlink" href="#try-it-out" title="Permalink to this headline"></a></h2>
<p>Now that you know a bit more about Python boolean operations, it’s a good time to visit some coding bat exercises and see if you can make your solutions cleaner and more compact.</p>
</div>
</div>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="../topics/03-recursion_booleans/index.html" class="btn btn-neutral float-left" title="3. Booleans and Recursion" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="Recursion.html" class="btn btn-neutral float-right" title="Recursion" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2020, University of Washington, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>