锘??xml version="1.0" encoding="utf-8" standalone="yes"?>色综合久久无码中文字幕,区久久AAA片69亚洲,一级A毛片免费观看久久精品http://www.shnenglu.com/fenglin/category/14809.html鍒涙柊銆佸垱鎰忋佹寫鎴?/description>zh-cnFri, 10 Sep 2010 19:38:21 GMTFri, 10 Sep 2010 19:38:21 GMT60django妗嗘灦瀹夎涓庨厤緗?/title><link>http://www.shnenglu.com/fenglin/articles/126082.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Tue, 07 Sep 2010 04:16:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/126082.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/126082.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/126082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/126082.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/126082.html</trackback:ping><description><![CDATA[django鏄竴涓紑婧愭鏋墮」鐩紝璇︽儏璇瘋瀹樼綉http://www.djangoproject.com<img src ="http://www.shnenglu.com/fenglin/aggbug/126082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-09-07 12:16 <a href="http://www.shnenglu.com/fenglin/articles/126082.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>瀛ython涓ゅぉ鍗婃椂闂村啓鐨勬垚鏋?/title><link>http://www.shnenglu.com/fenglin/articles/125516.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Wed, 01 Sep 2010 05:11:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125516.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125516.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125516.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125516.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125516.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br>#</span><span style="COLOR: #008000">Filename:myAddressBook.py</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> os<br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">used cPlickle store</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> cPickle as p<br><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">#######class Person##############</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Person:<br>  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self,name,email):<br>    self.name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> name<br>    self.email </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> email<br><br>  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br>    </span><span style="COLOR: #0000ff">pass</span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">###############################</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000"><br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">######class AddressBook#######</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> AddressBook:<br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">the long name of data file</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  addressFile </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Address.data</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>  <br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">dictionary</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  addressList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">{}<br> <br>  <br>  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self):<br>    </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">If the data file of addressbook is not exists,then create it. </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">not</span><span style="COLOR: #000000"> os.path.isfile(AddressBook.addressFile):      <br>      f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file(AddressBook.addressFile,</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>      p.dump(AddressBook.addressList,f)<br>      f.close()<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Created file successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>      </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">If the data file of addressbook is exists,then list all.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">      f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file(AddressBook.addressFile)<br>      AddressBook.addressList </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> p.load(f)<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Current address list:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>      f.close()<br>      self.List()</span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">call List function</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  <br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">When destroy object,save data to file.</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br>    </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br>      </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:    <br>        f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file(AddressBook.addressFile,</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>        p.dump(AddressBook.addressList,f) <br>        </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Save data successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">  <br>      </span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">:<br>        </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Save data fail!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">finally</span><span style="COLOR: #000000">:<br>      f.close()<br>  <br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Add new record</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> Add(self,pepole):<br>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> len(pepole.name)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0:<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The length of name must longer than zero.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> len(pepole.email)</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">0:<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The length of emial must longer than zero.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>      pr </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person(pepole.name,pepole.email)<br>      AddressBook.addressList[pepole.name] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> pr<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Add new person successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Delete a record by name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> Del(self,name):<br>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> AddressBook.addressList.has_key(name):<br>      </span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000"> AddressBook.addressList[name]<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Deleted %s successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> name<br>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The name is not exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>  <br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Modify a record by name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> Edit(self,name,people):<br>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> AddressBook.addressList.has_key(name):<br>      AddressBook.addressList[name] </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> people<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Edit %s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s information successfull!</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000"> % name</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The name is not exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:Find a record by name</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> Find(self,name):<br>    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> AddressBook.addressList.has_key(name):<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s information </span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000"> :\nName:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s\nEmail:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">       </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> (name,\<br>       AddressBook.addressList[name].name,\<br>       AddressBook.addressList[name].email)<br>    </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">The name is not exists.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>  <br>  </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">purpose:List all record</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">  </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> List(self):<br>    </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">  </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Address List</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000">  </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000"> name,people </span><span style="COLOR: #0000ff">in</span><span style="COLOR: #000000"> AddressBook.addressList.items():<br>      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s\</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">s information </span><span style="COLOR: #0000ff">is</span><span style="COLOR: #000000"> :\nName:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s\nEmail:</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000"> % (name,people.name,people.email)</span><span style="COLOR: #800000"><br></span><span style="COLOR: #000000">      </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">-</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000"><br>    <br></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">###############main####################################    </span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>ad </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> AddressBook()<br></span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000"> True:<br>  </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Main menu</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">10</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000"><br>           1.Add new address<br>           2.Delete an address by name<br>           3.Edit an address by name<br>           4.Find an address by name  <br>           5.List all record<br>           6.Exit</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">\<br>   </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">\n</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">+</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">*</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">30</span><span style="COLOR: #000000"><br>  </span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">:<br>    choice </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> int(raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please choice:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">))<br>  </span><span style="COLOR: #0000ff">except</span><span style="COLOR: #000000">:<br>    os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Error:Please enter a number from 1 to 6,enter again.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br>    </span><span style="COLOR: #0000ff">continue</span><span style="COLOR: #000000"><br><br>  </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:<br>    name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    email </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input email:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    people </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person(name,email)<br>    ad.Add(people)<br>  </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">:<br>    name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    ad.Del(name)<br>  </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">:<br>    name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    newemail </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input new email:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    people </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person(name,newemail)<br>    ad.Edit(name,people)<br>  </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">:<br>    name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> raw_input(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Please input name:</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br>    ad.Find(name)<br>  </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">:<br>    ad.List()<br>  </span><span style="COLOR: #0000ff">elif</span><span style="COLOR: #000000"> choice </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">6</span><span style="COLOR: #000000">:<br>    </span><span style="COLOR: #0000ff">break</span><span style="COLOR: #000000"><br>  </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br>    os.system(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">clear</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)    <br>    <br>     </span></div> <br> <img src ="http://www.shnenglu.com/fenglin/aggbug/125516.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-09-01 13:11 <a href="http://www.shnenglu.com/fenglin/articles/125516.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>repr鍑芥暟http://www.shnenglu.com/fenglin/articles/125483.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:44:00 GMThttp://www.shnenglu.com/fenglin/articles/125483.htmlhttp://www.shnenglu.com/fenglin/comments/125483.htmlhttp://www.shnenglu.com/fenglin/articles/125483.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125483.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125483.htmlrepr鍑芥暟鐢ㄦ潵鍙栧緱瀵硅薄鐨勮鑼冨瓧絎︿覆琛ㄧず銆?/span>鍙嶅紩鍙鳳紙涔熺О杞崲絎︼級鍙互瀹屾垚鐩稿悓鐨勫姛鑳姐?/span>娉?br>鎰忥紝鍦ㄥぇ澶氭暟鏃跺欐湁eval(repr(object)) == object銆?br>>>> i = []
>>> i.append('item')
>>> `i`
"['item']"
>>> repr(i)
"['item']"
 
鍩烘湰涓婏紝repr鍑芥暟鍜屽弽寮曞彿鐢ㄦ潵鑾峰彇瀵硅薄鐨勫彲鎵撳嵃鐨勮〃紺哄艦寮忋備綘鍙互閫氳繃瀹氫箟綾葷殑__repr__
鏂規(guī)硶鏉ユ帶鍒朵綘鐨勫璞″湪琚玶epr鍑芥暟璋冪敤鐨勬椂鍊欒繑鍥炵殑鍐呭銆?

椋庢灄 2010-09-01 08:44 鍙戣〃璇勮
]]>
assert璇彞http://www.shnenglu.com/fenglin/articles/125482.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:39:00 GMThttp://www.shnenglu.com/fenglin/articles/125482.htmlhttp://www.shnenglu.com/fenglin/comments/125482.htmlhttp://www.shnenglu.com/fenglin/articles/125482.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125482.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125482.htmlassert璇彞鐢ㄦ潵澹版槑鏌愪釜鏉′歡鏄湡鐨勩?br>渚嬪錛屽鏋滀綘闈炲父紜俊鏌愪釜浣犱嬌鐢ㄧ殑鍒楄〃涓嚦灝戞湁涓涓?br>鍏冪礌錛岃屼綘鎯寵媯楠岃繖涓鐐癸紝騫朵笖鍦ㄥ畠闈炵湡鐨勬椂鍊欏紩鍙戜竴涓敊璇紝閭d箞assert璇彞鏄簲鐢ㄥ湪榪?br>縐嶆儏褰笅鐨勭悊鎯寵鍙ャ傚綋assert璇彞澶辮觸鐨勬椂鍊欙紝浼?xì)寮曞彂涓涓狝ssertionError銆?br>>>> mylist = ['item']
>>> assert len(mylist) >= 1
>>> mylist.pop()
'item'
>>> assert len(mylist) >= 1
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
AssertionError

椋庢灄 2010-09-01 08:39 鍙戣〃璇勮
]]>
exec鍜宔val璇彞http://www.shnenglu.com/fenglin/articles/125481.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:36:00 GMThttp://www.shnenglu.com/fenglin/articles/125481.htmlhttp://www.shnenglu.com/fenglin/comments/125481.htmlhttp://www.shnenglu.com/fenglin/articles/125481.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125481.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125481.htmlexec璇彞鐢ㄦ潵鎵ц鍌ㄥ瓨鍦ㄥ瓧絎︿覆鎴栨枃浠朵腑鐨凱ython璇彞銆?/u>渚嬪錛屾垜浠彲浠ュ湪榪愯鏃剁敓鎴愪竴涓寘
鍚玃ython浠g爜鐨勫瓧絎︿覆錛岀劧鍚庝嬌鐢╡xec璇彞鎵ц榪欎簺璇彞銆備笅闈㈡槸涓涓畝鍗曠殑渚嬪瓙銆?br>>>> exec 'print "Hello World"'
Hello World

eval璇彞鐢ㄦ潵璁$畻瀛樺偍鍦ㄥ瓧絎︿覆涓殑鏈夋晥Python琛ㄨ揪寮忋?/strong>涓嬮潰鏄竴涓畝鍗曠殑渚嬪瓙銆?br>>>> eval('2*3')

椋庢灄 2010-09-01 08:36 鍙戣〃璇勮
]]>
lambda褰㈠紡http://www.shnenglu.com/fenglin/articles/125480.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:33:00 GMThttp://www.shnenglu.com/fenglin/articles/125480.htmlhttp://www.shnenglu.com/fenglin/comments/125480.htmlhttp://www.shnenglu.com/fenglin/articles/125480.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125480.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125480.html
#!/usr/bin/python
#
 Filename: lambda.py
def make_repeater(n):
    
return lambda s: s*n
twice 
= make_repeater(2)
print twice('word')
print twice(5
杈撳嚭
$ python lambda.py
wordword
10
 
瀹冨浣曞伐浣?/strong>
榪欓噷錛屾垜浠嬌鐢ㄤ簡make_repeater鍑芥暟鍦ㄨ繍琛屾椂鍒涘緩鏂扮殑鍑芥暟瀵硅薄錛屽茍涓旇繑鍥炲畠銆俵ambda璇彞鐢?br>鏉ュ垱寤哄嚱鏁板璞°傛湰璐ㄤ笂錛宭ambda闇瑕佷竴涓弬鏁幫紝鍚庨潰浠呰窡鍗曚釜琛ㄨ揪寮忎綔涓哄嚱鏁頒綋錛岃岃〃杈懼紡
鐨勫艱榪欎釜鏂板緩鐨勫嚱鏁拌繑鍥炪?span style="COLOR: red">娉ㄦ剰錛屽嵆渚挎槸print璇彞涔熶笉鑳界敤鍦╨ambda褰㈠紡涓紝鍙兘浣跨敤琛ㄨ揪
寮忋?/span>

椋庢灄 2010-09-01 08:33 鍙戣〃璇勮
]]>
鍦ㄥ嚱鏁頒腑鎺ユ敹鍏冪粍鍜屽垪琛?/title><link>http://www.shnenglu.com/fenglin/articles/125479.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Wed, 01 Sep 2010 00:28:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125479.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125479.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125479.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125479.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125479.html</trackback:ping><description><![CDATA[褰撹浣垮嚱鏁版帴鏀跺厓緇勬垨瀛楀吀褰㈠紡鐨勫弬鏁扮殑鏃跺欙紝鏈変竴縐嶇壒孌婄殑鏂規(guī)硶錛屽畠鍒嗗埆<span style="COLOR: red">浣跨敤*鍜?*鍓嶇紑</span>銆?br>榪欑鏂規(guī)硶鍦ㄥ嚱鏁伴渶瑕佽幏鍙栧彲鍙樻暟閲忕殑鍙傛暟鐨勬椂鍊欑壒鍒湁鐢ㄣ?br>>>> def powersum(power, *args):<br>...     '''Return the sum of each argument raised to specified power.'''<br>...     total = 0<br>...     for i in args:<br>...          total += pow(i, power)<br>...     return total<br>...<br>>>> powersum(2, 3, 4)<br>25<br>>>> powersum(2, 10)<br>100 <br><span style="COLOR: red">鐢變簬鍦╝rgs鍙橀噺鍓嶆湁*鍓嶇紑錛屾墍鏈夊浣欑殑鍑芥暟鍙傛暟閮戒細(xì)浣滀負(fù)涓涓厓緇勫瓨鍌ㄥ湪args涓傚鏋滀嬌鐢ㄧ殑<br>鏄?*鍓嶇紑錛屽浣欑殑鍙傛暟鍒欎細(xì)琚涓烘槸涓涓瓧鍏哥殑閿?鍊煎銆?/span> <img src ="http://www.shnenglu.com/fenglin/aggbug/125479.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-09-01 08:28 <a href="http://www.shnenglu.com/fenglin/articles/125479.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浣跨敤鍒楄〃緇煎悎http://www.shnenglu.com/fenglin/articles/125478.html椋庢灄椋庢灄Wed, 01 Sep 2010 00:24:00 GMThttp://www.shnenglu.com/fenglin/articles/125478.htmlhttp://www.shnenglu.com/fenglin/comments/125478.htmlhttp://www.shnenglu.com/fenglin/articles/125478.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125478.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125478.html#!/usr/bin/python
#
 Filename: list_comprehension.py
listone = [234]
listtwo 
= [2*for i in listone if i > 2]
print listtwo 
杈撳嚭
$ python list_comprehension.py
[6, 8]

瀹冨浣曞伐浣?/strong>
榪欓噷鎴戜滑涓烘弧瓚蟲潯浠訛紙if i > 2錛夌殑鏁版寚瀹氫簡涓涓搷浣滐紙2*i錛夛紝浠庤屽鍑轟竴涓柊鐨勫垪琛ㄣ傛敞鎰忓師
鏉ョ殑鍒楄〃騫舵病鏈夊彂鐢熷彉鍖栥傚湪寰堝鏃跺欙紝鎴戜滑閮芥槸浣跨敤寰幆鏉ュ鐞嗗垪琛ㄤ腑鐨勬瘡涓涓厓绱狅紝鑰屼嬌
鐢ㄥ垪琛ㄧ患鍚堝彲浠ョ敤涓縐嶆洿鍔犵簿紜佺畝媧併佹竻妤氱殑鏂規(guī)硶瀹屾垚鐩稿悓鐨勫伐浣溿?

椋庢灄 2010-09-01 08:24 鍙戣〃璇勮
]]>
vim蹇嵎閿誨浘http://www.shnenglu.com/fenglin/articles/125433.html椋庢灄椋庢灄Tue, 31 Aug 2010 10:21:00 GMThttp://www.shnenglu.com/fenglin/articles/125433.htmlhttp://www.shnenglu.com/fenglin/comments/125433.htmlhttp://www.shnenglu.com/fenglin/articles/125433.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125433.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125433.html

椋庢灄 2010-08-31 18:21 鍙戣〃璇勮
]]>
鐗規(guī)畩鐨勬柟娉?/title><link>http://www.shnenglu.com/fenglin/articles/125431.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Tue, 31 Aug 2010 10:09:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125431.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125431.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125431.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125431.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125431.html</trackback:ping><description><![CDATA[琛?5.1 涓浜涚壒孌婄殑鏂規(guī)硶<br>鍚嶇О 璇存槑<br>__init__(self,...) 榪欎釜鏂規(guī)硶鍦ㄦ柊寤哄璞℃伆濂借琚繑鍥炰嬌鐢ㄤ箣鍓嶈璋冪敤銆?br>__del__(self) 鎭板ソ鍦ㄥ璞¤琚垹闄や箣鍓嶈皟鐢ㄣ?br>__str__(self) 鍦ㄦ垜浠瀵硅薄浣跨敤print璇彞鎴栨槸浣跨敤str()鐨勬椂鍊欒皟鐢ㄣ?br>__lt__(self,other)褰撲嬌鐢?灝忎簬 榪愮畻絎︼紙<錛夌殑鏃跺欒皟鐢ㄣ傜被浼煎湴錛屽浜庢墍鏈夌殑榪愮畻絎?br>錛?錛?gt;絳夌瓑錛夐兘鏈夌壒孌婄殑鏂規(guī)硶銆?br>__getitem__(self,key)浣跨敤x[key]绱㈠紩鎿嶄綔絎︾殑鏃跺欒皟鐢ㄣ?br>__len__(self) 瀵瑰簭鍒楀璞′嬌鐢ㄥ唴寤虹殑len()鍑芥暟鐨勬椂鍊欒皟鐢ㄣ? <img src ="http://www.shnenglu.com/fenglin/aggbug/125431.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-08-31 18:09 <a href="http://www.shnenglu.com/fenglin/articles/125431.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>sys妯″潡http://www.shnenglu.com/fenglin/articles/125429.html椋庢灄椋庢灄Tue, 31 Aug 2010 10:06:00 GMThttp://www.shnenglu.com/fenglin/articles/125429.htmlhttp://www.shnenglu.com/fenglin/comments/125429.htmlhttp://www.shnenglu.com/fenglin/articles/125429.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125429.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125429.html#!/usr/bin/python
#
 Filename: cat.py
import sys
def readfile(filename):
    
'''Print a file to the standard output.'''
    f 
= file(filename)
    
while True:
        line 
= f.readline()
        
if len(line) == 0:
            
break
        
print line, # notice comma
    f.close()
# Script starts from here
if len(sys.argv) < 2:
    
print 'No action specified.'
    sys.exit()
if sys.argv[1].startswith('--'):
    option 
= sys.argv[1][2:]
    
# fetch sys.argv[1] but without the first two characters
    if option == 'version':
        
print 'Version 1.2'
    
elif option == 'help':
        
print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
  --version : Prints the version number
  --help    : Display this help
'''
    
else:
        
print 'Unknown option.'
    sys.exit()
else:
    
for filename in sys.argv[1:]:
        readfile(filename)
杈撳嚭
$ python cat.py
No action specified.
$ python cat.py --help
This program prints files to the standard output.
Any number of files can be specified.
Options include:
  --version : Prints the version number
  --help    : Display this help
$ python cat.py --version
Version 1.2
$ python cat.py --nonsense
Unknown option.
$ python cat.py poem.txt
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!

椋庢灄 2010-08-31 18:06 鍙戣〃璇勮
]]>
os妯″潡http://www.shnenglu.com/fenglin/articles/125428.html椋庢灄椋庢灄Tue, 31 Aug 2010 10:04:00 GMThttp://www.shnenglu.com/fenglin/articles/125428.htmlhttp://www.shnenglu.com/fenglin/comments/125428.htmlhttp://www.shnenglu.com/fenglin/articles/125428.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125428.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125428.html涓洪噸瑕佺殑銆傚嵆瀹冨厑璁鎬竴涓▼搴忓湪緙栧啓鍚庝笉闇瑕佷換浣曟敼鍔紝涔熶笉浼?xì)鍙戠敓鋼Q浣曢棶棰橈紝灝卞彲浠ュ湪
Linux鍜學(xué)indows涓嬭繍琛屻備竴涓緥瀛愬氨鏄嬌鐢╫s.sep鍙互鍙栦唬鎿嶄綔緋葷粺鐗瑰畾鐨勮礬寰勫垎鍓茬銆?br>涓嬮潰鍒楀嚭浜嗕竴浜涘湪os妯″潡涓瘮杈冩湁鐢ㄧ殑閮ㄥ垎銆傚畠浠腑鐨勫ぇ澶氭暟閮界畝鍗曟槑浜嗐?br>鈼?nbsp;    os.name瀛楃涓叉寚紺轟綘姝e湪浣跨敤鐨勫鉤鍙般傛瘮濡傚浜嶹indows錛屽畠鏄?nt'錛岃屽浜嶭inux/Unix
鐢ㄦ埛錛屽畠鏄?posix'銆?br>鈼?nbsp;    os.getcwd()鍑芥暟寰楀埌褰撳墠宸ヤ綔鐩綍錛屽嵆褰撳墠Python鑴氭湰宸ヤ綔鐨勭洰褰曡礬寰勩?br>鈼?nbsp;    os.getenv()鍜宱s.putenv()鍑芥暟鍒嗗埆鐢ㄦ潵璇誨彇鍜岃緗幆澧冨彉閲忋?br>鈼?nbsp;    os.listdir()榪斿洖鎸囧畾鐩綍涓嬬殑鎵鏈夋枃浠跺拰鐩綍鍚嶃?br>鈼?nbsp;    os.remove()鍑芥暟鐢ㄦ潵鍒犻櫎涓涓枃浠躲?br>鈼?nbsp;    os.system()鍑芥暟鐢ㄦ潵榪愯shell鍛戒護(hù)銆?br>鈼?nbsp;    os.linesep瀛楃涓茬粰鍑哄綋鍓嶅鉤鍙頒嬌鐢ㄧ殑琛岀粓姝㈢銆備緥濡傦紝W(xué)indows浣跨敤'\r\n'錛孡inux浣?br>鐢?\n'鑰孧ac浣跨敤'\r'銆?br>鈼?nbsp;    os.path.split()鍑芥暟榪斿洖涓涓礬寰勭殑鐩綍鍚嶅拰鏂囦歡鍚嶃?br>>>> os.path.split('/home/swaroop/byte/code/poem.txt')
('/home/swaroop/byte/code', 'poem.txt')
鈼?nbsp;    os.path.isfile()鍜宱s.path.isdir()鍑芥暟鍒嗗埆媯楠岀粰鍑虹殑璺緞鏄竴涓枃浠惰繕鏄洰褰曘傜被浼煎湴錛宱s.
path.exists()鍑芥暟鐢ㄦ潵媯楠岀粰鍑虹殑璺緞鏄惁鐪熷湴瀛樺湪銆?

椋庢灄 2010-08-31 18:04 鍙戣〃璇勮
]]>
try..finallyhttp://www.shnenglu.com/fenglin/articles/125423.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:19:00 GMThttp://www.shnenglu.com/fenglin/articles/125423.htmlhttp://www.shnenglu.com/fenglin/comments/125423.htmlhttp://www.shnenglu.com/fenglin/articles/125423.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125423.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125423.html鍙互浣跨敤finally鍧楁潵瀹屾垚銆傛敞鎰忥紝鍦ㄤ竴涓猼ry鍧椾笅錛屼綘鍙互鍚屾椂浣跨敤except浠庡彞鍜宖inally鍧椼傚鏋?br>浣犺鍚屾椂浣跨敤瀹冧滑鐨勮瘽錛岄渶瑕佹妸涓涓祵鍏ュ彟澶栦竴涓?br>
#!/usr/bin/python
#
 Filename: finally.py
import time
try:
    f 
= file('poem.txt')
    
while True: # our usual file-reading idiom
        line = f.readline()
        
if len(line) == 0:
            
break
        time.sleep(
2)
        
print line,
finally:
    f.close()
    
print 'Cleaning upclosed the file' 
杈撳嚭
$ python finally.py
Programming is fun
When the work is done
Cleaning up...closed the file
Traceback (most recent call last):
  File "finally.py", line 12, in ?
    time.sleep(2)
KeyboardInterrupt

瀹冨浣曞伐浣?/strong>
鎴戜滑榪涜閫氬父鐨勮鏂囦歡宸ヤ綔錛屼絾鏄垜鏈夋剰鍦ㄦ瘡鎵撳嵃涓琛屼箣鍓嶇敤time.sleep鏂規(guī)硶鏆傚仠2縐掗挓銆傝繖鏍?br>鍋氱殑鍘熷洜鏄紼嬪簭榪愯寰楁參涓浜涳紙Python鐢變簬鍏舵湰璐ㄩ氬父榪愯寰楀緢蹇級銆傚湪紼嬪簭榪愯鐨勬椂鍊欙紝
鎸塁trl-c涓柇/鍙栨秷紼嬪簭銆?br>鎴戜滑鍙互瑙傚療鍒癒eyboardInterrupt寮傚父琚Е鍙戯紝紼嬪簭閫鍑恒備絾鏄湪紼嬪簭閫鍑轟箣鍓嶏紝finally浠庡彞浠?br>鐒惰鎵ц錛屾妸鏂囦歡鍏抽棴

椋庢灄 2010-08-31 17:19 鍙戣〃璇勮
]]>
raise 寮曞彂寮傚父http://www.shnenglu.com/fenglin/articles/125421.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:12:00 GMThttp://www.shnenglu.com/fenglin/articles/125421.htmlhttp://www.shnenglu.com/fenglin/comments/125421.htmlhttp://www.shnenglu.com/fenglin/articles/125421.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125421.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125421.html鍙互寮曞彂鐨勯敊璇垨寮傚父搴旇鍒嗗埆鏄竴涓狤rror鎴朎xception綾葷殑鐩存帴鎴栭棿鎺ュ鍑虹被銆?br>
#!/usr/bin/python
#
 Filename: raising.py
class ShortInputException(Exception):
    
'''A user-defined exception class.'''
    
def __init__(self, length, atleast):
        Exception.
__init__(self)
        self.length 
= length
        self.atleast 
= atleast
try:
    s 
= raw_input('Enter something --> ')
    
if len(s) < 3:
        
raise ShortInputException(len(s), 3)
    
# Other work can continue as usual here
except EOFError:
    
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
    
print 'ShortInputException: The input was of length %d, \
          was expecting at least %d' % (x.length, x.atleast)
else:
    
print 'No exception was raised.' 
杈撳嚭
$ python raising.py
Enter something -->
Why did you do an EOF on me?
$ python raising.py
Enter something --> ab
ShortInputException: The input was of length 2, was expecting at least 3
$ python raising.py
Enter something --> abc
No exception was raised.

瀹冨浣曞伐浣?/strong>
榪欓噷錛屾垜浠垱寤轟簡鎴戜滑鑷繁鐨勫紓甯哥被鍨嬶紝鍏跺疄鎴戜滑鍙互浣跨敤浠諱綍棰勫畾涔夌殑寮傚父/閿欒銆傝繖涓柊
鐨勫紓甯哥被鍨嬫槸ShortInputException綾匯傚畠鏈変袱涓煙鈥斺攍ength鏄粰瀹氳緭鍏ョ殑闀垮害錛宎tleast鍒欐槸紼嬪簭
鏈熸湜鐨勬渶灝忛暱搴︺?br>鍦╡xcept浠庡彞涓紝鎴戜滑鎻愪緵浜嗛敊璇被鍜岀敤鏉ヨ〃紺洪敊璇?寮傚父瀵硅薄鐨勫彉閲忋傝繖涓庡嚱鏁拌皟鐢ㄤ腑鐨勫艦鍙?br>鍜屽疄鍙傛蹇電被浼箋傚湪榪欎釜鐗瑰埆鐨別xcept浠庡彞涓紝鎴戜滑浣跨敤寮傚父瀵硅薄鐨刲ength鍜宎tleast鍩熸潵涓虹敤鎴?br>鎵撳嵃涓涓伆褰撶殑娑堟伅銆?

椋庢灄 2010-08-31 17:12 鍙戣〃璇勮
]]>
澶勭悊寮傚父http://www.shnenglu.com/fenglin/articles/125420.html椋庢灄椋庢灄Tue, 31 Aug 2010 09:03:00 GMThttp://www.shnenglu.com/fenglin/articles/125420.htmlhttp://www.shnenglu.com/fenglin/comments/125420.htmlhttp://www.shnenglu.com/fenglin/articles/125420.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125420.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125420.html鐞嗚鍙ユ斁鍦╡xcept-鍧椾腑銆?br>
#!/usr/bin/python
#
 Filename: try_except.py
import sys
try:
    s 
= raw_input('Enter something --> ')
except EOFError:
    
print '\nWhy did you do an EOF on me?'
    sys.exit() 
# exit the program
except:
    
print '\nSome error/exception occurred.'
    
# here, we are not exiting the program
print 'Done' 
杈撳嚭
$ python try_except.py
Enter something -->
Why did you do an EOF on me?
$ python try_except.py
Enter something --> Python is exceptional!
Done

瀹冨浣曞伐浣?br>鎴戜滑鎶婃墍鏈夊彲鑳藉紩鍙戦敊璇殑璇彞鏀懼湪try鍧椾腑錛岀劧鍚庡湪except浠庡彞/鍧椾腑澶勭悊鎵鏈夌殑閿欒鍜屽紓甯搞?br>except浠庡彞鍙互涓撻棬澶勭悊鍗曚竴鐨勯敊璇垨寮傚父錛屾垨鑰呬竴緇勫寘鎷湪鍦嗘嫭鍙峰唴鐨勯敊璇?寮傚父銆傚鏋滄病鏈?br>緇欏嚭閿欒鎴栧紓甯哥殑鍚嶇О錛屽畠浼?xì)澶勭?鎵鏈夌殑 閿欒鍜屽紓甯搞傚浜庢瘡涓猼ry浠庡彞錛岃嚦灝戦兘鏈変竴涓浉鍏?br>鑱旂殑except浠庡彞銆?br>濡傛灉鏌愪釜閿欒鎴栧紓甯告病鏈夎澶勭悊錛岄粯璁ょ殑Python澶勭悊鍣ㄥ氨浼?xì)琚皟鐢ㄣ傚畠浼?xì)缁堟绋嬪簭鐨剺q愯錛屽茍
涓旀墦鍗頒竴涓秷鎭紝鎴戜滑宸茬粡鐪嬪埌浜嗚繖鏍風(fēng)殑澶勭悊銆?br>

椋庢灄 2010-08-31 17:03 鍙戣〃璇勮
]]>
鍌ㄥ瓨鍣?/title><link>http://www.shnenglu.com/fenglin/articles/125418.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Tue, 31 Aug 2010 08:54:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125418.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125418.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125418.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125418.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125418.html</trackback:ping><description><![CDATA[Python鎻愪緵涓涓爣鍑嗙殑妯″潡錛岀О涓簆ickle銆備嬌鐢ㄥ畠浣犲彲浠ュ湪涓涓枃浠朵腑鍌ㄥ瓨浠諱綍Python瀵硅薄錛屼箣<br>鍚庝綘鍙堝彲浠ユ妸瀹冨畬鏁存棤緙哄湴鍙栧嚭鏉ャ傝繖琚О涓?鎸佷箙鍦?鍌ㄥ瓨瀵硅薄銆?br>榪樻湁鍙︿竴涓ā鍧楃О涓?span style="COLOR: red">cPickle</span>錛屽畠鐨勫姛鑳藉拰pickle妯″潡瀹屽叏鐩稿悓錛屽彧涓嶈繃瀹冩槸鐢–璇█緙栧啓鐨勶紝鍥?br>姝よ蹇緱澶氾紙<span style="COLOR: red">姣攑ickle蹇?000鍊?/span>錛夈備綘鍙互浣跨敤瀹冧滑涓殑浠諱竴涓紝鑰屾垜浠湪榪欓噷灝嗕嬌鐢╟Pickle妯?br>鍧椼傝浣忥紝<span style="COLOR: red">鎴戜滑鎶婅繖涓や釜妯″潡閮界畝縐頒負(fù)pickle妯″潡</span>銆?br> <div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000"> Filename: pickling.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">import</span><span style="COLOR: #000000"> cPickle as p<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000">import pickle as p</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplistfile </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">shoplist.data</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> the name of the file where we will store the object</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">shoplist </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> [</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">apple</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">, </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">mango</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">, </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">carrot</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">]<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> Write to the file</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file(shoplistfile, </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">w</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">p.dump(shoplist, f) </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> dump the object to a file</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f.close()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">del</span><span style="COLOR: #000000"> shoplist </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> remove the shoplist</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000"> Read back from the storage</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">f </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> file(shoplistfile)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">storedlist </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> p.load(f)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> storedlist </span></div> <strong>杈撳嚭</strong><br>$ python pickling.py<br>['apple', 'mango', 'carrot'] <br><br><strong>瀹冨浣曞伐浣?/strong><br>棣栧厛錛岃娉ㄦ剰鎴戜滑浣跨敤浜唅mport..as璇硶銆傝繖鏄竴縐嶄究鍒╂柟娉曪紝浠ヤ究浜庢垜浠彲浠ヤ嬌鐢ㄦ洿鐭殑妯″潡<br>鍚嶇О銆傚湪榪欎釜渚嬪瓙涓紝瀹冭繕璁╂垜浠兘澶熼氳繃綆鍗曞湴鏀瑰彉涓琛屽氨鍒囨崲鍒板彟涓涓ā鍧楋紙cPickle鎴栬?br>pickle錛夛紒鍦ㄧ▼搴忕殑鍏朵綑閮ㄥ垎鐨勬椂鍊欙紝鎴戜滑綆鍗曞湴鎶婅繖涓ā鍧楃О涓簆銆?br>涓轟簡鍦ㄦ枃浠墮噷鍌ㄥ瓨涓涓璞★紝棣栧厛浠ュ啓妯″紡鎵撳紑涓涓猣ile瀵硅薄錛岀劧鍚庤皟鐢ㄥ偍瀛樺櫒妯″潡鐨刣ump鍑?br>鏁幫紝鎶婂璞″偍瀛樺埌鎵撳紑鐨勬枃浠朵腑銆傝繖涓繃紼嬬О涓?鍌ㄥ瓨 銆?br>鎺ヤ笅鏉ワ紝鎴戜滑浣跨敤pickle妯″潡鐨刲oad鍑芥暟鐨勮繑鍥炴潵鍙栧洖瀵硅薄銆傝繖涓繃紼嬬О涓?鍙栧偍瀛?銆? <img src ="http://www.shnenglu.com/fenglin/aggbug/125418.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-08-31 16:54 <a href="http://www.shnenglu.com/fenglin/articles/125418.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鏂囦歡http://www.shnenglu.com/fenglin/articles/125415.html椋庢灄椋庢灄Tue, 31 Aug 2010 08:33:00 GMThttp://www.shnenglu.com/fenglin/articles/125415.htmlhttp://www.shnenglu.com/fenglin/comments/125415.htmlhttp://www.shnenglu.com/fenglin/articles/125415.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125415.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125415.html鎭板綋鍦拌鍐欐枃浠躲傚鏂囦歡鐨勮鍐欒兘鍔涗緷璧栦簬浣犲湪鎵撳紑鏂囦歡鏃舵寚瀹氱殑妯″紡銆傛渶鍚庯紝褰撲綘瀹屾垚瀵規(guī)枃
浠剁殑鎿嶄綔鐨勬椂鍊欙紝浣犺皟鐢╟lose鏂規(guī)硶鏉ュ憡璇塒ython鎴戜滑瀹屾垚浜嗗鏂囦歡鐨勪嬌鐢ㄣ?br>
#!/usr/bin/python
#
 Filename: using_file.py
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!
'''
= file('poem.txt''w'# open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
= file('poem.txt')
# if no mode is specified, 'r'ead mode is assumed by default
while True:
    line 
= f.readline()
    
if len(line) == 0: # Zero length indicates EOF
        break
    
print line,
    
# Notice comma to avoid automatic newline added by Python
f.close() # close the file 
杈撳嚭
$ python using_file.py
Programming is fun
When the work is done
if you wanna make your work also fun:
        use Python!

瀹冨浣曞伐浣?br>棣栧厛錛屾垜浠氳繃鎸囨槑鎴戜滑甯屾湜鎵撳紑鐨勬枃浠跺拰妯″紡鏉ュ垱寤轟竴涓猣ile綾葷殑瀹炰緥銆傛ā寮忓彲浠ヤ負(fù)璇繪ā寮?br>錛?r'錛夈佸啓妯″紡錛?w'錛夋垨榪藉姞妯″紡錛?a'錛夈備簨瀹炰笂榪樻湁澶氬緱澶氱殑妯″紡鍙互浣跨敤錛屼綘鍙互浣跨敤
help(file)鏉ヤ簡瑙e畠浠殑璇︽儏銆?br>鎴戜滑棣栧厛鐢ㄥ啓妯″紡鎵撳紑鏂囦歡錛岀劧鍚庝嬌鐢╢ile綾葷殑write鏂規(guī)硶鏉ュ啓鏂囦歡錛屾渶鍚庢垜浠敤close鍏抽棴榪欎釜鏂?br>浠躲?

椋庢灄 2010-08-31 16:33 鍙戣〃璇勮
]]>
浣跨敤緇ф壙http://www.shnenglu.com/fenglin/articles/125414.html椋庢灄椋庢灄Tue, 31 Aug 2010 08:23:00 GMThttp://www.shnenglu.com/fenglin/articles/125414.htmlhttp://www.shnenglu.com/fenglin/comments/125414.htmlhttp://www.shnenglu.com/fenglin/articles/125414.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125414.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125414.html#!/usr/bin/python
#
 Filename: inherit.py
class SchoolMember:
    
'''Represents any school member.'''
    
def __init__(self, name, age):
        self.name 
= name
        self.age 
= age
        
print '(Initialized SchoolMember: %s)' % self.name
    
def tell(self):
        
'''Tell my details.'''
        
print 'Name:"%s" Age:"%s"' % (self.name, self.age),
class Teacher(SchoolMember):
    
'''Represents a teacher.'''
    
def __init__(self, name, age, salary):
        SchoolMember.
__init__(self, name, age)
        self.salary 
= salary
        
print '(Initialized Teacher: %s)' % self.name
    
def tell(self):
        SchoolMember.tell(self)
        
print 'Salary: "%d"' % self.salary
class Student(SchoolMember):
    
'''Represents a student.'''
    
def __init__(self, name, age, marks):
        SchoolMember.
__init__(self, name, age)
        self.marks 
= marks
        
print '(Initialized Student: %s)' % self.name
    
def tell(self):
        SchoolMember.tell(self)
        
print 'Marks: "%d"' % self.marks
= Teacher('Mrs. Shrividya'4030000)
= Student('Swaroop'2275)
print # prints a blank line
members = [t, s]
for member in members:
    member.tell() 
# works for both Teachers and Students  杈撳嚭
$ python inherit.py
(Initialized SchoolMember: Mrs. Shrividya)
(Initialized Teacher: Mrs. Shrividya)
(Initialized SchoolMember: Swaroop)
(Initialized Student: Swaroop)
Name:"Mrs. Shrividya" Age:"40" Salary: "30000"
Name:"Swaroop" Age:"22" Marks: "75"

椋庢灄 2010-08-31 16:23 鍙戣〃璇勮
]]>
浣跨敤綾諱笌瀵硅薄鐨勫彉閲?/title><link>http://www.shnenglu.com/fenglin/articles/125411.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Tue, 31 Aug 2010 08:04:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125411.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125411.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125411.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125411.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125411.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000"> Filename: objvar.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Person:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Represents a person.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    population </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> 0<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__init__</span><span style="COLOR: #000000">(self, name):<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Initializes the person's data.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        self.name </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> name<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">(Initializing %s)</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> self.name<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> When this person is created, he/she</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">        </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> adds to the population</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">        Person.population </span><span style="COLOR: #000000">+=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> </span><span style="COLOR: #800080">__del__</span><span style="COLOR: #000000">(self):<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">I am dying.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">%s says bye.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> self.name<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        Person.population </span><span style="COLOR: #000000">-=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> Person.population </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> 0:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">            </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I am the last one.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">            </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">There are still %d people left.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> Person.population<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> sayHi(self):<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Greeting by the person.<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        Really, that's all it does.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Hi, my name is %s.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> self.name<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #0000ff">def</span><span style="COLOR: #000000"> howMany(self):<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #800000">'''</span><span style="COLOR: #800000">Prints the current population.</span><span style="COLOR: #800000">'''</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> Person.population </span><span style="COLOR: #000000">==</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">            </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">I am the only person here.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">        </span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000">:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">            </span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> </span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">We have %d persons here.</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">%</span><span style="COLOR: #000000"> Person.population<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">swaroop </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Swaroop</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">swaroop.sayHi()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">swaroop.howMany()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">kalam </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person(</span><span style="COLOR: #800000">'</span><span style="COLOR: #800000">Abdul Kalam</span><span style="COLOR: #800000">'</span><span style="COLOR: #000000">)<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">kalam.sayHi()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">kalam.howMany()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">swaroop.sayHi()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">swaroop.howMany() </span></div> <strong>杈撳嚭</strong><br>$ python objvar.py<br>(Initializing Swaroop)<br>Hi, my name is Swaroop.<br>I am the only person here.<br>(Initializing Abdul Kalam)<br>Hi, my name is Abdul Kalam.<br>We have 2 persons here.<br>Hi, my name is Swaroop.<br>We have 2 persons here.<br>Abdul Kalam says bye.<br>There are still 1 people left.<br>Swaroop says bye.<br>I am the last one. <br><br><strong>瀹冨浣曞伐浣?/strong><br>榪欐槸涓涓緢闀跨殑渚嬪瓙錛屼絾鏄畠鏈夊姪浜庤鏄庣被涓庡璞$殑鍙橀噺鐨勬湰璐ㄣ傝繖閲岋紝population灞炰簬Person<br>綾伙紝鍥犳鏄竴涓被鐨勫彉閲忋俷ame鍙橀噺灞炰簬瀵硅薄錛堝畠浣跨敤self璧嬪鹼級鍥犳鏄璞$殑鍙橀噺銆?br>瑙傚療鍙互鍙戠幇__init__鏂規(guī)硶鐢ㄤ竴涓悕瀛楁潵鍒濆鍖朠erson瀹炰緥銆傚湪榪欎釜鏂規(guī)硶涓紝鎴戜滑璁﹑opulation<br>澧炲姞1錛岃繖鏄洜涓烘垜浠鍔犱簡涓涓漢銆傚悓鏍峰彲浠ュ彂鐜幫紝self.name鐨勫兼牴鎹瘡涓璞℃寚瀹氾紝榪欒〃<br>鏄庝簡瀹冧綔涓哄璞$殑鍙橀噺鐨勬湰璐ㄣ?br>璁頒綇錛屼綘鍙兘浣跨敤self鍙橀噺鏉ュ弬鑰冨悓涓涓璞$殑鍙橀噺鍜屾柟娉曘傝繖琚О涓?灞炴у弬鑰?銆?br>鍦ㄨ繖涓▼搴忎腑錛屾垜浠繕鐪嬪埌docstring瀵逛簬綾誨拰鏂規(guī)硶鍚屾牱鏈夌敤銆傛垜浠彲浠ュ湪榪愯鏃朵嬌鐢≒erson.<br>__doc__鍜孭erson.sayHi.__doc__鏉ュ垎鍒闂被涓庢柟娉曠殑鏂囨。瀛楃涓層?br>灝卞鍚宊_init__鏂規(guī)硶涓鏍鳳紝榪樻湁涓涓壒孌婄殑鏂規(guī)硶__del__錛屽畠鍦ㄥ璞℃秷閫濈殑鏃跺欒璋冪敤銆傚璞℃秷<br>閫濆嵆瀵硅薄涓嶅啀琚嬌鐢紝瀹冩墍鍗犵敤鐨勫唴瀛樺皢榪斿洖緇欑郴緇熶綔瀹冪敤銆傚湪榪欎釜鏂規(guī)硶閲岄潰錛屾垜浠彧鏄畝鍗?br>鍦版妸Person.population鍑?銆?br>褰撳璞′笉鍐嶈浣跨敤鏃訛紝__del__鏂規(guī)硶榪愯錛屼絾鏄緢闅句繚璇佽繖涓柟娉曠┒绔熷湪 浠涔堟椂鍊?榪愯銆傚鏋?br>浣犳兂瑕佹寚鏄庡畠鐨勮繍琛岋紝浣犲氨寰椾嬌鐢╠el璇彞錛屽氨濡傚悓鎴戜滑鍦ㄤ互鍓嶇殑渚嬪瓙涓嬌鐢ㄧ殑閭f牱銆?br>緇機++/Java/C#紼嬪簭鍛樼殑娉ㄩ噴<br>Python涓墍鏈夌殑綾繪垚鍛橈紙鍖呮嫭鏁版嵁鎴愬憳錛夐兘鏄?鍏叡鐨?錛屾墍鏈夌殑鏂規(guī)硶閮芥槸 鏈夋晥鐨?銆?br>鍙湁涓涓緥澶栵細(xì)濡傛灉浣犱嬌鐢ㄧ殑鏁版嵁鎴愬憳鍚嶇О浠?鍙屼笅鍒掔嚎鍓嶇紑 姣斿__privatevar錛孭ython鐨勫悕縐?br>綆$悊浣撶郴浼?xì)鏈夋晥鍦版妸瀹冧綔湄?fù)縐佹湁鍙橀噺銆?br>榪欐牱灝辨湁涓涓儻渚嬶紝濡傛灉鏌愪釜鍙橀噺鍙兂鍦ㄧ被鎴栧璞′腑浣跨敤錛屽氨搴旇浠ュ崟涓嬪垝綰垮墠緙銆傝屽叾浠栫殑<br>鍚嶇О閮藉皢浣滀負(fù)鍏叡鐨勶紝鍙互琚叾浠栫被/瀵硅薄浣跨敤銆傝浣忚繖鍙槸涓涓儻渚嬶紝騫朵笉鏄疨ython鎵瑕佹眰<br>鐨勶紙涓庡弻涓嬪垝綰垮墠緙涓嶅悓錛夈?br>鍚屾牱錛屾敞鎰廮_del__鏂規(guī)硶涓?destructor 鐨勬蹇電被浼箋? <img src ="http://www.shnenglu.com/fenglin/aggbug/125411.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-08-31 16:04 <a href="http://www.shnenglu.com/fenglin/articles/125411.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>__init__鏂規(guī)硶http://www.shnenglu.com/fenglin/articles/125408.html椋庢灄椋庢灄Tue, 31 Aug 2010 07:39:00 GMThttp://www.shnenglu.com/fenglin/articles/125408.htmlhttp://www.shnenglu.com/fenglin/comments/125408.htmlhttp://www.shnenglu.com/fenglin/articles/125408.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125408.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125408.html鏈涚殑 鍒濆鍖?銆傛敞鎰忥紝榪欎釜鍚嶇О鐨勫紑濮嬪拰緇撳熬閮芥槸鍙屼笅鍒掔嚎銆?br>
#!/usr/bin/python
#
 Filename: class_init.py
class Person:
    
def __init__(self, name):
        self.name 
= name
    
def sayHi(self):
        
print 'Hello, my name is', self.name
= Person('Swaroop')
p.sayHi()
# This short example can also be written as Person('Swaroop').sayHi() 
杈撳嚭
$ python class_init.py
Hello, my name is Swaroop

瀹冨浣曞伐浣?br>榪欓噷錛屾垜浠妸__init__鏂規(guī)硶瀹氫箟涓哄彇涓涓弬鏁皀ame錛堜互鍙婃櫘閫氱殑鍙傛暟self錛夈傚湪榪欎釜__init__閲岋紝
鎴戜滑鍙槸鍒涘緩涓涓柊鐨勫煙錛屼篃縐頒負(fù)name銆傛敞鎰忓畠浠槸涓や釜涓嶅悓鐨勫彉閲忥紝灝界瀹冧滑鏈夌浉鍚岀殑鍚?br>瀛椼傜偣鍙蜂嬌鎴戜滑鑳藉鍖哄垎瀹冧滑銆?br>鏈閲嶈鐨勬槸錛屾垜浠病鏈変笓闂ㄨ皟鐢╛_init__鏂規(guī)硶錛屽彧鏄湪鍒涘緩涓涓被鐨勬柊瀹炰緥鐨勬椂鍊欙紝鎶婂弬鏁板寘
鎷湪鍦嗘嫭鍙峰唴璺熷湪綾誨悕鍚庨潰錛屼粠鑰屼紶閫掔粰__init__鏂規(guī)硶銆傝繖鏄繖縐嶆柟娉曠殑閲嶈涔嬪銆?br>鐜板湪錛屾垜浠兘澶熷湪鎴戜滑鐨勬柟娉曚腑浣跨敤self.name鍩熴傝繖鍦╯ayHi鏂規(guī)硶涓緱鍒頒簡楠岃瘉銆?

椋庢灄 2010-08-31 15:39 鍙戣〃璇勮
]]>
綾?/title><link>http://www.shnenglu.com/fenglin/articles/125407.html</link><dc:creator>椋庢灄</dc:creator><author>椋庢灄</author><pubDate>Tue, 31 Aug 2010 07:33:00 GMT</pubDate><guid>http://www.shnenglu.com/fenglin/articles/125407.html</guid><wfw:comment>http://www.shnenglu.com/fenglin/comments/125407.html</wfw:comment><comments>http://www.shnenglu.com/fenglin/articles/125407.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/fenglin/comments/commentRss/125407.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/fenglin/services/trackbacks/125407.html</trackback:ping><description><![CDATA[<div style="BORDER-BOTTOM: #cccccc 1px solid; BORDER-LEFT: #cccccc 1px solid; PADDING-BOTTOM: 4px; BACKGROUND-COLOR: #eeeeee; PADDING-LEFT: 4px; WIDTH: 98%; PADDING-RIGHT: 5px; FONT-SIZE: 13px; WORD-BREAK: break-all; BORDER-TOP: #cccccc 1px solid; BORDER-RIGHT: #cccccc 1px solid; PADDING-TOP: 4px"><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"><span style="COLOR: #008000">#</span><span style="COLOR: #008000">!/usr/bin/python</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">#</span><span style="COLOR: #008000"> Filename: simplestclass.py</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> Person:<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif">    </span><span style="COLOR: #0000ff">pass</span><span style="COLOR: #000000"> </span><span style="COLOR: #008000">#</span><span style="COLOR: #008000"> An empty block</span><span style="COLOR: #008000"><br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #000000">p </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> Person()<br><img align=top src="http://www.shnenglu.com/Images/OutliningIndicators/None.gif"></span><span style="COLOR: #0000ff">print</span><span style="COLOR: #000000"> p </span></div> <strong>杈撳嚭</strong><br>$ python simplestclass.py<br><__main__.Person instance at 0xf6fcb18c> <br><br>鎴戜滑浣跨敤class璇彞鍚庤窡綾誨悕錛屽垱寤轟簡涓涓柊鐨勭被銆傝繖鍚庨潰璺熺潃涓涓緝榪涚殑璇彞鍧楀艦鎴愮被浣撱傚湪<br>榪欎釜渚嬪瓙涓紝鎴戜滑浣跨敤浜嗕竴涓┖鐧藉潡錛屽畠鐢眕ass璇彞琛ㄧず銆?br>鎺ヤ笅鏉ワ紝鎴戜滑浣跨敤綾誨悕鍚庤窡涓瀵瑰渾鎷彿鏉ュ垱寤轟竴涓璞?瀹炰緥銆備負(fù)浜嗛獙璇侊紝鎴戜滑綆鍗曞湴鎵撳嵃浜嗚繖<br>涓彉閲忕殑綾誨瀷銆傚畠鍛婅瘔鎴戜滑鎴戜滑宸茬粡鍦╛_main__妯″潡涓湁浜嗕竴涓狿erson綾葷殑瀹炰緥銆?br>鍙互娉ㄦ剰鍒板瓨鍌ㄥ璞$殑璁$畻鏈哄唴瀛樺湴鍧涔熸墦鍗頒簡鍑烘潵銆傝繖涓湴鍧鍦ㄤ綘鐨勮綆楁満涓婁細(xì)鏄彟澶栦竴涓?br>鍊鹼紝鍥犱負(fù)Python鍙互鍦ㄤ換浣曠┖浣嶅瓨鍌ㄥ璞°? <img src ="http://www.shnenglu.com/fenglin/aggbug/125407.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/fenglin/" target="_blank">椋庢灄</a> 2010-08-31 15:33 <a href="http://www.shnenglu.com/fenglin/articles/125407.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>瀛楃涓茬殑鏂規(guī)硶http://www.shnenglu.com/fenglin/articles/125399.html椋庢灄椋庢灄Tue, 31 Aug 2010 06:22:00 GMThttp://www.shnenglu.com/fenglin/articles/125399.htmlhttp://www.shnenglu.com/fenglin/comments/125399.htmlhttp://www.shnenglu.com/fenglin/articles/125399.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125399.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125399.html#!/usr/bin/python
#
 Filename: str_methods.py
name = 'Swaroop' # This is a string object 
if name.startswith('Swa'):
    
print 'Yes, the string starts with "Swa"'
if 'a' in name:
    
print 'Yes, it contains the string "a"'
if name.find('war'!= -1:
    
print 'Yes, it contains the string "war"'
delimiter 
= '_*_'
mylist 
= ['Brazil''Russia''India''China']
print delimiter.join(mylist)  杈撳嚭
$ python str_methods.py
Yes, the string starts with "Swa"
Yes, it contains the string "a"
Yes, it contains the string "war"
Brazil_*_Russia_*_India_*_China
 
瀹冨浣曞伐浣?/strong>
榪欓噷錛屾垜浠湅鍒頒嬌鐢ㄤ簡璁稿瀛楃涓叉柟娉曘?span style="COLOR: red">startwith鏂規(guī)硶鏄敤鏉ユ祴璇曞瓧絎︿覆鏄惁浠ョ粰瀹氬瓧絎︿覆寮
濮嬨?span style="COLOR: red">in鎿嶄綔絎︾敤鏉ユ楠屼竴涓粰瀹氬瓧絎︿覆鏄惁涓哄彟涓涓瓧絎︿覆鐨勪竴閮ㄥ垎銆?br>find鏂規(guī)硶鐢ㄦ潵鎵懼嚭緇欏畾瀛楃涓插湪鍙︿竴涓瓧絎︿覆涓殑浣嶇疆錛屾垨鑰呰繑鍥?1浠ヨ〃紺烘壘涓嶅埌瀛愬瓧絎︿覆銆?br>str綾諱篃鏈変互涓涓綔涓哄垎闅旂鐨勫瓧絎︿覆join搴忓垪鐨勯」鐩殑鏁存磥鐨勬柟娉曪紝瀹冭繑鍥炰竴涓敓鎴愮殑澶у瓧絎?br>涓層?

椋庢灄 2010-08-31 14:22 鍙戣〃璇勮
]]>
寮曠敤http://www.shnenglu.com/fenglin/articles/125398.html椋庢灄椋庢灄Tue, 31 Aug 2010 06:14:00 GMThttp://www.shnenglu.com/fenglin/articles/125398.htmlhttp://www.shnenglu.com/fenglin/comments/125398.htmlhttp://www.shnenglu.com/fenglin/articles/125398.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125398.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125398.html瀵硅薄鏈韓錛佷篃灝辨槸璇達(dá)紝鍙橀噺鍚嶆寚鍚戜綘璁$畻鏈轟腑瀛樺偍閭d釜瀵硅薄鐨勫唴瀛樸傝繖琚О浣滃悕縐板埌瀵硅薄鐨勭粦
瀹氥?br>
#!/usr/bin/python
#
 Filename: reference.py
print 'Simple Assignment'
shoplist 
= ['apple''mango''carrot''banana']
mylist 
= shoplist # mylist is just another name pointing to the same object!
del shoplist[0]
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that both shoplist and mylist both print the same list without
#
 the 'apple' confirming that they point to the same object
print 'Copy by making a full slice'
mylist 
= shoplist[:] # make a copy by doing a full slice
del mylist[0] # remove first item
print 'shoplist is', shoplist
print 'mylist is', mylist
# notice that now the two lists are different 
杈撳嚭
$ python reference.py
Simple Assignment
shoplist is ['mango', 'carrot', 'banana']
mylist is ['mango', 'carrot', 'banana']
Copy by making a full slice
shoplist is ['mango', 'carrot', 'banana']
mylist is ['carrot', 'banana']

浣犻渶瑕佽浣忕殑鍙槸濡傛灉浣犳兂瑕佸鍒朵竴涓垪琛ㄦ垨鑰呯被浼肩殑搴?br>鍒楁垨鑰呭叾浠栧鏉傜殑瀵硅薄錛堜笉鏄鏁存暟閭f牱鐨勭畝鍗?瀵硅薄 錛夛紝閭d箞浣犲繀欏諱嬌鐢ㄥ垏鐗囨搷浣滅鏉ュ彇寰?br>鎷瘋礉銆傚鏋滀綘鍙槸鎯寵浣跨敤鍙︿竴涓彉閲忓悕錛屼袱涓悕縐伴兘 寮曠敤 鍚屼竴涓璞★紝閭d箞濡傛灉浣犱笉灝忓績
鐨勮瘽錛屽彲鑳戒細(xì)寮曟潵鍚勭楹葷儲銆?

椋庢灄 2010-08-31 14:14 鍙戣〃璇勮
]]>
搴忓垪http://www.shnenglu.com/fenglin/articles/125384.html椋庢灄椋庢灄Tue, 31 Aug 2010 03:41:00 GMThttp://www.shnenglu.com/fenglin/articles/125384.htmlhttp://www.shnenglu.com/fenglin/comments/125384.htmlhttp://www.shnenglu.com/fenglin/articles/125384.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125384.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125384.html鐐規(guī)槸绱㈠紩鎿嶄綔絎﹀拰鍒囩墖鎿嶄綔絎︺傜儲寮曟搷浣滅璁╂垜浠彲浠ヤ粠搴忓垪涓姄鍙栦竴涓壒瀹氶」鐩傚垏鐗囨搷浣?br>絎﹁鎴戜滑鑳藉鑾峰彇搴忓垪鐨勪竴涓垏鐗囷紝鍗充竴閮ㄥ垎搴忓垪銆?br>
#!/usr/bin/python
#
 Filename: seq.py
shoplist = ['apple''mango''carrot''banana']
# Indexing or 'Subscription' operation
print 'Item 0 is', shoplist[0]
print 'Item 1 is', shoplist[1]
print 'Item 2 is', shoplist[2]
print 'Item 3 is', shoplist[3]
print 'Item -1 is', shoplist[-1]
print 'Item -2 is', shoplist[-2]
# Slicing on a list
print 'Item 1 to 3 is', shoplist[1:3]
print 'Item 2 to end is', shoplist[2:]
print 'Item 1 to -1 is', shoplist[1:-1]
print 'Item start to end is', shoplist[:]
# Slicing on a string
name = 'swaroop'
print 'characters 1 to 3 is', name[1:3]
print 'characters 2 to end is', name[2:]
print 'characters 1 to -1 is', name[1:-1]
print 'characters start to end is', name[:] 
杈撳嚭
$ python seq.py
Item 0 is apple
Item 1 is mango
Item 2 is carrot
Item 3 is banana
Item -1 is banana
Item -2 is carrot
Item 1 to 3 is ['mango', 'carrot']
Item 2 to end is ['carrot', 'banana']
Item 1 to -1 is ['mango', 'carrot']
Item start to end is ['apple', 'mango', 'carrot', 'banana']
characters 1 to 3 is wa
characters 2 to end is aroop
characters 1 to -1 is waroo
characters start to end is swaroop

瀹冨浣曞伐浣?/strong>
棣栧厛錛屾垜浠潵瀛︿範(fàn)濡備綍浣跨敤绱㈠紩鏉ュ彇寰楀簭鍒椾腑鐨勫崟涓」鐩傝繖涔熻縐頒綔鏄笅鏍囨搷浣溿傛瘡褰撲綘鐢?br>鏂規(guī)嫭鍙蜂腑鐨勪竴涓暟鏉ユ寚瀹氫竴涓簭鍒楃殑鏃跺欙紝Python浼?xì)湄?fù)浣犳姄鍙栧簭鍒椾腑瀵瑰簲浣嶇疆鐨勯」鐩傝浣忥紝
Python浠?寮濮嬭鏁般傚洜姝わ紝shoplist[0]鎶撳彇絎竴涓」鐩紝shoplist[3]鎶撳彇shoplist搴忓垪涓殑絎洓涓?br>鍏冪礌銆?br>绱㈠紩鍚屾牱鍙互鏄礋鏁幫紝鍦ㄩ偅鏍風(fēng)殑鎯呭喌涓嬶紝浣嶇疆鏄粠搴忓垪灝懼紑濮嬭綆楃殑銆?/span>鍥犳錛宻hoplist[-1]琛ㄧず
搴忓垪鐨勬渶鍚庝竴涓厓绱犺宻hoplist[-2]鎶撳彇搴忓垪鐨勫掓暟絎簩涓」鐩?br>鍒囩墖鎿嶄綔絎︽槸搴忓垪鍚嶅悗璺熶竴涓柟鎷彿錛屾柟鎷彿涓湁涓瀵瑰彲閫夌殑鏁板瓧錛屽茍鐢ㄥ啋鍙峰垎鍓層傛敞鎰忚繖涓?br>浣犱嬌鐢ㄧ殑绱㈠紩鎿嶄綔絎﹀崄鍒嗙浉浼箋?span style="COLOR: red">璁頒綇鏁版槸鍙夌殑錛岃屽啋鍙鋒槸蹇呴』鐨勩?/span>
鍒囩墖鎿嶄綔絎︿腑鐨勭涓涓暟錛堝啋鍙蜂箣鍓嶏級琛ㄧず鍒囩墖寮濮嬬殑浣嶇疆錛岀浜屼釜鏁幫紙鍐掑彿涔嬪悗錛夎〃紺哄垏鐗?br>鍒板摢閲岀粨鏉熴傚鏋滀笉鎸囧畾絎竴涓暟錛孭ython灝變粠搴忓垪棣栧紑濮嬨傚鏋滄病鏈夋寚瀹氱浜屼釜鏁幫紝鍒?br>Python浼?xì)鍋滄鍦ㄥ簭鍒棡銆傛敞鎰忥紝榪斿洖鐨勫簭鍒椾粠寮濮嬩綅緗?寮濮?錛屽垰濂藉湪 緇撴潫 浣嶇疆涔嬪墠緇撴潫銆傚嵆
寮濮嬩綅緗槸鍖呭惈鍦ㄥ簭鍒楀垏鐗囦腑鐨勶紝鑰岀粨鏉熶綅緗鎺掓枼鍦ㄥ垏鐗囧銆?br>榪欐牱錛宻hoplist[1:3]榪斿洖浠庝綅緗?寮濮嬶紝鍖呮嫭浣嶇疆2錛屼絾鏄仠姝㈠湪浣嶇疆3鐨勪竴涓簭鍒楀垏鐗囷紝鍥犳榪?br>鍥炰竴涓惈鏈変袱涓」鐩殑鍒囩墖銆傜被浼煎湴錛宻hoplist[:]榪斿洖鏁翠釜搴忓垪鐨勬嫹璐濄?br>浣犲彲浠ョ敤璐熸暟鍋氬垏鐗囥傝礋鏁扮敤鍦ㄤ粠搴忓垪灝懼紑濮嬭綆楃殑浣嶇疆銆備緥濡傦紝shoplist[:-1]浼?xì)杩斿洖闄や簡鏈鍚?br>涓涓」鐩鍖呭惈鎵鏈夐」鐩殑搴忓垪鍒囩墖銆?br>浣跨敤Python瑙i噴鍣ㄤ氦浜掑湴灝濊瘯涓嶅悓鍒囩墖鎸囧畾緇勫悎錛屽嵆鍦ㄦ彁紺虹涓嬩綘鑳藉椹笂鐪嬪埌緇撴灉銆傚簭鍒楃殑
紲炲涔嬪鍦ㄤ簬浣犲彲浠ョ敤鐩稿悓鐨勬柟娉曡闂厓緇勩佸垪琛ㄥ拰瀛楃涓層?

椋庢灄 2010-08-31 11:41 鍙戣〃璇勮
]]>
瀛楀吀http://www.shnenglu.com/fenglin/articles/125383.html椋庢灄椋庢灄Tue, 31 Aug 2010 03:26:00 GMThttp://www.shnenglu.com/fenglin/articles/125383.htmlhttp://www.shnenglu.com/fenglin/comments/125383.htmlhttp://www.shnenglu.com/fenglin/articles/125383.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125383.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125383.html鍊鹼紙璇︾粏鎯呭喌錛夎仈緋誨湪涓璧楓傛敞鎰忥紝閿繀欏繪槸鍞竴鐨勶紝灝卞儚濡傛灉鏈変袱涓漢鎭板閥鍚屽悕鐨勮瘽錛屼綘鏃?br>娉曟壘鍒版紜殑淇℃伅銆?br>娉ㄦ剰錛屼綘鍙兘浣跨敤涓嶅彲鍙樼殑瀵硅薄錛堟瘮濡傚瓧絎︿覆錛夋潵浣滀負(fù)瀛楀吀鐨勯敭錛屼絾鏄綘鍙互鎶婁笉鍙彉鎴栧彲鍙?br>鐨勫璞′綔涓哄瓧鍏哥殑鍊箋傚熀鏈鏉ュ氨鏄紝浣犲簲璇ュ彧浣跨敤綆鍗曠殑瀵硅薄浣滀負(fù)閿?br>閿煎鍦ㄥ瓧鍏鎬腑浠ヨ繖鏍風(fēng)殑鏂瑰紡鏍囪錛歞 = {key1 : value1, key2 : value2 }銆傛敞鎰忓畠浠殑閿?鍊煎鐢ㄥ啋
鍙峰垎鍓詫紝鑰屽悇涓鐢ㄩ楀彿鍒嗗壊錛屾墍鏈夎繖浜涢兘鍖呮嫭鍦ㄨ姳鎷彿涓?br>璁頒綇瀛楀吀涓殑閿?鍊煎鏄病鏈夐『搴忕殑銆傚鏋滀綘鎯寵涓涓壒瀹氱殑欏哄簭錛岄偅涔堜綘搴旇鍦ㄤ嬌鐢ㄥ墠鑷繁
瀵瑰畠浠帓搴忋?br>瀛楀吀鏄痙ict綾葷殑瀹炰緥/瀵硅薄銆?br>
#!/usr/bin/python
#
 Filename: using_dict.py
#
 'ab' is short for 'a'ddress'b'ook
ab = {       'Swaroop'   : 'swaroopch@byteofpython.info',
             
'Larry'     : 'larry@wall.org',
             
'Matsumoto' : 'matz@ruby-lang.org',
             
'Spammer'   : 'spammer@hotmail.com'
     }
print "Swaroop's address is %s" % ab['Swaroop']
# Adding a key/value pair
ab['Guido'= 'guido@python.org'
# Deleting a key/value pair
del ab['Spammer']
print '\nThere are %d contacts in the address-book\n' % len(ab)
for name, address in ab.items():
    
print 'Contact %s at %s' % (name, address)
if 'Guido' in ab: # OR ab.has_key('Guido')
    print "\nGuido's address is %s" % ab['Guido'
杈撳嚭
$ python using_dict.py
Swaroop's address is swaroopch@byteofpython.info
There are 4 contacts in the address-book
Contact Swaroop at swaroopch@byteofpython.info
Contact Matsumoto at matz@ruby-lang.org
Contact Larry at larry@wall.org
Contact Guido at guido@python.org
Guido's address is guido@python.org



椋庢灄 2010-08-31 11:26 鍙戣〃璇勮
]]>
鍏冪粍http://www.shnenglu.com/fenglin/articles/125380.html椋庢灄椋庢灄Tue, 31 Aug 2010 03:09:00 GMThttp://www.shnenglu.com/fenglin/articles/125380.htmlhttp://www.shnenglu.com/fenglin/comments/125380.htmlhttp://www.shnenglu.com/fenglin/articles/125380.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125380.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125380.html鎷彿涓敤閫楀彿鍒嗗壊鐨勯」鐩畾涔夈傚厓緇勯氬父鐢ㄥ湪浣胯鍙ユ垨鐢ㄦ埛瀹氫箟鐨勫嚱鏁拌兘澶熷畨鍏ㄥ湴閲囩敤涓緇勫?br>鐨勬椂鍊欙紝鍗寵浣跨敤鐨勫厓緇勭殑鍊間笉浼?xì)鏀瑰彉銆?br>
#!/usr/bin/python
#
 Filename: using_tuple.py
zoo = ('wolf''elephant''penguin')
print 'Number of animals in the zoo is', len(zoo)
new_zoo 
= ('monkey''dolphin', zoo)
print 'Number of animals in the new zoo is', len(new_zoo)
print 'All animals in new zoo are', new_zoo
print 'Animals brought from old zoo are', new_zoo[2]
print 'Last animal brought from old zoo is', new_zoo[2][2]
杈撳嚭
$ python using_tuple.py
Number of animals in the zoo is 3
Number of animals in the new zoo is 3
All animals in new zoo are ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))
Animals brought from old zoo are ('wolf', 'elephant', 'penguin')
Last animal brought from old zoo is penguin
 
鍚湁0涓垨1涓」鐩殑鍏冪粍銆備竴涓┖鐨勫厓緇勭敱涓瀵圭┖鐨勫渾鎷彿緇勬垚錛屽myempty = ()銆傜劧鑰岋紝鍚?br>鏈夊崟涓厓绱犵殑鍏冪粍灝變笉閭d箞綆鍗曚簡銆備綘蹇呴』鍦ㄧ涓涓紙鍞竴涓涓級欏圭洰鍚庤窡涓涓楀彿錛岃繖鏍?br>Python鎵嶈兘鍖哄垎鍏冪粍鍜岃〃杈懼紡涓竴涓甫鍦嗘嫭鍙風(fēng)殑瀵硅薄銆傚嵆濡傛灉浣犳兂瑕佺殑鏄竴涓寘鍚」鐩?鐨勫厓
緇勭殑鏃跺欙紝浣犲簲璇ユ寚鏄巗ingleton = (2 , )銆?br>
鍏冪粍涓庢墦鍗拌鍙?br>
#!/usr/bin/python
#
 Filename: print_tuple.py
age = 22
name 
= 'Swaroop'
print '%s is %d years old' % (name, age)
print 'Why is %s playing with that python?' % name 

杈撳嚭
$ python print_tuple.py
Swaroop is 22 years old
Why is Swaroop playing with that python?


椋庢灄 2010-08-31 11:09 鍙戣〃璇勮
]]>
鍒楄〃http://www.shnenglu.com/fenglin/articles/125368.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:54:00 GMThttp://www.shnenglu.com/fenglin/articles/125368.htmlhttp://www.shnenglu.com/fenglin/comments/125368.htmlhttp://www.shnenglu.com/fenglin/articles/125368.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125368.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125368.html涓涓喘鐗╁垪琛紝涓婇潰璁拌澆鐫浣犺涔扮殑涓滆タ錛屼綘灝卞鏄撶悊瑙e垪琛ㄤ簡銆傚彧涓嶈繃鍦ㄤ綘鐨勮喘鐗╄〃涓婏紝鍙?br>鑳芥瘡鏍蜂笢瑗塊兘鐙嚜鍗犳湁涓琛岋紝鑰屽湪Python涓紝浣犲湪姣忎釜欏圭洰涔嬮棿鐢ㄩ楀彿鍒嗗壊銆?br>鍒楄〃涓殑欏圭洰搴旇鍖呮嫭鍦ㄦ柟鎷彿涓紝榪欐牱Python灝辯煡閬撲綘鏄湪鎸囨槑涓涓垪琛ㄣ備竴鏃︿綘鍒涘緩浜嗕竴
涓垪琛紝浣犲彲浠ユ坊鍔犮佸垹闄ゆ垨鏄悳绱㈠垪琛ㄤ腑鐨勯」鐩傜敱浜庝綘鍙互澧炲姞鎴栧垹闄ら」鐩紝鎴戜滑璇村垪琛?br>鏄?鍙彉鐨?鏁版嵁綾誨瀷錛屽嵆榪欑綾誨瀷鏄彲浠ヨ鏀瑰彉鐨勩?br>
#!/usr/bin/python
#
 Filename: using_list.py
#
 This is my shopping list
shoplist = ['apple''mango''carrot''banana']
print 'I have', len(shoplist),'items to purchase.'
print 'These items are:'# Notice the comma at end of the line
for item in shoplist:
    
print item,
print '\nI also have to buy rice.'
shoplist.append(
'rice')
print 'My shopping list is now', shoplist
print 'I will sort my list now'
shoplist.sort()
print 'Sorted shopping list is', shoplist

print 'The first item I will buy is', shoplist[0]
olditem 
= shoplist[0]
del shoplist[0]
print 'I bought the', olditem
print 'My shopping list is now', shoplist 
杈撳嚭
$ python using_list.py
I have 4 items to purchase.
These items are: apple mango carrot banana
I also have to buy rice.
My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']
I will sort my list now
Sorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']
The first item I will buy is apple
I bought the apple
My shopping list is now ['banana', 'carrot', 'mango', 'rice']

娉ㄦ剰錛屾垜浠湪print璇彞鐨勭粨灝句嬌鐢ㄤ簡涓涓?閫楀彿 鏉ユ秷闄ゆ瘡涓猵rint璇彞鑷姩鎵撳嵃鐨勬崲琛岀銆傝繖鏍?br>鍋氭湁鐐歸毦鐪嬶紝涓嶈繃紜疄綆鍗曟湁鏁堛?/span>

椋庢灄 2010-08-31 10:54 鍙戣〃璇勮
]]>
鏁版嵁緇撴瀯http://www.shnenglu.com/fenglin/articles/125365.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:38:00 GMThttp://www.shnenglu.com/fenglin/articles/125365.htmlhttp://www.shnenglu.com/fenglin/comments/125365.htmlhttp://www.shnenglu.com/fenglin/articles/125365.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125365.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125365.html鍦≒ython涓湁涓夌鍐呭緩鐨勬暟鎹粨鏋勨斺斿垪琛ㄣ佸厓緇勫拰瀛楀吀銆?

椋庢灄 2010-08-31 10:38 鍙戣〃璇勮
]]>
dir()鍑芥暟http://www.shnenglu.com/fenglin/articles/125364.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:36:00 GMThttp://www.shnenglu.com/fenglin/articles/125364.htmlhttp://www.shnenglu.com/fenglin/comments/125364.htmlhttp://www.shnenglu.com/fenglin/articles/125364.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125364.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125364.html褰撲綘涓篸ir()鎻愪緵涓涓ā鍧楀悕鐨勬椂鍊欙紝瀹冭繑鍥炴ā鍧楀畾涔夌殑鍚嶇О鍒楄〃銆傚鏋滀笉鎻愪緵鍙傛暟錛屽畠榪斿洖褰?br>鍓嶆ā鍧椾腑瀹氫箟鐨勫悕縐板垪琛ㄣ?br>
$ python
>>> import sys
>>> dir(sys) # get list of attributes for sys module
['__displayhook__''__doc__''__excepthook__''__name__''__stderr__',
'__stdin__''__stdout__''_getframe''api_version''argv',
'builtin_module_names''byteorder''call_tracing''callstats',
'copyright''displayhook''exc_clear''exc_info''exc_type',
'excepthook''exec_prefix''executable''exit''getcheckinterval',
'getdefaultencoding''getdlopenflags''getfilesystemencoding',
'getrecursionlimit''getrefcount''hexversion''maxint''maxunicode',
'meta_path','modules''path''path_hooks''path_importer_cache',
'platform''prefix''ps1''ps2''setcheckinterval''setdlopenflags',
'setprofile''setrecursionlimit''settrace''stderr''stdin''stdout',
'version''version_info''warnoptions']
>>> dir() # get list of attributes for current module
['__builtins__''__doc__''__name__''sys']
>>>
>>> a = 5 # create a new variable 'a'
>>> dir()
[
'__builtins__''__doc__''__name__''a''sys']
>>>
>>> del a # delete/remove a name
>>>
>>> dir()
[
'__builtins__''__doc__''__name__''sys']


椋庢灄 2010-08-31 10:36 鍙戣〃璇勮
]]>
create module and call modulehttp://www.shnenglu.com/fenglin/articles/125363.html椋庢灄椋庢灄Tue, 31 Aug 2010 02:31:00 GMThttp://www.shnenglu.com/fenglin/articles/125363.htmlhttp://www.shnenglu.com/fenglin/comments/125363.htmlhttp://www.shnenglu.com/fenglin/articles/125363.html#Feedback0http://www.shnenglu.com/fenglin/comments/commentRss/125363.htmlhttp://www.shnenglu.com/fenglin/services/trackbacks/125363.html#!/usr/bin/python
#
 Filename: mymodule.py
def sayhi():
    
print 'Hi, this is mymodule speaking.'
version 
= '0.1'
# End of mymodule.py  璁頒綇榪欎釜妯″潡搴旇琚斁緗湪鎴戜滑杈撳叆瀹冪殑紼嬪簭鐨勫悓涓涓洰褰曚腑錛屾垨鑰呭湪sys.path鎵鍒楃洰褰曚箣涓銆?br>
#!/usr/bin/python
#
 Filename: mymodule_demo.py
import mymodule
mymodule.sayhi()
print 'Version', mymodule.version
杈撳嚭
$ python mymodule_demo.py
Hi, this is mymodule speaking.
Version 0.1

涓嬮潰鏄竴涓嬌鐢╢rom..import璇硶鐨勭増鏈?/strong>
#!/usr/bin/python
#
 Filename: mymodule_demo2.py
from mymodule import sayhi, version
# Alternative:
#
 from mymodule import *
sayhi()
print 'Version', version 


椋庢灄 2010-08-31 10:31 鍙戣〃璇勮
]]>
伊人久久大香线蕉综合5g| 久久伊人精品青青草原高清| 亚洲人成无码久久电影网站| 久久这里只有精品首页| 久久精品国产AV一区二区三区| 精品久久久无码人妻中文字幕| 精品久久香蕉国产线看观看亚洲| 久久久久国产视频电影| 国内精品久久久久影院日本| 精品久久久久久99人妻| 综合人妻久久一区二区精品| 国产成人香蕉久久久久| 色偷偷久久一区二区三区| 91麻精品国产91久久久久| 国内精品人妻无码久久久影院导航 | 思思久久好好热精品国产| 九九精品99久久久香蕉| 思思久久99热只有频精品66| 国产成人精品久久| 久久久女人与动物群交毛片 | 久久九九兔免费精品6| 国产亚洲精午夜久久久久久| 久久99亚洲网美利坚合众国| 伊人久久精品影院| 欧美麻豆久久久久久中文| 久久夜色精品国产亚洲| 国产亚洲婷婷香蕉久久精品| 久久久一本精品99久久精品66 | 久久久精品久久久久特色影视| 国产精品久久久久久久久鸭| 中文字幕无码免费久久| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 国产精品一区二区久久精品无码| 精品久久一区二区| 97精品国产91久久久久久| 久久人人爽人人爽人人AV| 久久综合亚洲色HEZYO社区| 久久精品无码一区二区三区免费| 国产精品久久久99| 久久一区二区三区免费| 亚洲伊人久久成综合人影院 |