@@ -105,4 +105,138 @@ \section{逻辑运算符}
105105\index {operator!or}
106106\index {operator!not}
107107
108+ 如果{\tt n \% 2 == 0 or n \% 3 == 0}有一个条件语句为真,则表达式的值就为真,亦即n可以被2或3整除。\\
109+
110+ 最后,{\tt not}运算符对一个布尔表达式取反,所以如果{\tt (
111+ x > y)为假,则{\tt not (x > y)}为真,亦即,{\tt x}小于或等于{\tt y}。\\
112+
113+ 严格来说,逻辑运算符的操作数只能是布尔表达式,但是Python
114+ 对此可没什么严格要求。任何不为0的整数也被解释成{\tt True}
115+ \footnote {这个也可扩展到任何其他的类型,比如后面要涉及到的
116+ list,tuple,dict,set还有str。}
117+
118+ \beforeverb
119+ \begin {verbatim }
120+ >>> 17 and True
121+ True
122+ \end {verbatim }
123+ \afterverb
124+
125+ 这个灵活性是很有用处的,但是可能会产生一些微妙的问题。我们
126+ 要尽可能的避免他们(除非知道自己在做什么)。
127+
128+ \section {条件执行 }
129+ \label {conditional execution }
130+
131+ \index {conditional statement 条件语句}
132+ \index {statement!conditional}
133+ \index {if statement if语句}
134+ \index {statement!if}
135+ \index {conditional execution 条件执行}
136+
137+ 考虑到要写一些有用的程序,我们几乎总是需要检查条件,并改变相应的改变程序的行为。条件语句给了我们这个能力。最简单的要属{\tt if}语句了:
138+
139+
140+
141+ \beforeverb
142+ \begin {verbatim }
143+ if x > 0:
144+ print 'x is positive'
145+ \end {verbatim }
146+ \afterverb
147+ {\tt if}语句后面的布尔表达式叫做条件。如果条件为真,则下面
148+ 缩进的语句就被执行。反之,则什么也不发生。\footnote {这是
149+ 针对本例而言,因为本例只有一条语句。在其他的情况下,可能
150+ 会有诸如{\tt else}之类的语句。}
151+
152+ \index {condition 条件}
153+ \index {compound statment}
154+ \index {statment!compound}
155+
156+ {\tt if}语句和函数定义有着相同的结构:
157+ 一个头,后面跟着一个缩进的语句块。这样的语句叫做复合语句。\\
158+
159+ 虽然对复合语句里面可以含有的语句数量不限,但是必须至少有一
160+ 条\footnote {c/c++中没有这样的限制}。偶然地,可能在语句体里
161+ 暂时不需要语句(通常作为一个占位符)。在这种情况下,我们可以
162+ 使用{\tt pass}语句,它什么也不做。
163+
164+
165+
166+ \index {pass statement}
167+ \index {statement!pass}
168+
169+ \beforeverb
170+ \begin {verbatim }
171+ if x < 0:
172+ pass # need to handle negative values!
173+ \end {verbatim }
174+ \afterverb
175+
176+ \section {选择执行 }
177+ \label {alternative execution}
178+
179+ \index {alternative executive 选择执行}
180+ \index {else keyword else关键字}
181+ \index {keyword!else}
182+
183+ {\tt if}语句的第二种形式是选择执行,此时,有两种可能性,
184+ 条件决定了哪一个可能性被执行。语法是这样:
185+
186+ \beforeverb
187+ \begin {verbatim }
188+ if x%2 == 0:
189+ print 'x is even'
190+ else:
191+ print 'x is odd'
192+ \end {verbatim }
193+ \afterverb
194+
195+ 如果{\tt x}除以2的余数是0,我们可以判定{\tt x}是偶数,程序
196+ 就输出这个效果。如果条件为假,第二个语句就被执行。因为条件
197+ 必须为真或假,其中一个必定会被执行。选择项叫做分支,因为
198+ 它们是执行流的分支。
199+
200+ \index {branch 分支}
201+
202+
203+ \section {链式条件 }
204+ \index {chained conditional 链式条件}
205+ \index {conditional!chained}
206+
207+ 有时,可能会有不止两种可能性,我们就需要更多的分支。一种方式是使用链式条件语句。
208+
209+ \beforeverb
210+ \begin {verbatim }
211+ if x < y:
212+ print 'x is less than y'
213+ elif x > y:
214+ print 'x is greater than y'
215+ else:
216+ print 'x and y are equal'
217+ \end {verbatim }
218+ \afterverb
219+
220+ {\tt elif}是"else if"的缩写形式。再次说明一下,只有一条
221+ 语句被执行。{\tt elif}语句的数目也是没有限制的。如果要写
222+ {\tt else}语句,必须是在链式条件的最后,但是如果没有,也是
223+ 允许的。
224+
225+ \index {elif keyword elif关键字}
226+ \index {keyword!elif}
227+
228+ \beforeverb
229+ \begin {verbatim }
230+ if choice == 'a':
231+ draw_a()
232+ elif choice == 'b':
233+ draw_b()
234+ elif choice == 'c':
235+ draw_c()
236+ \end {verbatim }
237+ \afterverb
238+
239+ 每个条件按顺序被检查。如果第一个为假,下一个就被检查,如此
240+ 如此。如果有一个为真,相应的分支就被执行,链式语句也就终止
241+ 。尽管可能有多个条件为真,也只有第一个为真的分支被执行。\\
108242
0 commit comments