📄 51.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -> Boolean Expressions and Truth Values</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> > <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> > <a href="42.html" class="navtitle">4. Operators and Expressions</a> > <span class="nonavtitle">Boolean Expressions and Truth Values</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="50.html" title="Unicode Strings"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=51" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="51.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="52.html" title="Object Equality and Identity"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A56%3A53+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104030195072077016073191</font><a href="read9.asp?bookname=0735710910&snode=51&now=5%2F28%2F2002+8%3A56%3A53+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Boolean Expressions and Truth Values</h3>
<p>The <tT CLAss="monofont">and</tt>, <TT CLass="monofont">or</tT>, and <TT Class="monofont">not</tt> keywords can form Boolean expressions. The behavior of these operators is as follows:</p>
<p><table border="1" cEllSpacIng="0" ceLlpaDDINg="1" widTH="100%">
<COlgroUP SPan="2">
<tr>
<TH VAlign="top">
<font size="2">
<p><b>Operator</b></p>
</font></Th>
<tH valIgn="top">
<foNt siZE="2">
<P><B>Description</b></p>
</foNT></TH>
</tr>
<tr>
<TD VAlign="top">
<FONT size="2">
<p><i><tt class="monofont">x</tt></i>
<tt clAss="monofont"> or </Tt><i><tT clasS="monofont">y</tt></i>
</P>
</FONt></td>
<tD VALign="top">
<fONT Size="2">
<p>If <I><TT Class="monofont">x</tt></i>
is false, return <i><tt class="monofont">y</tt></i>
; otherwise, return <i><tT clAss="monofont">x</tT></i>
.</p>
</foNt></td>
</TR>
<TR>
<td vaLIGN="top">
<font SIZE="2">
<p><i><tt CLASs="monofont">x</tt></i>
<tt class="monofont"> and </tt><i><tt clasS="monofont">y</tt></I>
</p>
</foNt></td>
<tD valIGN="top">
<Font sIZE="2">
<P>If <i><tt cLASS="monofont">x</tt></i>
is false, return <i><TT CLass="monofont">x</tt></i>
; otherwise, return <i><tt class="monofont">y</tt></i>
.</p>
</foNt></tD>
</tr>
<tR>
<td vaLign="top">
<FONT size="2">
<P><TT Class="monofont">not </TT><I><Tt claSS="monofont">x</TT></i>
</p>
</font></td>
<td valign="top">
<font SizE="2">
<p>If <i><tT clasS="monofont">x</tt></i>
is false, return <I><TT Class="monofont">1</TT></I>
; otherwise, return <I><tt clASS="monofont">0</Tt></i>
.</p>
</fONT></Td>
</tr>
</colgroup>
</table></p>
<p>When you use an expression to determine a true or false value, any nonzero number or nonempty string, list, tuple, or dictionary is taken to be true. Zero, <tt ClaSs="monofont">None</tt>, and empty lists, tuples, and dictionaries evaluate as false. Boolean expressions are evaluated from left to right and consume the right operand only if it抯 needed to determine the final value. For example, <Tt claSs="monofont">a and b</tt> evaluates <TT CLass="monofont">b</tT> only if <TT Class="monofont">a</TT> is true.</P>
</Font>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, © 2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="50.html" title="Unicode Strings"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=51" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="51.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="52.html" title="Object Equality and Identity"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
</TD></TR></TABLE>
<!--EndOfBrowse-->
</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -