亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? python for newbies.htm

?? 1000 HOWTOs for various needs [WINDOWS]
?? HTM
?? 第 1 頁 / 共 4 頁
字號:
Finally, an example: <PRE>&gt;&gt;&gt; f.readlines() #Our imaginary file; create it if you want

['This is the first line\012', 'This is the second line\012', "Hmm I'm running out

of creative things to say\012", 'Alright, this is the last line']

&gt;&gt;&gt; f.seek(0) #You need this to go back to the beginning of the file; see below

&gt;&gt;&gt; f.readline()

'This is the first line\012'

</PRE>Just in case you're wondering, the \012 at the end of every line is just a 

newline. When reading files, Python replaces simple \ characters like \n with 

their hex (ASCII) code. If you didn't get that, just remember that \012's are 

really \n's. There are three (maybe four) more important methods of files. 

First, read and seek. read just reads the entire file into a string, or a 

maximum number of bytes if you specify it. seek needs one argument, which is the 

offset in the file to go to, or seek. Huh? Well, Python reads files by putting a 

little pointer in one and moving it along. When you read from a file, the little 

pointer moves along to the end of the part you read. If you read again, it'll 

start from where the pointer left off. seek will tell the pointer to go back (or 

forward) to a specified point in the file. Here's another example, using the 

same imaginary file as above: <PRE>&gt;&gt;&gt; f.seek(12) #Offsets start at 0, therefore 12 is 'f'

&gt;&gt;&gt; f.read(10)

'first line'

&gt;&gt;&gt; f.seek(0)

&gt;&gt;&gt; f.read() #Whole file. You probably shouldn't do this with big files.

"This is the first line\012This is the second line\012Hmm I'm running out

of creative things to say\012Alright, this is the last line"

</PRE>Then, finally, there is the write function, which (surprise!) writes to 

your file. Just put in the parentheses the string you want to write. Be sure you 

have a file opened in mode w, r+, or a, though! Also, if you want to create a 

new file you must use the mode w or a, not r+. The last method is close, which 

closes the file and frees up any memory it's been using. It's always good 

programming practice to close any files when you're done with them or before you 

leave. One last example for this section, then we move on: <PRE>&gt;&gt;&gt; f = open( "mynewfile.txt", "w" )

&gt;&gt;&gt; f.write( "This is some text" )

&gt;&gt;&gt; f.close()

&gt;&gt;&gt; f.read() #Oops! Can't read a closed file (or a write-only file for that matter)

Traceback (most recent call last):

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

ValueError: I/O operation on closed file

&gt;&gt;&gt; f = open( "mynewfile.txt" )

&gt;&gt;&gt; f.read()

'This is some text'

</PRE>

<P></P>

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

href="#top">Back 

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

<HR>



<H2 align=center><A name=mod>Modules</A></H2>

<H3 align=center><A name=mod1>Overview and Importing</A></H3>

<P>Before we talk about Python's wide variety of builtin modules, let's go over 

what a module is first and how to get to them. A module is a script (or compiled 

library) that can be loaded into the interpreter environment separately from the 

core language of Python. Modules are accessed with the import statement, which 

can be used in a number of ways: <PRE>&gt;&gt;&gt; import sys #For these examples we're using the builtin sys module

&gt;&gt;&gt; sys.argv #Access names from the modules like you would an object

['']

&gt;&gt;&gt; from sys import argv #Another way to call it

&gt;&gt;&gt; argv

['']

&gt;&gt;&gt; from sys import * #Get everything in the sys module

&gt;&gt;&gt; stdout

&lt;open file '&lt;stdout&gt;', mode 'w' at 0x80d34b0&gt;

</PRE>Confusing? Not really. Let's go through it. The first line imports the 

module sys (one of the <A 

href="#builtin">builtin 

modules</A>). Notice on the next line that we needed to call a member of sys, 

argv (the arguments passed to the interpreter), with a . like we did for 

objects. This is using the so-called sys namespace, i.e. all sys members are 

accessed using, obviously, the name 'sys'. Alternatively, if we put 'from 

&lt;module&gt; import &lt;members&gt;', we use the global namespace. Then you 

can access the method's members just like any other sort of local variables or 

base functions. If you want to use the whole module in the global namespace, 

just do a 'from &lt;module&gt; import *'. Not too hard, eh? 

<P></P>

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

href="#top">Back 

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

<HR>



<H3 align=center><A name=builtin>Builtin Modules</A></H3>

<P>Python, being the versatile language it is, has a large selection of modules 

built into the default distribution. And I do mean large: at my count, there are 

196 modules available in Python 2.0 (including obsolete and OS-specific ones). 

When I say "builtin," this doesn't mean you don't have to import them, just that 

they should be included in Python installation. So, I'll list a choice 5 or so 

here, with basic descriptions of what they do and only a few of their members. 

</P>

<UL>

  <LI><B><FONT size=+1>sys</FONT></B> - Basic system/program functions 

  <UL>

    <LI><B>argv</B> - List of commands passed to the interpreter 

    <LI><B>stdout, stdin, stderr</B> - Basic standard output, standard input, 

    and standard error. These can be accessed like normal files. 

    <LI><B>exit</B> - Exits the program gracefully, accepting a value as the 

    exit code (basically 0=good exit, non-0=error) 

    <LI><B>path</B> - The paths Python looks in to find modules to import 

  </LI></UL>

  <LI><B><FONT size=+1>math</FONT></B> - Mathematical functions and constants. I 

  won't list them all here, but math contains many functions like sin, acos, 

  tanh, log, etc. Here are the constants: 

  <UL>

    <LI><B>pi</B> - The mathematical constant &#960;. The ratio of a circle's 

    diameter to its circumference, approximately 3.1415926535897931 

    <LI><B>e</B> - The base of the natural logarithm (ln). Get it with the sum 

    of 1/k! for all numbers k 0-&gt;infinity. Oddly enough, factorials (and 

    infinity) are not in the math module. </LI></UL>

  <LI><B><FONT size=+1>string</FONT></B> - Functions for more manipulations of 

  strings 

  <UL>

    <LI><B>printable</B> - A string of all so-called printable characters. Also 

    available are digits, letters (uppercase and lowercase), whitespace, and 

    punctuation. 

    <LI><B>atoi, atof, atol</B> - Convert strings to int, float, and long, 

    respectively. In Python 2.0 you should use builtins functions int, float, 

    and long. 

    <LI><B>find</B> - Find the lowest index where the second string (argument) 

    appears in the first. 

    <LI><B>count</B> - Count the number of times the second string appears in 

    the first. 

    <LI><B>split</B> - Split the string given in the first argument by 

    whitespace characters; return a list. If a second argument is given, the 

    string is split according to that instead of whitespace. 

    <LI><B>strip</B> - Strip leading and trailing whitespace from the given 

    string. 

    <LI><B>replace</B> - Replace all occurrences of the second string in the 

    first string with the third string. Err, look at it this way: replace( str, 

    "old", "new" ) replaces all occurrences of "old" in str with "new". </LI></UL>

  <LI><B><FONT size=+1>random</FONT></B> 

  <UL>

    <LI><B>seed</B> - Seeds the random number generator with three optional 

    arguments; defaults to the current time 

    <LI><B>random</B> - Returns the next random number as a floating-point 

    number between 0 and 1. 

    <LI><B>randint</B> - Returns a random number between two given integers. 

    <LI><B>uniform</B> - Same as randint, only with floating-point numbers. 

    <LI><B>choice</B> - Randomly chooses an element from the specified list or 

    tuple. </LI></UL>

  <LI><B><FONT size=+1>time</FONT></B> - Functions for getting and manipulating 

  local and arbitrary times. See the Python library documentation for more 

  complete information. 

  <UL>

    <LI><B>time</B> - No arguments, returns a long number that's your system's 

    current time, in seconds since...some time. Not really useful without some 

    sort of parser, except for seeding randoms. 

    <LI><B>localtime</B> - A parser. Given a time (like that returned by the 

    time function), it returns a tuple with the following information: year 

    (four-digit), month (1-12), day (1-31), hour (0-23), minute (0-59), second 

    (0-61 - 60 &amp; 61 for leap seconds, never used), weekday (0-6, Monday is 

    0), day (1-366), daylight savings time (0 - no, 1 - yes, -1 - system 

    default). Useful :) 

    <LI><B>gmtime</B> - Same as above, except in Greenwich Mean Time, not your 

    system's local time. 

    <LI><B>asctime</B> - Given a tuple from localtime or gmtime, returns a 

    string in the format 'Thu Mar 22 18:24:35 2001'. </LI></UL>

  <LI><B><FONT size=+1>re</FONT></B> - Regular expression (regexp) operators. I 

  won't cover them here, but I thought all you Perl fans out there might like to 

  know they exist in Python. There are some definite peculiarities about regexps 

  in Python, so read the docs...docs are your friends :). </LI></UL>

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

href="#top">Back 

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

<HR>



<H3 align=center><A name=yours>User-Defined Modules and Functions</A></H3>

<P>Of course, what use are all these modules if you can't use your own? Well, a 

user-defined module is just a .py file in one of Python's module directories. To 

import the module, it's run, and any functions, variables, classes, etc. are 

imported in. You can specify what to import, just like for any other module. 

Well, then, that's all well and good, but a big part of these modules is 

functions. How to make your own functions, then? Ok. </P>

<P>Alright. So we want a function. Since I like math, we'll do a simple 

logarithm to any base using the log function in the math module. Here we go, try 

this: <PRE>&gt;&gt;&gt; def logb( b, n ):

...   from math import log

...   a = log(n)/log(b)

...   if a - int(a) == 0: #a is an int

...     return int(a)

...   else:

...     return a

...

</PRE>How nice. Only a couple things should need explaining, unless you haven't 

taken high school algebra (hey no offense...I'm just a freshman :). This 'def' 

thing? Well, it's just the name of the function and the arguments it takes. I 

should hope you have a good idea of what arguments are now. Then there's return. 

It's...uh...what the function returns :). Execution stops after a return 

statemnt, just like a break statement. Except it returns something. Ok. Well, 

I've kinda run out of things to say...so here's an example module, just for 

practice. Hey, this is a tutorial, isn't it? Let's go, put this in moremath.py: <PRE>#!/usr/bin/python



def logb( b, n ): #Definition

  from math import log #Need this!

  a = log(n)/log(b) #Change of base formula

  if a - int(a) == 0: #a is an int

    return int(a) #Get rid of that decimal :)

  else: #a is a float

    return a #Just return it



#Ye Olde Golden Ratio &lt;www.verbose.net&gt;

phi = 1.6180339887498948

</PRE>And now in the interpreter: <PRE>&gt;&gt;&gt; import moremath

&gt;&gt;&gt; moremath.phi #Floating point numbers aren't that accurate, you may notice

1.6180339887499999

&gt;&gt;&gt; from moremath import logb

&gt;&gt;&gt; logb(2,1024)

10

</PRE>Wow! A module! Just like other modules! But you did it myself! Or I did it 

yourself...or...wait...you did it yourself. If you look in the directory where 

you put moremath.py now, you'll now find a moremath.pyc file. This is a 

'compiled' file, which is not really compiled (it's bytecode - kinda like a Java 

.class file), but has less memory overhead than a .py file. As far as I know 

.pyc files are not portable, though, so if you're just using this module for 

yourself you can use the .pyc but if you want to distribute it, I recommend you 

keep the .py file. In any case, it's certainly easier to edit your source if you 

still have a .py file. Any questions :)? 

<P></P>

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

href="#top">Back 

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

<HR>



<H2 align=center><A name=close>In Closing</A></H2>

<H3 align=center><A name=perl>Why Python is Better than Perl</A></H3>

<P>I admit it: maybe Python isn't better than Perl. It's different, to say the 

least. For many things it <I>is</I> better. And it's got definite advantages, 

and that's what I thought I'd just review, or look at, or list, or whatever. 

Here goes. </P>

<UL>

  <LI>Intuitiveness. Instead of all that nasty, obfuscated code, Python has 

  syntax and blocking that resembles, somewhat, normal (English) speech and 

  indenting. 

  <LI>High-levelness. List slicing, tuple packing, etc. allow for high-level 

  manipulation of data without all that mucking about with 

  pointers...wait...that's C...and efficient though it may be, it sometimes even 

  confuses the 黚erhacker in me. But it's still easier in Python than Perl. 

  <LI>More integrated object-oriented (OO) support. OO in Perl involves working 

  with hashes as classes and weird stuff like that. Bottom line, Python was 

  designed from the start to be OO...even though I didn't talk about classes too 

  much. Sorry. 

  <LI>Better C integration. Well, this is opinionated here, but I think 

  embedding Python in C is easier than Perl...what can I say? 

  <LI>Interactive mode. No need to put scripts in separate files. You know what 

  I'm saying. 

  <LI>Wider variety of data structures. Perl has arrays, hashes, and scalars. 

  Python has scalars (int, float, long), lists, dictionaries (which I didn't 

  talk about; they're like hashes or associative arrays), tuples. And it's 

  extensible and evolving, according to the docs. 

  <LI>range function. People complain about the lack of a generic for loop, like 

  Perl has. range really makes the for loop...without having a whole different 

  type of loop. Less overhead. And you can use range for other things, too. 

  <LI>Monty Python. 'Nuff said. </LI></UL>

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

href="#top">Back 

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

<HR>



<H3 align=center><A name=ref>References</A></H3>

<UL>

  <LI><A href="http://www.python.org/">Python.org</A>. A must-see. 

  <LI><A href="http://www.python.org/doc/current/">Current Python docs</A>. 

  Gotta read them. 

  <LI><A href="http://www.zope.org/">Zope</A>. Great webserver written in 

  Python. 

  <LI><A href="http://www.programmershaven.com/zone23/links/link281.htm">Python 

  section at ProgrammersHaven</A>. More good links. 

  <LI><A href="http://blacksun.box.sk/">Black Sun Research Facility</A>. Great 

  site for networking, programming, cracking, OSes, etc. This tutorial should be 

  published there :) 

  <LI><A href="http://www.pythonline.com/">Pythonline</A>. Monty Python, may or 

  may not be the official site, I'm not sure. </LI></UL>

<P>Any comments, questions, glowing praise, rants, hatemail, feel free to <A 

href="mailto:skinite@home.com">mail me</A> or leave a message for me at <A 

href="http://dvd.box.sk/wb/?did=blacksun">BSRF's message board</A>. I'm a 

member, I check it regularly. Hatemail is ok, but death threats are discouraged. 

</P>

<P>Well, we've come to the end, at long last. I hope you've learned something, 

and you aren't too high on the adrenaline rush of coding. I'm not good at these 

ending things, so I think I'll just trail off right...about...now... </P>

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

href="#top">Back 

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

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
麻豆精品新av中文字幕| 欧美午夜精品一区二区蜜桃| www.日韩av| 欧美三电影在线| 久久综合久久综合久久| 亚洲一区中文日韩| 成人av在线影院| 精品999久久久| 天天综合日日夜夜精品| 91色porny蝌蚪| 日本一区二区电影| 韩国理伦片一区二区三区在线播放| 色婷婷激情综合| 中文字幕在线不卡一区二区三区| 狠狠色丁香久久婷婷综合_中| 欧美日韩精品三区| 亚洲午夜私人影院| av不卡免费在线观看| 久久精品一区二区三区不卡 | 久久综合色综合88| 日韩精品视频网站| 欧美视频一区二区三区四区| 国产精品麻豆一区二区| 国产91精品露脸国语对白| 欧美一级日韩免费不卡| 亚洲成av人影院| 欧美日韩一区二区在线观看视频 | 色呦呦网站一区| 综合婷婷亚洲小说| 99久久精品免费看| 国产精品久久久久久户外露出| 麻豆精品国产传媒mv男同| 91精品国产综合久久久久久久久久 | 精品国产精品网麻豆系列| 天天影视涩香欲综合网| 欧美一区二区三区免费在线看| 亚洲电影你懂得| 91精品国产麻豆国产自产在线| 亚洲福利视频一区二区| 欧美亚洲动漫精品| 丝袜美腿亚洲一区| 欧美成人综合网站| 国产精品一二一区| 国产精品乱码一区二三区小蝌蚪| 成人免费高清在线| 亚洲乱码中文字幕| 欧美另类一区二区三区| 青草av.久久免费一区| 精品久久一二三区| 成人av网址在线观看| 亚洲另类春色国产| 欧美日韩中文精品| 精品在线播放午夜| 国产精品区一区二区三| 在线亚洲人成电影网站色www| 五月激情丁香一区二区三区| 精品粉嫩aⅴ一区二区三区四区| 国产一区二区不卡| 亚洲乱码国产乱码精品精的特点 | 亚洲精品免费在线观看| 欧美日韩美少妇| 精品一区二区久久久| 欧美国产97人人爽人人喊| 日本高清不卡一区| 久久精品国产秦先生| 国产精品对白交换视频| 欧美日韩一区二区在线观看视频| 精品在线播放午夜| 伊人色综合久久天天| 日韩欧美国产精品| 99re这里只有精品6| 蜜桃久久精品一区二区| 自拍偷拍国产亚洲| 久久亚洲影视婷婷| 欧美在线观看视频一区二区| 极品销魂美女一区二区三区| 亚洲色图欧美在线| 久久综合色天天久久综合图片| 一本在线高清不卡dvd| 国产一区二区久久| 亚洲一二三四区| 亚洲国产岛国毛片在线| 91精品国产91久久久久久最新毛片| 高清不卡一区二区在线| 日韩电影在线观看网站| 亚洲欧洲国产专区| 久久精品一区二区三区不卡| 91精品蜜臀在线一区尤物| 99精品久久99久久久久| 九九国产精品视频| 丝袜美腿高跟呻吟高潮一区| 中文字幕在线观看一区二区| 久久先锋资源网| 欧美一级久久久久久久大片| 欧美怡红院视频| 成人avav影音| 丁香天五香天堂综合| 精品无人码麻豆乱码1区2区| 日韩黄色片在线观看| 亚洲激情网站免费观看| 国产精品久久影院| 国产日韩欧美亚洲| 亚洲精品一区二区三区在线观看| 欧美色视频一区| 在线观看视频欧美| 一本到一区二区三区| av资源站一区| aa级大片欧美| 成人午夜视频福利| 成人福利电影精品一区二区在线观看| 久久99精品国产.久久久久| 男男gaygay亚洲| 另类小说综合欧美亚洲| 欧美aaaaa成人免费观看视频| 亚洲一区二三区| 亚洲国产成人高清精品| 一区二区欧美视频| 亚洲一级不卡视频| 天天综合天天综合色| 日韩av中文字幕一区二区| 天天综合日日夜夜精品| 麻豆免费看一区二区三区| 日本vs亚洲vs韩国一区三区| 日韩av电影天堂| 久久er精品视频| 国产精品综合一区二区三区| 高清在线成人网| 一本到三区不卡视频| 在线中文字幕不卡| 制服丝袜日韩国产| 精品黑人一区二区三区久久| 久久精品日韩一区二区三区| 国产精品久久久久久亚洲毛片 | 亚洲欧洲国产日本综合| 夜夜嗨av一区二区三区网页 | 一区二区三区日韩在线观看| 亚洲一区二区三区在线| 日韩国产高清在线| 国产一区二区不卡在线| 99国产精品一区| 在线不卡免费欧美| 久久久精品日韩欧美| 国产精品三级视频| 午夜成人免费视频| 国产一区二区三区日韩 | 亚洲欧美日本在线| 日本欧美加勒比视频| 国产主播一区二区| 日本道色综合久久| 欧美v亚洲v综合ⅴ国产v| 中文字幕中文乱码欧美一区二区| 国产精品福利一区| 日韩国产欧美三级| 99久久久精品免费观看国产蜜| 在线播放日韩导航| 欧美极品少妇xxxxⅹ高跟鞋| 亚洲一区二区三区在线播放| 国内久久精品视频| 欧美日韩精品专区| 国产精品久99| 久久国产精品99久久久久久老狼| 成人av在线看| 日韩欧美一区中文| 一区二区三区中文免费| 久久99国产精品麻豆| 99国产精品99久久久久久| 91精品国产欧美一区二区成人| 国产精品情趣视频| 蜜桃久久av一区| 欧美日韩日日摸| 国产精品短视频| 亚洲色图19p| 国产 日韩 欧美大片| 精品日韩一区二区| 亚洲国产一区二区视频| eeuss鲁片一区二区三区在线看| 日韩欧美aaaaaa| 五月天一区二区| 日本韩国欧美一区| 国产精品色呦呦| 国产精品一区二区三区99| 欧美一区二区三区成人| 亚洲风情在线资源站| 色综合色综合色综合色综合色综合| 久久综合色婷婷| 狠狠色丁香久久婷婷综| 日韩一区二区免费在线观看| 日韩中文字幕av电影| 在线免费观看成人短视频| 中文字幕视频一区| 成人免费看黄yyy456| 国产欧美日韩麻豆91| 国产精品亚洲一区二区三区妖精| 欧美α欧美αv大片| 日本中文字幕一区二区有限公司| 欧美性色aⅴ视频一区日韩精品| 亚洲男人的天堂一区二区 | 亚洲欧美激情一区二区| 99久久精品国产一区二区三区| 国产精品乱人伦|