📄 26.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 Developer's Handbook -> Programs</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="22.html" class="navtitle">2. Language Review</a> > <span class="nonavtitle">Programs</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="25.html" title="The Shell Environment"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=26" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="26.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="27.html" title="Built-In Data Types"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A18%3A34+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162145081058111154037148089</font><a href="read1.asp?bookname=0672319942&snode=26&now=5%2F31%2F2002+4%3A18%3A34+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>
Programs</h3>
<p>Until now, all the examples were written directly in the interpreter environment. However, most Python programs are executed as external scripts, being loaded from files.</p>
<P>You can write your own Python scripts by using any text editor of your choice. Remember to always save your files using the <TT Class="monofont">.py</TT> extension.</P>
<P>As with any other UNIX scripting language, Python scripts (for UNIX) need a special handling.</p>
<p>First, you need to put a "shebang" in the first line of the script. This line declares the location of Python's interpreter in your system. For example</p>
<pRE>
#!/usr/local/bin/python
</PRe>
<p>Note that this example works only if Python was installed under the given mounting point. Most Linux systems have Python installed under <tt class="monofont">/usr</tt> by default, so the preceding example will not work. Today, the following line of code seems to be more common, and does not depend on where Python is installed:</p>
<pre>
#!/usr/bin/env python
</pre>
<p>If you are running your scripts on an MS Windows environment, you can keep this line of code for portability purposes because the literal <tT clAss="monofont">#</tT> is only used to identify comment lines that are ignored by the interpreter, so it will cause no harm to your programs.<a namE="idx1073742054"></a>
<a nAME="idx1073742055"></A>
<a namE="idx1073742056"></A>
</P>
<Div clASS="note"><P clasS="notetitle"><B>Tip</B></P><p>
<p>The "shebang" line is only meaningful when you work on a UNIX system.<a name="idx1073742057"></a>
<a name="idx1073742058"></a>
<a name="idx1073742059"></a>
</P>
</p></dIv>
<br>
<Br>
<p>If you don't know where <a nAme="idx1073742060"></a>
<A NAMe="idx1073742061"></a>
<a nAME="idx1073742062"></A>Python is located on your UNIX system, use the following <a namE="idx1073742063"></A>
<A Name="idx1073742064"></a>command:</P>
<PRE>
$ whereis python
</pre>
<p>Also, remember to <a name="idx1073742065"></a>
<a name="idx1073742066"></a>
<a namE="idx1073742067"></a>set the permissions on your script to <tT claSs="monofont">755</tt> in order to let every user be able to execute it.</p>
<Pre>
$ chmod +x scriptname.py
</pRE>
<P>or</P>
<pre>
$ chmod 755 scriptname.py
</pRE>
<P>As you cannot directly <A name="idx1073742068"></A>
<A NAme="idx1073742069"></a>
<a NAME="idx1073742070"></a>
<a name="idx1073742071"></a>
<a name="idx1073742072"></a>
<a name="idx1073742073"></a>execute Python scripts in the MS Windows systems through the command line, you have two options: Either double-click the file using Windows Explorer or call the interpreter, passing the filename as an argument. For example,</p>
<Pre>
c:\>python scriptname.py
</Pre>
<p>Another way to call the interpreter on Windows systems is by typing <A name="idx1073742074"></A>
<a naME="idx1073742075"></A>
<Tt claSS="monofont">start scriptname.py</TT> at the shell prompt. This command will find and execute the program associated with the extension <tt clASS="monofont">.py.</Tt>
</p>
<p>If you want to <a NAME="idx1073742076"></a>
<a name="idx1073742077"></a>
<a name="idx1073742078"></a>
<a name="idx1073742079"></a>
<a NamE="idx1073742080"></a>
<a nAme="idx1073742081"></a>
<a Name="idx1073742082"></A>open the interpreter after executing a program, use the <A NAme="idx1073742083"></a>
<a NAME="idx1073742084"></a>
<tt cLASS="monofont">-i</tt> argument when calling the script. The interpreter will run your script, and after it executes all the commands, it will open its command-line interface for you. Here's how to call the script with a command-line option:</p>
<pRE>
c:\python -i scriptname.py
</PRe>
<p>Otherwise, after the script finishes its execution, it will automatically close the interpreter.</p>
<p>After spending some time creating Python programs, you might find some <tt class="monofont">.pyc</tt> files in the same directory in which you are saving your <tt class="monofont">.py</Tt> scripts. See <a Href="227.html">Chapter 17, "Development Tools,"
</A> to know more about this other file extension.<a namE="idx1073742085"></a>
<a nAME="idx1073742086"></A>
<a namE="idx1073742087"></A>
</P>
<H4>
Indentation</h4>
<p>Python delimits code blocks by using indentation. There is no concept of <tt CLASs="monofont">{}</tt>s or <tT CLAss="monofont">Begin/End</tt>s as in other languages. When you indent a block of code, you define the way the statements are grouped. It also reduces errors due to bad indentation. For instance, the following C or Perl code looks like a single <tt class="monofont">if</tt> statement, but the second statement is always executed:</p>
<pre>
if (expression)
statement1;
statement2;
</pre>
<P>Python doesn't suffer from this problem because indentation defines block structure.</p>
<p>Another great aspect of this implementation is that you can reduce the size of your code while using indentation instead of conventional block delimiters.</P>
<div Class="note"><P claSS="notetitle"><B>Tip</B></p><p>
<p>Keep in mind that <a NAME="idx1073742088"></a>
<a naME="idx1073742089"></A>tabs are internally converted to spaces (<Tt claSS="monofont">1 tab = 8 spaces</TT>), and <a name="idx1073742090"></a>
<a name="idx1073742091"></a>
<a name="idx1073742092"></a>blank lines are ignored when part of scripts.</p>
</p></Div>
<Br>
<br>
<P>I suggest you write one statement per line, using a <tt clAss="monofont">newline</tT> (<TT Class="monofont">ENTER</TT>) to terminate each line. If you decide to have more than one <A Name="idx1073742093"></a>
<A NAMe="idx1073742094"></a>
<a nAME="idx1073742095"></A>statement in the same line, you need to separate them by using <a name="idx1073742096"></a>
<a name="idx1073742097"></a>semicolons, as shown in the following:<a name="idx1073742098"></a>
<a nAme="idx1073742099"></A>
<a naMe="idx1073742100"></a>
</p>
<pRe>
>>> print "When AH "; print "were young
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -