| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="tut.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="tut.html" title='Python Tutorial' /> |
| 8 | <link rel='contents' href='node2.html' title="Contents" /> |
| 9 | <link rel='index' href='node19.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node4.html" /> |
| 13 | <link rel="prev" href="node2.html" /> |
| 14 | <link rel="parent" href="tut.html" /> |
| 15 | <link rel="next" href="node4.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>1. Whetting Your Appetite </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="Contents" |
| 25 | href="node2.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="Python Tutorial" |
| 28 | href="tut.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="2. Using the Python" |
| 31 | href="node4.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Tutorial</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="node2.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="node19.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="node2.html">Contents</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node4.html">2. Using the Python</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H1><A NAME="SECTION003000000000000000000"></A><A NAME="intro"></A> |
| 56 | <BR> |
| 57 | 1. Whetting Your Appetite |
| 58 | </H1> |
| 59 | |
| 60 | <P> |
| 61 | If you ever wrote a large shell script, you probably know this |
| 62 | feeling: you'd love to add yet another feature, but it's already so |
| 63 | slow, and so big, and so complicated; or the feature involves a system |
| 64 | call or other function that is only accessible from C ...Usually |
| 65 | the problem at hand isn't serious enough to warrant rewriting the |
| 66 | script in C; perhaps the problem requires variable-length strings or |
| 67 | other data types (like sorted lists of file names) that are easy in |
| 68 | the shell but lots of work to implement in C, or perhaps you're not |
| 69 | sufficiently familiar with C. |
| 70 | |
| 71 | <P> |
| 72 | Another situation: perhaps you have to work with several C libraries, |
| 73 | and the usual C write/compile/test/re-compile cycle is too slow. You |
| 74 | need to develop software more quickly. Possibly you've |
| 75 | written a program that could use an extension language, and you don't |
| 76 | want to design a language, write and debug an interpreter for it, then |
| 77 | tie it into your application. |
| 78 | |
| 79 | <P> |
| 80 | In such cases, Python may be just the language for you. Python is |
| 81 | simple to use, but it is a real programming language, offering much |
| 82 | more structure and support for large programs than the shell has. On |
| 83 | the other hand, it also offers much more error checking than C, and, |
| 84 | being a <em>very-high-level language</em>, it has high-level data types |
| 85 | built in, such as flexible arrays and dictionaries that would cost you |
| 86 | days to implement efficiently in C. Because of its more general data |
| 87 | types Python is applicable to a much larger problem domain than |
| 88 | <em>Awk</em> or even <em>Perl</em>, yet many things are at least as easy |
| 89 | in Python as in those languages. |
| 90 | |
| 91 | <P> |
| 92 | Python allows you to split your program in modules that can be |
| 93 | reused in other Python programs. It comes with a large collection of |
| 94 | standard modules that you can use as the basis of your programs -- or |
| 95 | as examples to start learning to program in Python. Some of these |
| 96 | modules provide things like file I/O, system calls, |
| 97 | sockets, and even interfaces to graphical user interface toolkits like Tk. |
| 98 | |
| 99 | <P> |
| 100 | Python is an interpreted language, which can save you considerable time |
| 101 | during program development because no compilation and linking is |
| 102 | necessary. The interpreter can be used interactively, which makes it |
| 103 | easy to experiment with features of the language, to write throw-away |
| 104 | programs, or to test functions during bottom-up program development. |
| 105 | It is also a handy desk calculator. |
| 106 | |
| 107 | <P> |
| 108 | Python enables programs to written compactly and readably. Programs |
| 109 | written in Python are typically much shorter than equivalent C or |
| 110 | C++ programs, for several reasons: |
| 111 | |
| 112 | <UL> |
| 113 | <LI>the high-level data types allow you to express complex operations in a |
| 114 | single statement; |
| 115 | </LI> |
| 116 | <LI>statement grouping is done by indentation instead of beginning and ending |
| 117 | brackets; |
| 118 | </LI> |
| 119 | <LI>no variable or argument declarations are necessary. |
| 120 | </LI> |
| 121 | </UL> |
| 122 | |
| 123 | <P> |
| 124 | Python is <em>extensible</em>: if you know how to program in C it is easy |
| 125 | to add a new built-in function or module to the interpreter, either to |
| 126 | perform critical operations at maximum speed, or to link Python |
| 127 | programs to libraries that may only be available in binary form (such |
| 128 | as a vendor-specific graphics library). Once you are really hooked, |
| 129 | you can link the Python interpreter into an application written in C |
| 130 | and use it as an extension or command language for that application. |
| 131 | |
| 132 | <P> |
| 133 | By the way, the language is named after the BBC show ``Monty Python's |
| 134 | Flying Circus'' and has nothing to do with nasty reptiles. Making |
| 135 | references to Monty Python skits in documentation is not only allowed, |
| 136 | it is encouraged! |
| 137 | |
| 138 | <P> |
| 139 | Now that you are all excited about Python, you'll want to examine it |
| 140 | in some more detail. Since the best way to learn a language is |
| 141 | to use it, you are invited to do so with this tutorial. |
| 142 | |
| 143 | <P> |
| 144 | In the next chapter, the mechanics of using the interpreter are |
| 145 | explained. This is rather mundane information, but essential for |
| 146 | trying out the examples shown later. |
| 147 | |
| 148 | <P> |
| 149 | The rest of the tutorial introduces various features of the Python |
| 150 | language and system through examples, beginning with simple |
| 151 | expressions, statements and data types, through functions and modules, |
| 152 | and finally touching upon advanced concepts like exceptions |
| 153 | and user-defined classes. |
| 154 | |
| 155 | <P> |
| 156 | |
| 157 | <DIV CLASS="navigation"> |
| 158 | <div class='online-navigation'> |
| 159 | <p></p><hr /> |
| 160 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 161 | <tr> |
| 162 | <td class='online-navigation'><a rel="prev" title="Contents" |
| 163 | href="node2.html"><img src='../icons/previous.png' |
| 164 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 165 | <td class='online-navigation'><a rel="parent" title="Python Tutorial" |
| 166 | href="tut.html"><img src='../icons/up.png' |
| 167 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 168 | <td class='online-navigation'><a rel="next" title="2. Using the Python" |
| 169 | href="node4.html"><img src='../icons/next.png' |
| 170 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 171 | <td align="center" width="100%">Python Tutorial</td> |
| 172 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 173 | href="node2.html"><img src='../icons/contents.png' |
| 174 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 175 | <td class='online-navigation'><img src='../icons/blank.png' |
| 176 | border='0' height='32' alt='' width='32' /></td> |
| 177 | <td class='online-navigation'><a rel="index" title="Index" |
| 178 | href="node19.html"><img src='../icons/index.png' |
| 179 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 180 | </tr></table> |
| 181 | <div class='online-navigation'> |
| 182 | <b class="navlabel">Previous:</b> |
| 183 | <a class="sectref" rel="prev" href="node2.html">Contents</A> |
| 184 | <b class="navlabel">Up:</b> |
| 185 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> |
| 186 | <b class="navlabel">Next:</b> |
| 187 | <a class="sectref" rel="next" href="node4.html">2. Using the Python</A> |
| 188 | </div> |
| 189 | </div> |
| 190 | <hr /> |
| 191 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 192 | </DIV> |
| 193 | <!--End of Navigation Panel--> |
| 194 | <ADDRESS> |
| 195 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 196 | </ADDRESS> |
| 197 | </BODY> |
| 198 | </HTML> |