Python:绝对入门者简介Python: Introduction for Absolute Beginners

所需积分/C币:8 2019-10-28 19:39:22 2.36MB PDF
收藏 收藏

Programming languages Compiled Interpreted CC++ Fortran Java Python Shell Perl Interactive Batch As you probably know, programming languages split into two broad camps according to how they are used Compiled languages go through a compilation"stage where the text written by the programmer is converted into machine code. This machine code is then processed directly by the Cpu at a later stage when the user wants to run the program. This is called, unsurprisingly, run time” Interpreted languages are stored as the text written by the programmer and this is converted into machine instructions all in one go at run time There are some languages which occupy the middle ground. Java, for example, is converted into a pseudo-machine-code for a CPU that doesn't actually exist. At run time the java environment emulates this CPu in a program which interprets the supposed machine code in the same way that a standard interpreter interprets the plain text of its program. In the way Java is treated it is closer to a compiled language than a classic interpreted language so it is treated as a compiled language in this course Python can create some intermediate files to make subsequent interpretation simpler However, there is no formal"compilation"phase the user goes through to create these files and they get automatically handled by the python system So in terms of how we use it, Python is a classic interpreted language. Any clever tricks it pulls behind the curtains will be ignored for the purposes of this course So, if an interpreted language takes text programs and runs them directly, where does it get its text from? Interpreted languages typically support getting their text either directly from the user typing at the keyboard or from a text file of commands If the interpreter(Python in our case) gets it input from the user then we say it is running interactively". If it gets its input from a file we say it is running in"batch mode. We tend to use interactive mode for simple use and a text file for anything complex Interactive use Unix prompt python Python26(r26:66714,Feb32009,20:5203) [GCC 4.3.2 Igcc-4 3-branch revision 141291]on Type help","copyright","credits"or"license >> print Hello, world! Python prompt Hello world! >>>3 3 Now that we have a Unix command line interpreter we issue the command to launch the Python interpreter. That command is the single word,“ python” In these notes we show the Unix prompt, the hint from the Unix system that it is ready to receive commands, as a single dollar sign character ($). On PWF Linux the prompt is actually that character preceded by some other intormation Another convention in these notes is to indicate with the use of bold face the text that you have to type while regular type face is used for the computer's output The interactive Python interpreter starts by printing three lines of introductory blurb which will not be of interest to us After this preamble though, it prints a Python prompt. This consists of three"greater than"characters (>>)and is the indication that the Python interpreter is ready for you to type some Python commands. You cannot type Unix commands at the prompt.(Well, you can type them but the interpreter won't understand them.) So let's issue our first Python command. There's a tradition in computing that the first program developed in any language should output the phrase hello, world! and we see no reason to deviate from the norm here The Python command to output some text is"print". This command needs to be followed by the text to be output. The text, "Hello, world! is surrounded by single quotes ()to indicate that it should be considered as text by Python and not some other commands. The item(or items )that a command(such as print) needs to know what to do are called its "arguments", so here we would say that Hello world! is the print command's argument The command is executed and the text"Hello, world! is produced. The print command always starts a new line after outputting its text You will probably not be surprised to learn that everything in Python is case-sensitive: you have to give the print command all in lower-case,“ PRINT”,"“pR⊥Nt”,etc. won't work Note that what the Python interpreter does is evaluate whatever it has been given and outputs the result of that evaluation, so if we just give it a bare number, e.g. 3, then it evaluates that number and displays the result s python othon2.6(r26:66714,Feb32009,20:52:03) Type " help","copyright ,"credits"or "license" on GCC 4.3.2 gcc-4 3-branch revision 1412911 >> print Hello, world Hello. world! >>>3 >>> To quit the Python interpreter Press control+d s Unix prompt Now that we are in the python interpreter it would be useful if we knew how to get out of it again. In common with many Unix commands that read input from the keyboard, the interpreter can be quit by indicating"end of input". This is done with a control+d". To get this hold down the control key (typically marked"Ctrl)and tap the "D"key once. Then release the control key. Be careful to only press the"D"key once. The"control+d"key combination, meaning end of input, also means this to the underlying Unix command interpreter. If you press"control+d twice, the first kills off the python interpreter returning control to the unix command line, and the second kills off the Unix command interpreter. If the entire terminal window disappears then this is what you have done wrong. Start up another window, restart Python and try again If you are running Python interactively on a non- Unix platform you may need a different key combination. If you type" exit "at the Python prompt it will tell you what you need to do on the current platform. On PWF Linux you get this >> exit Use exit or ctrl-d(i.e. EOF)to exit It would also be useful if we knew how to use pythons help system We'll look at how we access Pythons help in a few slides'time Batch use #!/usr/bin/python python hello. py print 'Hello, world Hello, world No“3 hello. py Now let us look at the file hello py in the home directory of the course accounts you are using. We see the same two lines print Hello, wor ld! 3 (Ignore the line starting # Such lines are comments and have no effect on a Python program. We will return to them later.) The suffix".py"on the file name is not required by Python but it is conventional and some editors wi put you into a " Python editing mode automatically if the files name ends that way. Also, on some non Unix platforms(such as Microsoft Windows) the " py""suffix will indicate to the operating system that the file is a Python script. (Python programs are conventionally referred to as"Python scripts".) We can run this script in batch mode by giving the name of the file to the Python interpreter at the Unix prompt ss python hello. py Hello wor ld This time we see different output. The print command seems to execute, but there is no sign of the 3 We can now see the differences between interactive mode and batch mode Interactive Python evaluates every line given to it and outputs the evaluation. The print command doesn't evaluate to anything but prints its argument at the same time. The integer 3 outputs nothing (it isn't a command but evaluates to 3 so that gets output Batch mode is more terse. Evaluation is not output, but done quietly. Only the commands that explicitly generate output produce text on the screen Batch mode is similarly more terse in not printing the introductory blurb 6 s python Python2.6(26:66714,Feb32009,20:52:03 IGCC 4.3.2 [gcc-43-branch revision 141291] on Type "help", "copyright", " credits"or"license >> help Type helpo for interactive help or help(object for help about object >> helpo Welcome to Python 2.6! This is the online help utility If this is your first time using Python, help> help utility prompt Launch the python interpreter again as we will be using it interactively for a while The first thing we will do is look at Python's interactive help(which Python refers to as its "online help utility”) You may have noticed that the introductory blurb we get when we start Python suggests a number of words we might like to type to get "more information One of those words is"help". Let's see what happens if we type "help"at the Python prom >> help Type help o for interactive help, or help ( object)for help about object Python tells us there are two ways we can get help from within the Python interpreter. We can either get interactive help via its online help utility by typing "help (," which we ll do in a moment, or we can directly ask Python for help about some particular thing(which we 'll do a bit later by typing"help( thing") where"thing "is the Python command we want to know about). So let's try out Python's interactive help, by typing"help o "at the Python prompt. This will start Python's online help utility as shown above. Note that we get an introductory blurb telling us how the utility works(as well as how to quit it(type"quit)), and the prompt changes from Python's prompt (>>>”)to help> Note that the online help utility will only provide useful information if the Python documentation is installed on the system. If the documentation hasn't been installed then you won' t be able to get help this way. Fortunately, the complete python documentation(exactly as given by the online help utility is available on the web at http://docs.pythonorg/ in a variety of formats. It also includes a tutorial on Python help> printThe thing on which you want help help> quit Type"quit"to leave the help utility You are now leaving help and returning to the python interpreter. If you want to ask fpr help on a particular object directly from the interpreter, you car type"help(object)". Executing"help('string,) has the same effect as typing a particular string at the help> prompt >>> Back to Python prompt help('print) Note the quote marks (or"") >>> Official Python documentation(includes tutorial) http://docs.pythonorg Using Python 's online help utility interactively is really straightforward: you just type the name of the Python command, keyword or topic you want to learn more about and press return Let' s see what happens if we ask it about the print command help> print On PWF Linux the screen clears and we get a new screen of text that looks something like this: 6. 6 The print statement print_ stmt print(expression[1("," expression[2])*[ expression [3][("expression[4])+[" 1) Download entire grammar as text. [5] print evaluates each expression in turn and writes the resulting object lines 1-10 (For space reasons only the first 10 lines of the help text are shown(in very small type-don't try and read this text in these notes here but rather try this out yourself on the computer in front of you). You can get another page of text by pressing the space bar, and move back a page by typing "b''(for "back), and you can quit from this screen by typing"“q”(for“quit”).( f you want help onon what you can do in this screen,type“h”(for help"). The program that is displaying this text is not Python but an external program known as a pager (because it displays text a page at a time)-this means exactly which pager is used and how it behaves depends on the underlying operating system and how it is configured Note that on PWF Linux when you finish reading this help text and type q, the help text disappears and you get back the screen you had before, complete with the help>” prompt When youve finished trying this out, quit the help utility by typing quit"at the"help>"prompt and pressing return. Finally, we can also get help on a Python command, keyword or help topic directly, without using the online help utility interactively. To do this, type"help( thing )"at the Python prompt, where “ thing” is the Python command,etc. on which you want help( note the quotes() around“th立ng”) 8 s python Python2.6(26:66714,Feb32009,20:52:03 IGCC 4.3.2 [gcc-43-branch revision 141291] on Type "help", "copyright", " credits"or"license >> print 3 Pair of arguments separated by a comma >> print 3,5 35 Pair of outputs no comma 9 Before we go any further, we'll demonstrate a property of the print command We have already seen print used with a single argument But we can also use it with two arguments, separated by a comma Note that print outputs the two arguments(separated by a single space), and not the comma The comma separates the arguments; it is not an argument in its own right This is a general feature of Python commands: when the command has multiple arguments, commas are used to separate the arguments 9 Using Python for science Science Quantitative Using numbers Numbers in Python We are going to spend a little while talking about numbers and how computers (and in particular python handle them Why? Well, this is a course in the "Scientific Computing series and science is about quantifying things. That requires numbers to represent those quantities and so we need to understand how python handles numbers Those of you who are already familiar with how computers handle numbers, please bear with us for a little while -despite what you may be tempted to think, this is not common knowledge to all programmers, especially those who have learnt to program in languages which tend to hide the subtleties of computer arithmetic from the programmer (e.g. Perl Visual Basic) 10

试读 127P Python:绝对入门者简介Python: Introduction for Absolute Beginners
立即下载 低至0.43元/次 身份认证VIP会员低至7折
    weixin_38743968 如果觉得有用,不妨留言支持一下
    • 至尊王者

    关注 私信 TA的资源
    Python:绝对入门者简介Python: Introduction for Absolute Beginners 8积分/C币 立即下载
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第1页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第2页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第3页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第4页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第5页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第6页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第7页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第8页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第9页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第10页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第11页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第12页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第13页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第14页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第15页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第16页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第17页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第18页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第19页
    Python:绝对入门者简介Python: Introduction for Absolute Beginners第20页


    8积分/C币 立即下载 >