{"id":9302,"date":"2016-06-07T23:30:08","date_gmt":"2016-06-08T03:30:08","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9302"},"modified":"2017-06-28T18:54:09","modified_gmt":"2017-06-28T22:54:09","slug":"zh-14","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-14\/","title":{"rendered":"14: \u65b9\u6cd5"},"content":{"rendered":"<!-- Please retain this notice and add more notes if you create a new version.<br \/>\nOriginal lesson author: David Pritchard, daveagp@gmail.com, http:\/\/cscircles.ca<br \/>\nLicense: http:\/\/creativecommons.org\/licenses\/by-nc-sa\/3.0\/-->\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86Python\u4e2d\u7684\u51e0\u79cd\u6570\u636e\u7ed3\u6784\uff1a\u5b57\u7b26\u4e32\u548c\u5e8f\u5217\u3002\u5b83\u4eec\u6bcf\u4e2a\u90fd\u652f\u6301\u51e0\u79cd\u65b9\u6cd5\uff0c\u8fd9\u4e5f\u662f\u65b9\u7a0b\u7684\u53d8\u4f53\u5f62\u5f0f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c<code>list<\/code>\u4e2d\u7684\u4e00\u4e2a<em>\u65b9\u6cd5<\/em>\u662f<code>reverse()<\/code>\u3002\u4ece\u540d\u5b57\u4e0a\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u98a0\u5012\u8fd9\u4e2a\u5e8f\u5217\uff08\u4e5f\u5c31\u662f\u8bf4\uff0c\u7b2c\u4e00\u9879\u653e\u5728\u6700\u540e\u4e00\u9879\uff0c\u6700\u540e\u4e00\u9879\u653e\u5728\u7b2c\u4e00\u9879\uff09\u3002\u4f60\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\uff08.\uff09\u6765\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align: center;\"><code>\u00abobjectName\u00bb.\u00abmethodName\u00bb(\u00ablist of arguments, if any\u00bb)<\/code><\/p>\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6211\u4eec\u5df2\u7ecf\u89c1\u5230\u7684\u4f7f\u7528\u65b9\u7a0b\u7684\u53e5\u6cd5\u662f<\/p>\n<p style=\"text-align: center;\"><code>\u00abfunctionName\u00bb(\u00ablist of arguments, if any\u00bb)<\/code><\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5728\u5e8f\u5217\u4e0a\u4f7f\u7528reverse \u7684\u65b9\u6cd5\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform0\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox0'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u98a0\u5012\u4e00\u4e2a\u5e8f\u5217\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode0' id='usercode0'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nL = [1, 2, 3, 4, 5]\nL.reverse()\nprint(L)<\/textarea><\/div>\n<div id='pbhistory0' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit0' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(0)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(0,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse0\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"0\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"a68c9120529207772697ad707aa355dd\"\/>\n<div id='pbresults0' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit0\").value = \"Run program\";document.getElementById(\"inputInUse0\").value = \"N\";<\/script>\n<\/p>\n<p>\u8fd9\u6709\u4e00\u4e2a\u65b9\u6cd5\u7684\u4f8b\u5b50\uff0c\u5b83\u5e26\u6709\u4e00\u4e2a\u5b9e\u53c2str. startswith:<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform1\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox1'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u4e00\u4e9b\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode1' id='usercode1'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nprint('piece of cake'.startswith('pie'))\nprint('red'.startswith('blue'))<\/textarea><\/div>\n<div id='pbhistory1' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit1' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(1)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(1,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse1\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"1\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"0df932323286e8bc030de657906c3b21\"\/>\n<div id='pbresults1' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit1\").value = \"Run program\";document.getElementById(\"inputInUse1\").value = \"N\";<\/script>\n<\/p>\n<h2>\u591a\u79cd\u65b9\u6cd5<\/h2>\n<p>\u5728\u4e0b\u9762\uff0c\u6211\u4eec\u4f1a\u63d0\u5230\u6700\u666e\u904d\u7684\u5b57\u7b26\u4e32\u548c\u5e8f\u5217\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5927\u591a\u90fd\u80fd\u901a\u8fc7\u4f60\u81ea\u5df1\u5199\u7684\u7a0b\u5e8f\u4ee3\u66ff\uff0c\u4f46\u662f\u4f7f\u7528\u6807\u51c6\u7684\u65b9\u6cd5\u4f1a\u8ba9\u4ee3\u7801\u66f4\u4fbf\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\u3002<\/p>\n<h3>\u5e8f\u5217<\/h3>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4f1a\u66f4\u6539\u5e8f\u5217\uff1a<\/p>\n<ul>\n<li><code>list.index(X)<\/code>: \u5bfb\u627e\u5e8f\u5217\u4e2d\u7684<code>X<\/code>\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u8fd8<code>i<\/code>\uff0c\u5176\u4e2d<code>list[i]==X<\/code>\u901a\u8fc7\u641c\u7d22\u5e8f\u5217\u4e2d\u6240\u6709\u7684\u9879\u8fd4\u8fd8\u6700\u5c0f\u7684<code>i<\/code>\u3002\u5982\u679c<code>X<\/code>\u5728\u5e8f\u5217\u4e2d\u4e0d\u5b58\u5728\uff0c<a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=L%3D%5B0%2C%201%2C%202%5D%0Aprint%28L.index%283%29%29\" target=\"_blank\">\u4f1a\u53d1\u751f<code>ValueError<\/code><\/a>\u3002\n<ul>\n<li>\u5982\u679c<code>X<\/code>\u662f\u5e8f\u5217\u4e2d\u7684\u9879\uff0c<code>X in list<\/code>\u8fd4\u8fd8<code>True<\/code>\uff0c\u5426\u5219<code>False<\/code>\u3002\u4f7f\u7528\u8fd9\u4e2a\u4f1a\u907f\u514d<code>ValueError<\/code>\uff08<code>in<\/code>\u662f\u4e00\u4e2a\u8fd0\u7b97\u7b26\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u65b9\u6cd5\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><code>list.count(X)<\/code>: \u8fd4\u8fd8<code>X<\/code>\u5728\u5e8f\u5217\u4e2d\u51fa\u73b0\u7684\u6b21\u6570<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform2\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox2'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u5e8f\u5217\u65b9\u6cd5\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode2' id='usercode2'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nL = [3, 1, 4, 1, 5, 9]\nprint(L.index(9))\nprint(L[L.index(3)])\nprint(L.count(1))<\/textarea><\/div>\n<div id='pbhistory2' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit2' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(2)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(2,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse2\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"2\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"9993f511a43e0c4b83307fc954f7eeb2\"\/>\n<div id='pbresults2' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit2\").value = \"Run program\";document.getElementById(\"inputInUse2\").value = \"N\";<\/script>\n<\/p>\n<p>\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u6539\u53d8\u5e8f\u5217\uff1a<\/p>\n<ul>\n<li><code>list.append(X)<\/code>\u628a<code>X<\/code>\u52a0\u5230<code>list<\/code>\u672b<\/li>\n<li><code>list.insert(i, X)<\/code>\u628a<code>X<\/code>\u52a0\u5230\u4f4d\u7f6e <code>i<\/code>\u4e0a<\/li>\n<li>list. extend (L)\u5728\u7ed3\u5c3e\u52a0\u4e0a\u4e00\u4e2a\u5e8f\u5217L<\/li>\n<li><code>list.remove(X)<\/code>\u79fb\u9664\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684<code>X<\/code><\/li>\n<li><code>list.pop(i)<\/code>\u5220\u9664&amp; \u8fd4\u8fd8<code>list[i]<\/code>\uff0c\u800c<code>list.pop()<\/code>\u5220\u9664&amp; \u8fd4\u8fd8\u6700\u540e\u4e00\u9879<\/li>\n<li><code>del list[i]<\/code>\u5220\u9664\u5e8f\u5217<code>list<\/code>\u4e2d\u7b2c<code>i<\/code>\u9879\uff08\u6ce8\u610f\uff0c\u8fd9\u662f\u4e00\u4e2a\"<code>del<\/code> statement\"\uff0c\u4e0d\u662f\u4e00\u4e2a\u65b9\u6cd5\uff09<\/li>\n<li><code>list.reverse()<\/code>\u98a0\u5012\u5e8f\u5217<\/li>\n<li><code>list.sort()<\/code>\u5c06\u5e8f\u5217\u6392\u5e8f<\/li>\n<\/ul>\n<p>\u4e0a\u8ff0\u6240\u6709\u7684\u65b9\u6cd5\u9664\u4e86<code>pop<\/code>\uff0c\u90fd\u8fd4\u8fd8<code>None<\/code>\u3002\u5176\u4e2d\u4e00\u4e9b\u5e26\u6709\u4e9b\u8bb8\u4e0d\u540c\u5b9e\u53c2\u7684\u65b9\u7a0b\u4e5f\u53ef\u4ee5\u88ab\u8c03\u7528\uff1b\u8be6\u89c1Python\u4e2d\u7684<a href=\"http:\/\/docs.python.org\/py3k\/library\/stdtypes.html#mutable-sequence-types\">\u5e8f\u5217\u65b9\u6cd5<\/a>\u3002\u5e8f\u5217\u4e5f\u652f\u6301\u590d\u6742\u7684\u5b50\u8303\u56f4\uff0c\u540d\u4e3a\"slices\"\uff0c\u5141\u8bb8\u6574\u4e2a\u5b50\u8303\u56f4\u7684\u63d2\u5165\u548c\u5220\u9664\uff0c\u7c7b\u4f3c\u4e8e\u6211\u4eec\u5728<a href=\"\/7a-strings\/\">previous<\/a> <a href=\"\/12-tips\/\">\u8bfe<\/a>\u4e2d\u6240\u770b\u89c1\u7684<code>string[x:y:z]<\/code>\u3002<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform3\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox3'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>\u66ff\u6362<\/span><\/div>\u4f7f\u7528<code>index<\/code>\u548c\u5176\u4ed6\u7684\u5e8f\u5217\u65b9\u6cd5\uff0c\u7f16\u5199\u4e00\u4e2a\u7a0b\u5e8f<code>replace(list, X, Y)<\/code>\uff0c\u7528<code>Y<\/code>\u66ff\u6362\u5e8f\u5217<code>list<\/code>\u4e2d\u7684\u6240\u6709<code>X<\/code>\u3002\u4f8b\u5982\uff0c\u5982\u679c<code>L = [3, 1, 4, 1, 5, 9]<\/code>\uff0c\u90a3\u4e48<code>replace(L, 1, 7)<\/code>\u4f1a\u6539\u53d8<code>L<\/code>\uff0c\u4f7f\u5176\u53d8\u4e3a<code>[3, 7, 4, 7, 5, 9]<\/code>\u3002\u4e3a\u4e86\u4f7f\u8fd9\u4e2a\u7ec3\u4e60\u66f4\u5177\u6311\u6218\u6027\uff0c\u4f60\u4e0d\u80fd\u4f7f\u7528 [ ]\u3002<br\/>\u6ce8\u610f\uff1a\u4f60\u4e0d\u9700\u8981\u4f7f\u7528<code>return<\/code>\u3002<a class=\"hintlink\"  id=\"hintlink4\">Hint<\/a><div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode3' id='usercode3'  cols=10 rows=20   class='pyboxCode RW'>\ndef replace(list, X, Y):\n<\/textarea><\/div>\n<div id='pbhistory3' class='flexcontain' style='display:none;'><\/div>\n<input type='hidden' id='defaultCode3' value='def replace(list, X, Y):\\n'><\/input>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit3' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(3)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(3,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(3,'14.replace')\" ><\/td>\n<\/tr><\/table><select id='pbSelect3' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='default' data-pbonclick=\"pbSetText(3,descape($('#defaultCode3').val()))\" >Reset code to default<\/option>\n<option name='help' data-pbonclick=\"helpClick(3);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse3\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"3\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"f39913ba59787be29d89882830f686ef\"\/>\n<div id='pbresults3' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(3);});document.getElementById(\"submit3\").value = \"Run program\";document.getElementById(\"inputInUse3\").value = \"N\";<\/script>\n<\/p>\n<h2>\u5b57\u7b26\u4e32<\/h2>\n<p>\u5c31\u50cf\u5e8f\u5217\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b57\u7b26\u4e32\u4f7f\u7528<code>in<\/code>\uff0c<code>index<\/code>\u548c<code>count<\/code>\u3002\u8fd9\u4e9b\u66f4\u52a0\u65b9\u4fbf\uff0c\u56e0\u4e3a\u4ed6\u4eec\u4e5f\u5bf9\u5b50\u94fe\u9002\u7528\uff0c\u5e76\u4e0d\u4ec5\u4ec5\u662f\u627e\u5230\u5355\u4e2a\u7684\u5b57\u7b26\uff1a<\/p>\n<ul>\n<li><code>S in T<\/code>\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u5224\u65ad\u662f\u5426\u5b57\u7b26\u4e32<code>S<\/code>\u662f\u5b57\u7b26\u4e32<code>T<\/code>\u7684\u5b50\u94fe<\/li>\n<li><code>S.index(T)<\/code>\u627e\u5230<code>S<\/code>\u4e2d\u5b50\u94fe<code>T<\/code>\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7684\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u7684\u7d22\u5f15<\/li>\n<li><code>S.count(T)<\/code>\u8fd4\u8fd8<code>S<\/code>\u7684\u5b50\u94fe<code>T<\/code>\u4e0d\u91cd\u53e0\u51fa\u73b0\u7684\u6b21\u6570<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform5\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox5'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u5bf9\u5b57\u7b26\u4e32\u4f7f\u7528<code>index<\/code>\u548c<code>count<\/code>\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode5' id='usercode5'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nprint(\"marmalade\".count(\"ma\"))\nprint(\"find a substring\".index(\"substring\"))\nprint(\"key\" in \"monkey\")<\/textarea><\/div>\n<div id='pbhistory5' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit5' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(5)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(5,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse5\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"5\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"261274ce2beec2490b6df7b6e4facb2d\"\/>\n<div id='pbresults5' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit5\").value = \"Run program\";document.getElementById(\"inputInUse5\").value = \"N\";<\/script>\n<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u6709\u7528\u7684<code>str<\/code>\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li>\u5b57\u6bcd: <code>capitalize, lower, upper, islower, isupper<\/code><\/li>\n<li>\u5b57\u7b26: <code>isalpha, isdigit<\/code><\/li>\n<li>Padding: <code>center, ljust, rjust<\/code>; <code>strip<\/code> \u4f1a\u6d88\u9664padding<\/li>\n<li>\u5b50\u94fe:\u00a0<code>endswith, startswith, find, replace<\/code><\/li>\n<li>\u5206\u89e3: <code>split, splitlines<\/code><\/li>\n<\/ul>\n<p>\u6211\u4eec\u4f1a\u5728\u9700\u8981\u7684\u65f6\u5019\u66f4\u8be6\u7ec6\u7684\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002\u5b8c\u6574\u8be6\u7ec6\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u5217\u8868\u4f1a<br \/>\n\u5728Python \u6587\u6863\u4e2d\u7ed9\u51fa\u3002<\/p>\n<p><strong>\u5b57\u7b26\u4e32\u662f\u4e0d\u53ef\u66f4\u6539\u7684<\/strong>\u3002\u6211\u4eec\u63d0\u5230\u8fc7<code>list.reverse()<\/code>\u53ef\u4ee5\u98a0\u5012\u4e00\u4e2a\u5e8f\u5217\uff0c\u4f46\u662f\u6ca1\u6709<code>str.reverse()<\/code>\u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a\u5b57\u7b26\u4e32\u4e00\u65e6\u88ab\u521b\u5efa\u5c31\u4e0d\u80fd\u88ab\u66f4\u6539\u3002\u5728\u7b2c17\u8bfe\u4e2d\u6211\u4eec\u4f1a\u505a\u4ee5\u89e3\u91ca\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\u65b9\u6cd5\u7684\u4f8b\u5b50\uff1a<code>S.replace(old, new)<\/code>\u8fd4\u8fd8\u4e00\u4e2a\u66f4\u6539\u8fc7\u7684<code>S<\/code>\uff0c\u6240\u6709\u7684\u5b50\u94fe<code>old<\/code>\u88ab\u66f4\u6362\u6210<code>new<\/code>\u3002\u8fd9\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u66f4\u6539\u4ee5\u524d\u7684\u5b57\u7b26\u4e32\uff1a<br \/>\n<form class=\"pbform\" action=\"#\" id=\"pbform6\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox6'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div>\u4f8b\u5b50\uff1a<code>replace<\/code>\u8fd4\u8fd8\u65b0\u7684\u5b57\u7b26\u4e32\uff0c\u4e0d\u66f4\u6539\u539f\u6765\u7684\u5b57\u7b26\u4e32\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 110px;'><textarea wrap='off' name='usercode6' id='usercode6'  cols=10 rows=4 readonly='readonly'  style = 'height : 110px;'  class='pyboxCode RO'>\nname = 'Bob Jones'\nnewName = name.replace('Bob', 'Robert')\nprint(name)\nprint(newName)<\/textarea><\/div>\n<div id='pbhistory6' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit6' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(6)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(6,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse6\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"6\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"c3953c67584745ca9939e83eeb7dcd45\"\/>\n<div id='pbresults6' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit6\").value = \"Run program\";document.getElementById(\"inputInUse6\").value = \"N\";<\/script>\n<\/p>\n<p>\u4e0b\u9762\u7684\u8fd9\u4e9b\u65b9\u6cd5\u5bf9\u4e0b\u4e00\u4e2a\u7ec3\u4e60\u4f1a\u6709\u6240\u5e2e\u52a9\uff1a<\/p>\n<ul>\n<li><code>str.replace<\/code>\uff0c\u6211\u4eec\u521a\u4ecb\u7ecd\u8fc7<\/li>\n<li>\u5e03\u5c14\u65b9\u6cd5<code>str.isalpha()<\/code>\uff0c\u5982\u679c<code>str<\/code>\u662f\u4e00\u4e2a\u53ea\u7531\u5b57\u6bcd\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff08\u6216\u5b57\u7b26\uff09\uff0c\u8fd4\u8fd8<code>True<\/code><\/li>\n<li>\u5e03\u5c14\u65b9\u6cd5<code>str.isdigit()<\/code>\uff0c\u5982\u679c<code>str<\/code>\u662f\u4e00\u4e2a\u53ea\u7531\u6570\u5b57\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff08\u6216\u5b57\u7b26\uff09\uff0c\u8fd4\u8fd8<code>True<\/code><\/li>\n<li><code>str.upper()<\/code>\u8fd4\u8fd8<code>str<\/code>\u7684\u5927\u5199\u5f62\u5f0f\u3002<\/li>\n<\/ul>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform7\" method=\"POST\">\n<div class='pybox modeNeutral ' id='pybox7'>\n<img title='You have not yet completed this problem.' src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/icon.png' class='pycheck'\/><div class=\"heading\"><span class='type'>Coding Exercise: <\/span><span class='title'>Exact Postage<\/span><\/div>\u5b9a\u4e49\u4e00\u4e2a\u65b9\u7a0b<code>postalValidate(S)<\/code>\uff0c\u9996\u5148\u67e5\u770b<code>S<\/code>\u662f\u5426\u4ee3\u8868<em>\u6709\u6548\u7684<\/em>\u90ae\u653f\u7f16\u7801\uff1a<\/p>\n<ul>\n<li>\u9996\u5148\uff0c\u5220\u9664\u6240\u6709\u7684\u7a7a\u683c\uff1b<\/li>\n<li>\u5269\u4e0b\u7684\u5f62\u5f0f\u4e00\u5b9a\u662f<code>L#L#L#<\/code>\uff0c\u5176\u4e2d<code>L<\/code>\u662f\u5b57\u6bcd\uff08\u4e0d\u8bba\u5927\u5c0f\u5199\uff09\uff0c<code>#<\/code>\u662f\u6570\u5b57\u3002<\/li>\n<\/ul>\n<p>\u5982\u679c<code>S<\/code>\u4e0d\u662f\u6709\u6548\u7684\u90ae\u653f\u7f16\u7801\uff0c\u8fd4\u8fd8\u5e03\u5c14\u503c<code>False<\/code>\u3002\u5982\u679cS \u6709\u6548\uff0c\u4ee5L#L#L#\u7684\u5f62<br \/>\n\u5f0f\uff0c\u8fd4\u8fd8\u76f8\u540c\u7684\u90ae\u653f\u7f16\u7801\uff0c\u5176\u4e2d\u6bcf\u4e2a<code>L<\/code>\u90fd\u662f\u5927\u5199\u3002<div class=\"helpOuter\" style=\"display: none;\"><div class=\"helpInner\"><div style=\"text-align: center\">You need to create an account and log in to ask a question.<\/div><\/div><\/div><div class='pyboxTextwrap pyboxCodewrap RW resizy'  style='height: 526px;'><textarea wrap='off' name='usercode7' id='usercode7'  cols=10 rows=20   class='pyboxCode RW'>\n<\/textarea><\/div>\n<div id='pbhistory7' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit7' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(7)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(7,'Y')\" ><\/td>\n<td><input type='button' name='history' value=\"History\" onclick=\"historyClick(7,'14.postal')\" ><\/td>\n<\/tr><\/table><select id='pbSelect7' class='selectmore'><option name='more'>More actions...<\/option>\n<option name='help' data-pbonclick=\"helpClick(7);\" >Help<\/option>\n<\/select><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse7\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"7\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"da65cdd5e6688c34bc358eaba3e2f499\"\/>\n<div id='pbresults7' class='pbresults avoidline'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>jQuery(function(){pbToggleCodeMirror(7);});document.getElementById(\"submit7\").value = \"Run program\";document.getElementById(\"inputInUse7\").value = \"N\";<\/script>\n<\/p>\n<p><table class='pywarn'><tr><td class='pywarnleft'><img src='https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-content\/plugins\/pybox\/files\/warning.png'\/><\/td><td class='pywarnright'><span> \u8fd9\u8282\u8bfe\u5269\u4f59\u7684\u90e8\u5206\u6709\u70b9\u6280\u672f\u6027\uff0c\u4f59\u4e0b\u7684\u8bfe\u7a0b\u5bf9\u8fd9\u65b9\u9762\u77e5\u8bc6\u5e76\u6ca1\u6709\u8981\u6c42\u3002 <\/span><\/td><\/table><\/p>\n<h3>\u66f4\u591a\u5bf9\u8c61<\/h3>\n<p>\u968f\u7740\u5bf9Python \u7684\u6df1\u5165\u5b66\u4e60\uff0c\u4f60\u4f1a\u53d1\u73b0\u9664\u4e86\u5b57\u7b26\u4e32\u548c\u5e8f\u5217\u4ee5\u5916\u7684\u66f4\u591a\u7684\u7c7b\u578b\u3002\u5176\u4ed6\u4e00\u4e9b\u4f60\u53ef\u80fd\u89c9\u5f97\u6709\u7528\u7684\u4e1c\u897f\u5305\u62ec<em>\u6587\u4ef6\u5bf9\u8c61<\/em>\uff0c<em>\u96c6\u5408<\/em>\uff0c<em>\u5b57\u5178<\/em>\u3002\u5b83\u4eec\u90fd\u6709\u5f88\u591a\u6709\u7528\u7684\u65b9\u6cd5\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>dir<\/code>\u65b9\u7a0b\u5217\u51faPython\u4e2d\u4e00\u4e2a\u5bf9\u8c61\u7684\u6240\u6709\u7684\u65b9\u6cd5\uff1a<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform8\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox8'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div><code>str<\/code>\u7684\u65b9\u6cd5\u3002\u6ce8\u610f<code>startswith<\/code>\u662f\u5176\u4e2d\u7684\u4e00\u4e2a\u8f93\u51fa\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 58px;'><textarea wrap='off' name='usercode8' id='usercode8'  cols=10 rows=2 readonly='readonly'  style = 'height : 58px;'  class='pyboxCode RO'>\nmyName = 'Jim'\nprint(dir(myName))<\/textarea><\/div>\n<div id='pbhistory8' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit8' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(8)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(8,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse8\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"8\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"b61570b2b95b30ad8670236bea6a68b9\"\/>\n<div id='pbresults8' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit8\").value = \"Run program\";document.getElementById(\"inputInUse8\").value = \"N\";<\/script>\n<\/p>\n<p>\u67e5\u770b\u5bf9\u8c61\u7684\u6027\u8d28\u88ab\u79f0\u4f5c<em>\u53cd\u7701<\/em>\u3002Python\u4e2d\u6240\u6709\u7684\u4e1c\u897f\u90fd\u53ef\u4ee5\u6709\u65b9\u6cd5\uff1a<\/p>\n<p><form class=\"pbform\" action=\"#\" id=\"pbform9\" method=\"POST\">\n<div class='pybox modeNeutral  facultative' id='pybox9'>\n<div class=\"heading\"><span class=\"title\">Example<\/span><\/div><code>int<\/code>\u7684\u6210\u5458\u3002<div class='pyboxTextwrap pyboxCodewrap RO '  style='height: 84px;'><textarea wrap='off' name='usercode9' id='usercode9'  cols=10 rows=3 readonly='readonly'  style = 'height : 84px;'  class='pyboxCode RO'>\nx = 42\nprint(dir(x)) # includes 'bit_length' \nprint(x.bit_length()) # a method of int<\/textarea><\/div>\n<div id='pbhistory9' class='flexcontain' style='display:none;'><\/div>\n<div class='pyboxbuttons'><table><tr>\n<td><input type='submit' name='submit' id='submit9' value=' '\/><\/td>\n<td><input type='button' name='consolecopy' value=\"Open in console\" onclick=\"pbConsoleCopy(9)\" ><\/td>\n<td><input type='button' name='visualize' value=\"Visualize\" onclick=\"pbVisualize(9,'N')\" ><\/td>\n<\/tr><\/table><\/div>\n<input type=\"hidden\" name=\"lang\" value=\"\"\/><input type=\"hidden\" id=\"inputInUse9\" name=\"inputInUse\" value=\"Y\"\/>\n<input type=\"hidden\" name=\"pyId\" value=\"9\"\/>\n<input type=\"hidden\" name=\"hash\" value=\"67274b4370fe010f7675c24568b2b247\"\/>\n<div id='pbresults9' class='pbresults'><\/div>\n<\/div>\n<\/form>\n<script type='text\/javascript'>document.getElementById(\"submit9\").value = \"Run program\";document.getElementById(\"inputInUse9\").value = \"N\";<\/script>\n<\/p>\n<p>\u4e00\u4e9b<code>dir<\/code>\u4e2d\u7684\u9879\u5b9e\u9645\u4e0a\u662f<em>member variables<\/em>\u800c\u4e0d\u662f\u65b9\u6cd5\uff0c\u4f8b\u5982<code>int.denominator<\/code>\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u4e0d\u662f\u4e00\u4e2a\u65b9\u7a0b\u3002\u4e25\u683c\u6765\u8bf4\uff0c<a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/console\/?consolecode=def%20f%28%29%3A%20return%2042%0Ag%20%3D%20f%0Aprint%28g%28%29%29%0Aprint%28type%28f%29%29\" target=\"_blank\">\u65b9\u7a0b\u662fPython\u4e2d\u7684\u5bf9\u8c61<\/a>\uff0c\u6240\u4ee5\u5b50\u65b9\u7a0b\u662f\u5b50\u53d8\u91cf\u4e2d\u7684\u7279\u6b8a\u4f8b\u5b50\u3002<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u5bf9\u6a21\u5757\u8fdb\u884c\u53cd\u7701\u3002\u5982\u679c\u4f60\u5148\u4e0b\u4ee4<code>import math<\/code>\uff0c\u7136\u540e\u4e0b\u4ee4<code>dir(math)<\/code>\uff0c\u6309\u540e\u4f60\u5c31\u4f1a\u5f97\u5230\u4e00\u5e8f\u5217<code>math<\/code>\u6a21\u5757\u4e2d\u6240\u6709\u7684\u4e1c\u897f, \u5305\u62ec\u6570\u5b57<code>pi<\/code>\u548c\u65b9\u7a0b<code>sqrt<\/code>\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u6709\u5bf9\u8c61\uff1f<\/h3>\n<p>\u4e3a\u4ec0\u4e48\u6211\u4eec\u6709\u7c7b\u4f3c\u4e8e<code>S.index(T)<\/code>\u7684\u65b9\u6cd5\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u7a0b<code>index(S, T)<\/code>\uff1f\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u6709<em>\u5bf9\u8c61<\/em> <code>S<\/code>\u548c<em>\u65b9\u6cd5<\/em> <code>str.index()<\/code>\uff1f<\/p>\n<p>\u5f53\u4f60\u5f00\u59cb\u5bf9\u66f4\u590d\u6742\u7684\uff0c\u4e0d\u540c\u79cd\u6570\u636e\u8fdb\u884c\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u5bf9\u8c61\u7684\u4e3b\u8981\u4f18\u52bf\u4f1a\u663e\u73b0\u51fa\u6765\u3002\u6bcf\u4e00\u79cd\u5bf9\u8c61\uff08\u6bd4\u5982\uff0cstr\u3001class\uff09\u4e0d\u4ec5\u4ee3\u8868\u53ef\u4ee5\u88ab\u50a8\u5b58\u7684\u6570\u636e\uff08\u6bd4\u5982\uff0c\u4e00\u7cfb\u5217\u5b57\u7b26\u548c\u957f\u5ea6\uff09\uff0c\u8fd8\u4ee3\u8868\u53ef\u4ee5\u5728\u5176\u4e0a\u8fdb\u884c\u8fd0\u7b97\u7684\u7c7b\u578b\uff08\u6bd4\u5982\uff0c\u8f6c\u6362\u4e3a\u5927\u5199\u6216\u4ea7\u751f\u5b50\u94fe\uff09\u3002\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u4f8b\u5b50\u662f<em>\u6587\u4ef6\u5bf9\u8c61<\/em>\uff1a\u4ed6\u4eec\u4ee3\u8868\u88ab\u6253\u5f00\u7684\u6587\u4ef6\u540d\u5b57\uff0c\u4f60\u73b0\u5728\u5728\u6587\u4ef6\u4e2d\u7684\u4f4d\u7f6e\uff0c\u4ee5\u53ca\u9605\u8bfb\uff0c\u5199\u7684\u65b9\u6cd5\u3002\u4f60\u751a\u81f3\u53ef\u4ee5\u5b9a\u4e49\u4f60\u81ea\u5df1\u7684\u6570\u636e\u7c7b\u578b\uff01<\/p>\n<p>\u8fd9\u4e2a\u901a\u5e38\u7684\u65b9\u6cd5\u88ab\u79f0\u4e3a\"\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\" (OOP)\u3002\u5176\u4e2d\u7684\u4e00\u4e9b\u597d\u5904\u662f\uff1a<\/p>\n<ul>\n<li><strong>\u673a\u6784<\/strong>:\u00a0<code>math<\/code>\u6a21\u5757\u4e2d\u7684\u6240\u6709\u4e1c\u897f\u90fd\u53ef\u4ee5\u901a\u8fc7<code>math.\u00abname\u00bb<\/code>\u53e5\u6cd5\u5f97\u5230\uff0c\u907f\u514d\u5728\u7a0b\u5e8f\u4e2d\u91cd\u590d\u5199\u5df2\u7ecf\u5b58\u5728\u7684\u53d8\u91cf\u540d\u5b57\u3002<\/li>\n<li><strong><strong>\u5c01\u88c5<\/strong>: <\/strong>\u5c31\u50cf\u662f\u4e00\u4e2a\u80fd\u540c\u65f6\u5bf9\u51e0\u4e2a\u5b57\u7b26\u4e32\u6216\u51e0\u4e2a\u6587\u4ef6\u8fdb\u884c\u5904\u7406\u7684\u7a0b\u5e8f\u4e00\u6837\uff0c\u4f60\u53ef\u4ee5\u5bf9\u5f88\u591a\u7531\u5176\u4ed6\u7c7b\u522b\u5b9a\u4e49\u7684\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li><strong>\u91cd\u590d\u4f7f\u7528<\/strong>: \u4e00\u65e6\u4f60\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\uff08\u6bd4\u5982<code>str<\/code>\uff09\u6216\u4e00\u4e2a\u65b9\u6cd5\u5e93\uff08\u6bd4\u5982<code>math<\/code>\uff09\uff0c\u4f60\u53ef\u4ee5\u91cd\u590d\u4f7f\u7528\u5b83\uff0c\u6216\u628a\u5b83\u7ed9\u5176\u4ed6\u4eba\u4f7f\u7528\u3002<\/li>\n<li><strong>\u8c03\u8bd5<\/strong>: \u65e9\u4e9b\u65f6\u5019\uff0c\u6211\u4eec\u770b\u5230\u4e86\u5982\u4f55\u7f16\u5199\u4e00\u4e9b\u65b9\u7a0b\u6765\u9884\u9632\u62e5\u6709\u8fd1\u4f3c\u4ee3\u7801\u7684\u526f\u672c\uff0c\u4ece\u800c\u4f7f\u8c03\u8bd5\u66f4\u7b80\u5355\u3002\u7f16\u5199\u4e00\u4e2a\u4f4d\u7f6e\u5bf9\u5e94\u4e00\u4e2a\u6570\u636e\u7684\u6240\u6709\u7684\u65b9\u7a0b\uff08\u79cd\u7c7b\u5b9a\u4e49\uff09\u53ef\u4ee5\u8d77\u5230\u76f8\u540c\u7684\u6548\u679c\u3002<\/li>\n<li><strong>\u79cd\u7c7b\u95f4\u7684\u5173\u7cfb<\/strong>: Python\u77e5\u9053<code>index<\/code>\u65b9\u6cd5\u5bf9\u4e8e\u5b57\u7b26\u4e32\u6765\u8bf4\u662f\u4e00\u4e2a\u610f\u601d\uff0c\u5bf9\u4e8e\u5e8f\u5217\u6765\u8bf4\u662f\u522b\u7684\u610f\u601d\u3002\u540c\u6837\u7684\uff0cPython\u4e0d\u4ec5\u53ef\u4ee5\u5728\u4f60\u7684\u7535\u8111\u4e0a\u8bfb\u53d6\u5e76\u4e66\u5199\u6587\u6863\uff0c\u4e5f\u53ef\u4ee5\u5728\u7f51\u7edc\u4e0a\u8bfb\u53d6\u5e76\u4e66\u5199\u6570\u636e\u3002\u5728\u8fd9\u4e24\u4e2a\u60c5\u51b5\u4e2d\uff08\u5b57\u7b26\u6216\u5e8f\u5217\u987a\u5e8f\uff0c\u672c\u5730\u6216\u8fdc\u7a0b\u6587\u4ef6\uff09\uff0c\u76f8\u5173\u7684\u79cd\u7c7b\u53ef\u4ee5\u7528\u540c\u4e00\u79cd\u65b9\u5f0f\u89e3\u51b3\u3002\u90a3\u5c31\u662f\u901a\u8fc7\u4f7f\u7528<em>inheritance<\/em>\u7684\u6982\u5ff5\u3002<\/li>\n<\/ul>\n<p>\u5728\u5269\u4e0b\u7684CS Circles\u8bfe\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ea\u4f1a<em>\u4f7f\u7528<\/em>\u5bf9\u8c61\u548c\u65b9\u6cd5\uff1b\u4f60\u53ef\u4ee5\u81ea\u884c\u4e86\u89e3\u66f4\u591a\u6709\u5173\u521b\u5efa\u81ea\u5df1\u72ec\u6709\u7684\u79cd\u7c7b\u7684\u4fe1\u606f\uff08\u67e5\u770b\u8d44\u6e90\u7f51\u9875\uff09\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u4e09\u8282\u8bfe\u53ef\u4ee5\u6309\u4efb\u4f55\u987a\u5e8f\u5b8c\u6210\uff0c\u4f1a\u6709\u4e00\u7cfb\u5217\u7ed3\u5408\u4e4b\u524d\u8bfe\u7a0b\u4e3b\u9898\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5230\u76ee\u524d\u4e3a\u6b62\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86Python\u4e2d\u7684\u51e0\u79cd\u6570\u636e\u7ed3\u6784\uff1a\u5b57\u7b26\u4e32\u548c\u5e8f\u5217\u3002\u5b83\u4eec\u6bcf\u4e2a\u90fd\u652f\u6301\u51e0\u79cd\u65b9\u6cd5\uff0c\u8fd9\u4e5f\u662f\u65b9\u7a0b\u7684\u53d8\u4f53\u5f62\u5f0f\u3002 \u4f8b\u5982\uff0clist\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\u662freverse()\u3002\u4ece\u540d\u5b57\u4e0a\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u98a0\u5012\u8fd9\u4e2a\u5e8f\u5217\uff08\u4e5f\u5c31\u662f\u8bf4\uff0c\u7b2c\u4e00\u9879\u653e\u5728\u6700\u540e\u4e00\u9879\uff0c\u6700\u540e\u4e00\u9879\u653e\u5728\u7b2c\u4e00\u9879\uff09\u3002\u4f60\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\uff08.\uff09\u6765\u4f7f\u7528\u6b64\u65b9\u6cd5\uff0c\u7ed3\u6784\u5982\u4e0b\uff1a \u00abobjectName\u00bb.\u00abmethodName\u00bb(\u00ablist of arguments, if any\u00bb) \u76f8\u6bd4\u4e4b\u4e0b\uff0c\u6211\u4eec\u5df2\u7ecf\u89c1\u5230\u7684\u4f7f\u7528\u65b9\u7a0b\u7684\u53e5\u6cd5\u662f \u00abfunctionName\u00bb(\u00ablist of arguments, if any\u00bb) \u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5728\u5e8f\u5217\u4e0a\u4f7f\u7528reverse \u7684\u65b9\u6cd5\u3002 Example\u98a0\u5012\u4e00\u4e2a\u5e8f\u5217\u3002 L = [1, 2, 3, 4, 5] L.reverse() print(L) \u8fd9\u6709\u4e00\u4e2a\u65b9\u6cd5\u7684\u4f8b\u5b50\uff0c\u5b83\u5e26\u6709\u4e00\u4e2a\u5b9e\u53c2str. startswith: Example\u4e00\u4e9b\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002 print('piece of cake'.startswith('pie')) print('red'.startswith('blue')) \u591a\u79cd\u65b9\u6cd5 \u5728\u4e0b\u9762\uff0c\u6211\u4eec\u4f1a\u63d0\u5230\u6700\u666e\u904d\u7684\u5b57\u7b26\u4e32\u548c\u5e8f\u5217\u65b9\u6cd5\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5927\u591a\u90fd\u80fd\u901a\u8fc7\u4f60\u81ea\u5df1\u5199\u7684\u7a0b\u5e8f\u4ee3\u66ff\uff0c\u4f46\u662f\u4f7f\u7528\u6807\u51c6\u7684\u65b9\u6cd5\u4f1a\u8ba9\u4ee3\u7801\u66f4\u4fbf\u4e8e\u9605\u8bfb\u548c\u7f16\u8f91\u3002 \u5e8f\u5217 \u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4f1a\u66f4\u6539\u5e8f\u5217\uff1a list.index(X): \u5bfb\u627e\u5e8f\u5217\u4e2d\u7684X\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u8fd4\u8fd8i\uff0c\u5176\u4e2dlist[i]==X\u901a\u8fc7\u641c\u7d22\u5e8f\u5217\u4e2d\u6240\u6709\u7684\u9879\u8fd4\u8fd8\u6700\u5c0f\u7684i\u3002\u5982\u679cX\u5728\u5e8f\u5217\u4e2d\u4e0d\u5b58\u5728\uff0c\u4f1a\u53d1\u751fValueError\u3002 \u5982\u679cX\u662f\u5e8f\u5217\u4e2d\u7684\u9879\uff0cX in &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/zh-14\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-9302","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/comments?post=9302"}],"version-history":[{"count":7,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9302\/revisions"}],"predecessor-version":[{"id":10233,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9302\/revisions\/10233"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}