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

📄 python for newbies.htm

📁 1000 HOWTOs for various needs [WINDOWS]
💻 HTM
📖 第 1 页 / 共 4 页
字号:


<HTML><HEAD><TITLE>Python for Newbies</TITLE>

<!--Style from http://blacksun.box.sk/tutorials.htm :)-->

<STYLE type=text/css>A:active {

	TEXT-DECORATION: none

}

A:hover {

	TEXT-DECORATION: underline

}

A:link {

	TEXT-DECORATION: none

}

A:visited {

	TEXT-DECORATION: none

}

</STYLE>



</HEAD>

<BODY aLink=#ccff00 bgColor=#000000 link=#99ccff text=#cccccc 

vLink=#cc99ff><FONT face="Verdana, Arial, Helvetica, sans-serif"><A name=top>

<H1 align=center>Python for Newbies</H1></A>

<H2 align=center>by <A href="mailto:skinite@home.com">Pupp3tM</A>, a.k.a. David 

Borowitz</H2>

<H3>Table of Contents</H3>

<OL>

  <LI><A 

  href="#intro">Introduction</A> 



  <OL type=a>

    <LI><A 

    href="#whatis">What 

    is Python?</A> 

    <LI><A 

    href="#interp">The 

    Interpreter</A> </LI></OL>

  <LI><A 

  href="#first">Your 

  First Program</A> 

  <OL type=a>

    <LI><A 

    href="#hello">Hello, 

    World!</A> 

    <LI><A 

    href="#explain">Explanation</A> 

    </LI></OL>

  <LI><A 

  href="#varmath">Variables 

  and Math</A> 

  <OL type=a>

    <LI><A 

    href="#var">Variables: 

    Strings, Numbers, etc.</A> 

    <LI><A 

    href="#math">Math 

    and Operators</A> </LI></OL>

  <LI><A 

  href="#io">Input/Output</A> 



  <OL type=a>

    <LI><A 

    href="#print">Printing 

    information</A> 

    <LI><A 

    href="#user">Interacting 

    with the User</A> </LI></OL>

  <LI><A 

  href="#control">Program 

  Control</A> 

  <OL type=a>

    <LI><A 

    href="#if">What 

    If...</A> 

    <LI><A 

    href="#for">For 

    Ever...</A> 

    <LI><A 

    href="#while">While 

    We...and Others</A> </LI></OL>

  <LI><A 

  href="#inter">Intermediate 

  Python</A> 

  <OL type=a>

    <LI><A 

    href="#tuples">Tuples!</A> 



    <LI><A 

    href="#string">Strings 

    and Slice Indexing</A> 

    <LI><A 

    href="#fileio">File 

    I/O</A> </LI></OL>

  <LI><A 

  href="#mod">Modules</A> 



  <OL type=a>

    <LI><A 

    href="#mod1">Overview 

    and Importing</A> 

    <LI><A 

    href="#builtin">Builtin 

    Modules</A> 

    <LI><A 

    href="#yours">User-Defined 

    Modules and Functions</A> </LI></OL>

  <LI><A 

  href="#close">In 

  Closing</A> 

  <OL type=a>

    <LI><A 

    href="#perl">Why 

    Python is Better than Perl</A> 

    <LI><A 

    href="#ref">References</A> 

    </LI></OL></LI></OL>

<HR>



<H2 align=center><A name=intro>Introduction</A></H2>

<H3 align=center><A name=whatis>What is Python?</A></H3><BR>

<P>Python is, in short, a scripting language. It is similar in function to Perl, 

but to my knowledge is not nearly as popular. To fix that is part of the goal of 

this tutorial. It is a very high-level language that can perform complex tasks, 

but is surprisingly easy to learn. Many different add=ons (modules) are 

available to control everything from MP3s to windowing toolkits (Unix). I find 

Python to be a just plain <I>fun</I> language to program in, as it is very 

intuitive and suited to a variety of uses. </P>

<P>Python can be run on almost any platform, from 'nix to Windows. To get 

Python, first go to <A href="http://www.python.org/">Python.org</A>, and then 

download away! </P>

<P>This tutorial is geared towards people who have little experience with 

programming but know at least something. I make numerous references to languages 

like C and Perl, which are good to know, but you won't lose much if you just 

skip over them. </P>

<P>What's with the funky name, you ask? Some sort of carnivorous reptile? No, 

dear reader. Python's creators were (are) big Monty Python fans. The name comes 

from the BBC show "Monty Python's Flying Circus," and in the official docs, it 

says "Making references to Monty Python skits in documentation is not only 

allowed, it is encouraged." That said, I'm afraid I haven't seen all too many 

Monty Python movies so we'll go a little light on the references :) </P>

<P>So sit back, grab a Coke, <A href="http://www.python.org/download/">download 

Python</A>, and get ready to learn :) </P>

<P><FONT size=-2><A 

href="#top">Back 

to top</A></FONT></P>

<HR>



<H3 align=center><A name=interp>The Interpreter</A></H3><BR>

<P>Python is a scripted, i.e. interpreted, language. Unlike C, which has a 

compiler, we have in Python the...(drum roll please)...Python interpreter. Once 

you have Python correctly installed, in your Unix shell or Dos box type 'python' 

(Note: anytime I tell you to type something and put it in quotes, don't type the 

quotes. I'm sure you already figured this out, but...). If you've used perl or 

some other interpreted language before, you might notice something: there's a 

prompt (those three little "&gt;&gt;&gt;" things)! Not waiting for stdin or 

saying "Usage: python &lt;script-file&gt;"! That's right, Python has an 

<I>interactive mode</I>. In this mode, you can type any Python command and it 

will work just like you typed it from a script, with a few differences. Most 

importantly, if you type a variable of some sort or something that returns a 

value of some sort (except assignments), it will print the result automatically. 

Neat for trying stuff out (and learning!). Now type '1 + 1' at the prompt you 

should still be at. What do you see? 2! Now ain't that a powerful language: <PRE>&gt;&gt;&gt; 1 + 1

2

</PRE>You can do this with any kind of variable or math expression (see part <A 

href="#math">3b</A>) 

and get some output. Works with strings too: <PRE>&gt;&gt;&gt; "elp! I'm being oppressed"

"elp! I'm being oppressed"

</PRE>Note the bad Monty Python reference :). Anyway, that's all for the 

interpreter. Now we get into the meat of things. If at any point there's a 

sample script and you don't feel like creating a new file for it, feel free to 

type (or better yet copy &amp; paste) it into the interpreter, it should work 

the same. Wait, scratch that :). If I put something in a script, it most likely 

means you only get the effect from a script and it won't work in interactive 

mode. Sorry. 

<P></P>

<P>Wait! One note before we leave. To quit the interpreter, you can't do 

anything fancy like type 'quit'. For some reason (beats me), that doesn't work. 

You have to type Ctrl+D to quit (on Unix at least, haven't tested it on 

Windows). Anyway if you type 'quit' it'll say something like 'Use Ctrl-D (i.e. 

EOF) to exit.' so listen to that. </P>

<P><FONT size=-2><A 

href="#top">Back 

to top</A></FONT></P>

<HR>



<H2 align=center><A name=first>Your First Program</A></H2>

<H3 align=center><A name=hello>Hello, World!</A></H3><BR>Alright, if you've ever 

read a programming tutorial before you know they all have some sort of program 

that just prints "Hello, World!" or something on the screen. Unfortunately, this 

tutorial is no different. Alright, here goes: <PRE>#!/usr/bin/python

#You only need the above line for Unix; replace that with your path to python



hello = "Hello, World!"

print hello

</PRE>That's it! Put that in a text file, name it hello.py, run it, and we're 

good. On Unix you may have to 'chmod +x hello.py' to run it, or just type 

'python hello.py'. Windows, I'm pretty sure .py files are associated with the 

Python interpreter automatically. 

<P></P>

<P><FONT size=-2><A 

href="#top">Back 

to top</A></FONT></P>

<HR>



<H3 align=center><A name=explain>Explanation</A></H3><BR>"Wow," you may be 

saying, "that was easy." Indeed it was. Now, though, you probably only have some 

remote clue of what's going on. We'll take it line by line. <PRE>#!/usr/bin/python

</PRE>This line, like you should've read, is only needed for Unix users. It 

tells your shell where the python executable is so it can run the script. <PRE>#You only need the above line for Unix; replace that with your path to python

</PRE>A pretty useless line, you may think, but it illustrates an important 

point: comments! Any good programmer knows that you need comments in your code, 

so just put # and anything after that on that line is a comment and will be 

ignored by the interpreter. <PRE>hello = "Hello, World!"

</PRE>I didn't really need this line, but I thought a Hello, World! script with 

only one line is kinda lame, so I added it anyway. hello is a string variable, 

which we'll talk about later, and the = operator assigns "Hello, World!" as its 

value. I'm sure if you've programmed before you've seen all this. <PRE>print hello

</PRE>This last line, as you may well guess, prints the contents of hello. If 

you're a perl guy (or girl :), you need to know that if you're printing a 

variable you can't put the name of the variable in quotes. But that's not too 

inconvenient, as we should see later. 

<P></P>

<P>Alright! Our first program! Now just a note or two before we move on. First, 

you may notice that there's nothing on the end of any lines, no ; like in perl 

or C. That's one of the convenient features of Python. I think it's more 

intuitive because blocks (later dammit!) are determined by indenting and every 

statement has its own line...you may disagree I guess but you'll just have to 

get used to it. </P>

<P><FONT size=-2><A 

href="#top">Back 

to top</A></FONT></P>

<HR>



<H2 align=center><A name=varmath>Variables and Math</A></H2>

<H3 align=center><A name=var>Variables: Strings, Numbers, etc.</A></H3><BR>In 

Python, just like any other language worth its salt, there are a number of data 

types, Mainly, there are numbers (of arbitrary precision), strings, lists, and 

tuples. Tuples are more of an "advanced" variable type, kind of like lists, so I 

won't go into them here, but I'll touch on them a bit in the <A 

href="#inter">Intermediate 

Python</A> section. 

<P></P>

<P>Numbers. What can I say? Numbers are numbers. To assign them, just like you 

did with the string in Hello World!, just put the name of your variable, the = 

operator, and what you want it to be: <PRE>&gt;&gt;&gt; a = 5

&gt;&gt;&gt;

</PRE>A word on variable names: I'm not quite sure exactly how long Python 

allows them to be (should I know?), but as a general rule you shouldn't have 

names more than 20 or so characters. Like other languages, variable names must 

be all alphanumeric characters and underscores (_), but you <I>can</I> start 

names with underscores, unlike some languages. 

<P></P>

<P>Anyway, on to strings. Strings store, obviously, strings of characters. 

Nothing you haven't heard before. For strings, you can use either type of quotes 

("" or ''), but be sure you know which kind. If you want to use, say, an 

apostrophe inside a string, you need to make sure of some things. If the string 

has "" quotes, don't worry about it, just put your apostraph. If you were in '' 

quotes, though, you would need to escape the apostrophe. This is so the 

interpreter doesn't think the apostrophe, which is just like a quote, is the end 

of your string. To escape the apostrophe, just put a \ (backslash) in front of 

the quote. Try it! It's great: <PRE>&gt;&gt;&gt; "I'm Dave"

"I'm Dave"

&gt;&gt;&gt; 'Python\'s my favorite language'

"Python's my favorite language"

&gt;&gt;&gt; "\"I can't think of anything else to say!\" he said"

'"I can\'t think of anything else to say!" he said'

&gt;&gt;&gt; 'That's too bad'

      File "&lt;stdin&gt;", line 1

    'That's too bad'

          ^

SyntaxError: invalid syntax

</PRE>Note the last two examples. The first one illustrates escaping of the "" 

quotes and also this: If you have both types of quotes in a string, Python will 

default to one and escape the other. Don't worry; if you're printing this to the 

screen it won't show the \ character in your output. In the last example, that's 

what happens when you don't escape quotes. You may notice Python has a nice way 

of handling errors, but we won't go into that. 

<P></P>

<P>Speaking of escaping, here are a few more characters you can escape (if it 

says 'n' then you use '\n' to escape it): 

<UL>

  <LI>n - Prints a newline, i.e. starts a new line. 

  <LI>t - Horizontal tab 

  <LI>b - Backslash. Deletes the last character typed 

  <LI>a - System beep 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -