Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="lambdas.html" /> | |
13 | <link rel="prev" href="comparisons.html" /> | |
14 | <link rel="parent" href="expressions.html" /> | |
15 | <link rel="next" href="lambdas.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.10 Boolean operations</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.9 Comparisons" | |
25 | href="comparisons.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5. Expressions" | |
28 | href="expressions.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.11 Lambdas" | |
31 | href="lambdas.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="comparisons.html">5.9 Comparisons</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="lambdas.html">5.11 Lambdas</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0071000000000000000000"></A><A NAME="Booleans"></A> | |
56 | <BR> | |
57 | 5.10 Boolean operations | |
58 | </H1> | |
59 | <a id='l2h-439' xml:id='l2h-439'></a> | |
60 | <P> | |
61 | Boolean operations have the lowest priority of all Python operations: | |
62 | ||
63 | <P> | |
64 | <dl><dd class="grammar"> | |
65 | <div class="productions"> | |
66 | <table> | |
67 | <tr> | |
68 | <td><a id='tok-expression' xml:id='tok-expression'>expression</a></td> | |
69 | <td>::=</td> | |
70 | <td><a class='grammartoken' href="Booleans.html#tok-or_test">or_test</a> | <a class='grammartoken' href="lambdas.html#tok-lambda_form">lambda_form</a></td></tr> | |
71 | <tr> | |
72 | <td><a id='tok-or_test' xml:id='tok-or_test'>or_test</a></td> | |
73 | <td>::=</td> | |
74 | <td><a class='grammartoken' href="Booleans.html#tok-and_test">and_test</a> | <a class='grammartoken' href="Booleans.html#tok-or_test">or_test</a> "or" <a class='grammartoken' href="Booleans.html#tok-and_test">and_test</a></td></tr> | |
75 | <tr> | |
76 | <td><a id='tok-and_test' xml:id='tok-and_test'>and_test</a></td> | |
77 | <td>::=</td> | |
78 | <td><a class='grammartoken' href="Booleans.html#tok-not_test">not_test</a> | <a class='grammartoken' href="Booleans.html#tok-and_test">and_test</a> "and" <a class='grammartoken' href="Booleans.html#tok-not_test">not_test</a></td></tr> | |
79 | <tr> | |
80 | <td><a id='tok-not_test' xml:id='tok-not_test'>not_test</a></td> | |
81 | <td>::=</td> | |
82 | <td><a class='grammartoken' href="comparisons.html#tok-comparison">comparison</a> | "not" <a class='grammartoken' href="Booleans.html#tok-not_test">not_test</a></td></tr> | |
83 | </table> | |
84 | </div> | |
85 | <a class="grammar-footer" | |
86 | href="grammar.txt" type="text/plain" | |
87 | >Download entire grammar as text.</a> | |
88 | </dd></dl> | |
89 | ||
90 | <P> | |
91 | In the context of Boolean operations, and also when expressions are | |
92 | used by control flow statements, the following values are interpreted | |
93 | as false: <code>False</code>, <code>None</code>, numeric zero of all types, and empty | |
94 | strings and containers (including strings, tuples, lists, dictionaries, | |
95 | sets and frozensets). All other values are interpreted as true. | |
96 | ||
97 | <P> | |
98 | The operator <tt class="keyword">not</tt> yields <code>True</code> if its argument is false, | |
99 | <code>False</code> otherwise. | |
100 | <a id='l2h-440' xml:id='l2h-440'></a> | |
101 | <P> | |
102 | The expression <code><var>x</var> and <var>y</var></code> first evaluates <var>x</var>; if | |
103 | <var>x</var> is false, its value is returned; otherwise, <var>y</var> is | |
104 | evaluated and the resulting value is returned. | |
105 | <a id='l2h-441' xml:id='l2h-441'></a> | |
106 | <P> | |
107 | The expression <code><var>x</var> or <var>y</var></code> first evaluates <var>x</var>; if | |
108 | <var>x</var> is true, its value is returned; otherwise, <var>y</var> is | |
109 | evaluated and the resulting value is returned. | |
110 | <a id='l2h-442' xml:id='l2h-442'></a> | |
111 | <P> | |
112 | (Note that neither <tt class="keyword">and</tt> nor <tt class="keyword">or</tt> restrict the value | |
113 | and type they return to <code>False</code> and <code>True</code>, but rather return the | |
114 | last evaluated argument. | |
115 | This is sometimes useful, e.g., if <code>s</code> is a string that should be | |
116 | replaced by a default value if it is empty, the expression | |
117 | <code>s or 'foo'</code> yields the desired value. Because <tt class="keyword">not</tt> has to | |
118 | invent a value anyway, it does not bother to return a value of the | |
119 | same type as its argument, so e.g., <code>not 'foo'</code> yields <code>False</code>, | |
120 | not <code>''</code>.) | |
121 | ||
122 | <P> | |
123 | ||
124 | <DIV CLASS="navigation"> | |
125 | <div class='online-navigation'> | |
126 | <p></p><hr /> | |
127 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
128 | <tr> | |
129 | <td class='online-navigation'><a rel="prev" title="5.9 Comparisons" | |
130 | href="comparisons.html"><img src='../icons/previous.png' | |
131 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
132 | <td class='online-navigation'><a rel="parent" title="5. Expressions" | |
133 | href="expressions.html"><img src='../icons/up.png' | |
134 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
135 | <td class='online-navigation'><a rel="next" title="5.11 Lambdas" | |
136 | href="lambdas.html"><img src='../icons/next.png' | |
137 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
138 | <td align="center" width="100%">Python Reference Manual</td> | |
139 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
140 | href="contents.html"><img src='../icons/contents.png' | |
141 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
142 | <td class='online-navigation'><img src='../icons/blank.png' | |
143 | border='0' height='32' alt='' width='32' /></td> | |
144 | <td class='online-navigation'><a rel="index" title="Index" | |
145 | href="genindex.html"><img src='../icons/index.png' | |
146 | border='0' height='32' alt='Index' width='32' /></A></td> | |
147 | </tr></table> | |
148 | <div class='online-navigation'> | |
149 | <b class="navlabel">Previous:</b> | |
150 | <a class="sectref" rel="prev" href="comparisons.html">5.9 Comparisons</A> | |
151 | <b class="navlabel">Up:</b> | |
152 | <a class="sectref" rel="parent" href="expressions.html">5. Expressions</A> | |
153 | <b class="navlabel">Next:</b> | |
154 | <a class="sectref" rel="next" href="lambdas.html">5.11 Lambdas</A> | |
155 | </div> | |
156 | </div> | |
157 | <hr /> | |
158 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
159 | </DIV> | |
160 | <!--End of Navigation Panel--> | |
161 | <ADDRESS> | |
162 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
163 | </ADDRESS> | |
164 | </BODY> | |
165 | </HTML> |