📄 index.lxp@lxpwrap=c653_252ehtm.htm
字号:
<table border="0" cellspacing="0" cellpadding="3" width="100%"><tr><td> <div align="center" id="bldcontent"> <a href="../default.htm"><img src="../images/opendocs.png" width="63" height="76" border="0"></a> <br> <div class="symbol">Your OpenSource Publisher™</div> </div> </td></tr></table> <div align="center" class="author"> <a href="../products.lxp">Products</a> | <a href="../wheretobuy.lxp">Where to buy</a> | <a href="../bookstore.lxp">Retailers</a> | <a href="../faq.lxp">FAQ</a> | <a href="../writeforus.lxp">Write for Us.</a> | <a href="#contact">Contact Us.</a> </div> <table border="0" cellspacing="3" cellpadding="0" width="100%"><tr><td width="100%"> <div class="content"> <table border="0" cellspacing="2" cellpadding="0" width="100%"><tr><td width="100%"> <div align="center"><H4 CLASS="AUTHOR"><A NAME="AEN5">Boudewijn Rempt</A><br><a href="../../https@secure.linuxports.com/opendocs/default.htm"><img src=odpyqt125.png></a><br>ISBN: 0-97003300-4-4<br><a href="../../https@secure.linuxports.com/opendocs/default.htm">Available from bookstores everywhere or you can order it here.</a><p>You can download the source files for the book <a href="pyqtsrc.tgz">(code / eps) here.</a><hr></div> <HTML><HEAD><TITLE>Introduction to Python</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.72"><LINKREL="HOME"TITLE="GUI Programming with Python: QT Edition"HREF="book1.htm"><LINKREL="UP"TITLE="Introduction to the BlackAdder IDE"HREF="p266.htm"><LINKREL="PREVIOUS"TITLE="Conclusion"HREF="x650.htm"><LINKREL="NEXT"TITLE="The Rules"HREF="x719.htm"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GUI Programming with Python: QT Edition</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><A accesskey="P" href="index.lxp@lxpwrap=x650_252ehtm.htm">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><A accesskey="N" href="index.lxp@lxpwrap=x719_252ehtm.htm">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1>Chapter 4. Introduction to Python</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><A href="index.lxp@lxpwrap=c653_252ehtm.htm#AEN663">Programming fundamentals</A></DT><DT><A href="index.lxp@lxpwrap=x719_252ehtm.htm">The Rules</A></DT><DT><A href="index.lxp@lxpwrap=x823_252ehtm.htm">Constructions</A></DT><DT><A href="index.lxp@lxpwrap=x879_252ehtm.htm">Conclusion</A></DT></DL></DIV><P>In this chapter I attempt to explain the fundamentals of Python. Here I have the same difficulty as Bertie Wooster faces when he tries to keep us abreast of the developments in <ICLASS="CITETITLE">Much Obliged, Jeeves</I>. If I start too early, and begin at the very beginning, telling you all about how a computer doesn't understand plain English, I'm likely to irritate the coves who already know all about that, and just want a quick update on the high-level datastructures of Python and the current state of iterators and generators. However, that would leave the birds who are just starting out wondering whether it was such a good idea, after all, to pick up this book, and start learning how to program.</P><P>The fact is, writing an introduction to a complete programming language — or the concept of programming in itself — in just one chapter is the deuce of a task. It can't really be done, I'm afraid to say. If you already know a few programming languages, the on-line Python tutorial that is included with BlackAdder (or with Python itself) will probably suffice. If you haven't programmed all that much before, I highly advise you to buy Marc Lutz' excellent book, <ICLASS="CITETITLE">Learning燩ython</I>, which is more like an introduction to programming, with a focus on Python.</P><P>Still with me? Then we had better take a quick tour through Python — which is really one of the easiest programming languages to master. Like ancient Gaul, and like this book, I have divided this chapter into three sections. The first tries to gently introduce the concept of programming to people who need to be primed with the most basic concepts. This is difficult for me to do, because I have been programming since I was twelve years old, so bear with me. The second is about Rules. Every programming language needs rules, and these are the rules that you need to keep in mind while programming Python. The final part gives an overview of the various constructions that Python contains for your satisfaction and pleasure.</P><DIVCLASS="SECT1"><H1CLASS="SECT1">Programming fundamentals</A></H1><P>Please don't think that I can teach you programming in just the space of this section — you need to read some good books for that, such as Steve McConnel's <ICLASS="CITETITLE">Code Complete</I>. What I can do is show you what the fuss is all about.</P><P>Computers do not do anything of their own volition: ultimately, someone has always told the machine what to do. Even crashing, down to the ultimate Blue Screen of Death, is caused by a computer blindly following instructions given by a human being.</P><P>Instructions can take the form of mouseclicks on fancy icons or buttons, or of bits of text the computer can understand. While there is still no computer that can understand plain English, there are many sub-dialects of English that a computer can understand. Python is one of these — a mix between pidgin English and mathematical notation. It is close to both the way computers work, and the way people think.</P><P>Unless you have a speech-to-text interface for your computer, you will have to type out all the pidgin-English, and then tell the computer to read what you've written, and do what you told it to. In a sense, you have to write a kind of manual for the computer to read, on how to perform a certain task.</P><P>Let's start with a simple example: fire up BlackAdder, and open the Python Interpreter window. If you start typing at the <TTCLASS="PROMPT">>>></TT>, nothing will happen — only by pressing the Enter key will Python realize that it has been spoken to. Go ahead and type something — you can't hurt the computer or your system, except if, by a fluke, you type <TTCLASS="USERINPUT"><B>import os</B></TT>, followed by Enter and <TTCLASS="USERINPUT"><B>os.system("deltree c:")</B></TT> — which would radically clean out your C drive. So don't do this! On the other hand, asking Python about the captain's age or the contents of a bathtub that's being filled by two taps is all right.</P><P>Chances are very small that you will have hit upon something Python understands by accident, for you are strictly limited to the few <SPAN><ICLASS="EMPHASIS">keywords</I></SPAN> Python actually knows about. Most of these keywords are concerned with creating <SPAN><ICLASS="EMPHASIS">blocks</I></SPAN> of instructions, called functions. Functions are used to construct more complex systems. Other keywords are used for creating another kind of block, called classes, which are combinations of information and instructions.</P><P>Let's construct a class that knows the value of something (though not the price), and has a function that does something to that value. Remember to press enter at the end of each line, and don't type the three > signs or the three dots — Python does this for you.</P><PRECLASS="SCREEN">Python 2.1.1 (#1, Aug 11 2001, 20:14:53) [GCC 2.95.2 19991024(release)] on linux2 Type "copyright", "credits" or "license"for more information.>>> class FirstClass:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -