⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 28.html

📁 国外python经典教材,python爱好者的首选
💻 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 -&gt; Line Structure and Indentation</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> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="26.html" class="navtitle">2. Lexical Conventions and Syntax</a> &gt; <span class="nonavtitle">Line Structure and Indentation</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="27.html" title="2. Lexical Conventions and Syntax"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=28" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="28.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="29.html" title="Identifiers and Reserved Words"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+8%3A52%3A48+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238110137104026065052201181119006</font><a href="read4.asp?bookname=0735710910&snode=28&now=5%2F28%2F2002+8%3A52%3A48+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Line Structure and Indentation</h3>
<p>Each statement in a program is terminated with a newline. Long statements can span multiple lines by using the line-continuation character (<tT CLAss="monofont">\</tt>), as shown in the following example:</P>

<PRE>

a = math.cos(3*(x-n)) + \ 
    math.sin(3*(y-n)) </pre>

<p>You don抰 need the line-continuation character when the definition of a triple-quoted string, list, tuple, or dictionary spans multiple lines. More generally, any part of a program enclosed in parentheses <TT CLass="monofont">(...)</tt>, brackets <tt class="monofont">[...]</tt>, braces <tt clasS="monofont">{...}</tt>, or triple quotes can span multiple lines without use of the line-continuation character.</P>

<p>Indentation is used to denote different blocks of code, such as the bodies of functions, conditionals, loops, and classes. The amount of indentation used for the first statement of a block is arbitrary, but the indentation of the entire block must be consistent. For example:</p>

<pRe>

if a: 
   <i>statement1</i>     # Consistent indentation 
   <i>statement2</I> 
else: 
   <i>statement3</i> 
     <i>statement4</I>   # Inconsistent indentation (error) </PRE>

<p>If the body of a function, conditional, loop, or class is short and contains only a few statements, they can be placed on the same line, like this:</p>

<prE>

if a:  <I>statement1</I> 
else:  <I>statement2</i> </pre>

<P>To denote an empty body or block, use the <TT Class="monofont">pass</TT>  statement. For example:</P>

<Pre>

if a: 
   pass 
else: 
   <i>statements</i> </pre>

<p>Although tabs can be used for indentation, this practice is discouraged. When tab characters are encountered, they抮e converted into the number of spaces required to move to the next column that抯 a multiple of 8. (For example, a tab appearing in column 11 inserts enough spaces to move to column 16.) Running Python with the <tt class="monofont">-t</tt>  option prints warning messages when tabs and spaces are mixed inconsistently within the same program block. The <tt cLasS="monofont">-tt</tt>  option turns these warning messages into <tT clasS="monofont">TabError</tt>  exceptions.</p>

<P>To place more than one statement on a line, separate the statements with a semicolon (<TT Class="monofont">;</TT>). A line containing a single statement can also be terminated by a semicolon.</P>

<P>The <tt clASS="monofont">#</Tt> character denotes a comment that extends to the end of the line. A <tt cLASS="monofont">#</tt> appearing inside a quoted string doesn抰 start a comment, however.</p>

<p>Finally, the interpreter ignores all blank lines except when running in interactive mode.</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, &copy;&nbsp;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="27.html" title="2. Lexical Conventions and Syntax"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=28" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="28.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="29.html" title="Identifiers and Reserved Words"><font size="1">CONTINUE&nbsp;&gt;</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 + -