truth.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>2.1.1 Truth Value Testing </title>
<META NAME="description" CONTENT="2.1.1 Truth Value Testing ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html">
<LINK REL="previous" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">
<LINK REL="up" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">
<LINK REL="next" href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">2.1 Built-in Types</A>
<b class="navlabel">Up:</b> <a class="sectref" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">2.1 Built-in Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html">2.1.2 Boolean Operations</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2>
<BR>
2.1.1 Truth Value Testing
</H2>
<P>
Any object can be tested for truth value, for use in an <tt class="keyword">if</tt> or
<tt class="keyword">while</tt> condition or as operand of the Boolean operations below.
The following values are considered false:
<P>
<UL>
<LI><code>None</code>
<P>
</LI>
<LI>zero of any numeric type, for example, <code>0</code>, <code>0L</code>,
<code>0.0</code>, <code>0j</code>.
<P>
</LI>
<LI>any empty sequence, for example, <code>''</code>, <code>()</code>, <code>[]</code>.
<P>
</LI>
<LI>any empty mapping, for example, <code>{}</code>.
<P>
</LI>
<LI>instances of user-defined classes, if the class defines a
<tt class="method">__nonzero__()</tt> or <tt class="method">__len__()</tt> method, when that
method returns zero.<A NAME="tex2html2"
HREF="#foot1393"><SUP>2.2</SUP></A>
<P>
</LI>
</UL>
<P>
All other values are considered true -- so objects of many types are
always true.
<P>
Operations and built-in functions that have a Boolean result always
return <code>0</code> for false and <code>1</code> for true, unless otherwise
stated. (Important exception: the Boolean operations
"<tt class="samp">or</tt>" and "<tt class="samp">and</tt>" always return one of
their operands.)
<P>
<BR><HR><H4>Footnotes</H4>
<DL>
<DT><A NAME="foot1393">... zero.</A><A NAME="foot1393"
href="truth.html#tex2html2" tppabs="http://www.python.org/doc/current/lib/truth.html#tex2html2"><SUP>2.2</SUP></A>
<DD>Additional information on these
special methods may be found in the <em class='citetitle'><a
href="javascript:if(confirm('http://www.python.org/doc/current/ref/ref.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.python.org/doc/current/ref/ref.html'" tppabs="http://www.python.org/doc/current/ref/ref.html"
title='Python
Reference Manual'
>Python
Reference Manual</a></em>.
</DL>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">2.1 Built-in Types</A>
<b class="navlabel">Up:</b> <a class="sectref" href="types.html" tppabs="http://www.python.org/doc/current/lib/types.html">2.1 Built-in Types</A>
<b class="navlabel">Next:</b> <a class="sectref" href="boolean.html" tppabs="http://www.python.org/doc/current/lib/boolean.html">2.1.2 Boolean Operations</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -