{"id":9477,"date":"2016-08-16T02:18:03","date_gmt":"2016-08-16T06:18:03","guid":{"rendered":"http:\/\/cscircles.cemc.uwaterloo.ca\/?page_id=9477"},"modified":"2016-08-31T14:20:51","modified_gmt":"2016-08-31T18:20:51","slug":"cheatsheet-zh","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/cheatsheet-zh\/","title":{"rendered":"\u5907\u5fd8\u5355"},"content":{"rendered":"<p>\u8fd9\u662f\u6211\u4eec\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u7684Python\u53e5\u6cd5(\u91cd\u65b0\u6392\u5e8f). \u8fd9\u662f<a href=\"..\/wp-content\/plugins\/pybox\/files\/cheatsheet-zh.pdf\">\u53ef\u6253\u5370\u7684pdf\u6587\u6863<\/a>.<\/p>\n<pre>3 * 4, 3 + 4, 3 - 4, 3 \/ 4                #==> 12, 7, -1, 0.75\n3 ** 4, 3 \/\/ 4, 3 % 4                     #==> 81, 0, 3\n4 > 3, 4 >= 3, 3 == 3.0, 3 != 4, 3 <= 4   #==> True, True, True, True, True\n# \u8fd0\u7b97\u987a\u5e8f\uff1a\u62ec\u53f7, **, {* \/ \/\/ %}, {+ -}, {== != <= < > >=}\nmin(3, 4), max(3, 4), abs(-10)            #==> 3, 4, 10\nsum([1, 2, 3])  # [1, 2, 3]\u662f\u4e00\u4e2a\u5217\u8868      #==> 6\n\ntype(3), type(3.0), type(\"myVariable\")    #==> class 'int', class 'float',\n                                          #    class 'str'\nint(\"4\"+\"0\"), float(3), str(1 \/ 2)        #==> 40, 3.0, '0.5'\n\n\"double quotes: ', escaped \\\" \\\\ \\'\"      #==> double quotes: ', escaped \" \\ '\n'it\\'s \"similar\" in single quotes '       #==> it's \"similar\" in single quotes\n\nord(\"A\"), chr(66)                         #==> 65, 'B'\nstring = \"hello\"\n# \u4e0b\u5217\u8bed\u53e5\u4e5f\u9002\u7528\u4e8e\u5217\u8868\nlen(string)                               #==> 5\nstring[0], string[4]    # \u5f97\u5230\u5b57\u7b26  #==> \"h\", \"o\"\nstring[1:3]             # \u5f97\u5230\u4e00\u4e2a\u5b50\u5b57\u7b26\u4e32#==> \"el\"\nstring[:2], string[2:]  # l\/r \u5b50\u5b57\u7b26\u4e32  #==> \"he\", \"llo\"\nstring[-1], string[-2:] # \u8d1f\u7d22\u5f15          #==> \"o\", \"lo\"\n\"con\" + \"cat\" + \"enation \" + str(123)     #==> \"concatenation 123\"\n\"boo\" * 2                                 #==> \"booboo\"\n\ngetLineOfInputAsString = input()          #==> \u8bfb\u5165\u8f93\u5165 (\u6216\u8005 EOF \u9519\u8bef)\nprint(\"takes\", 0, \"or more arguments\")    #==> takes 0 or more arguments\nprint(\"using\", \"custom\", \"sep\", sep=\".\")  #==> using.custom.sep\nprint(\"no\", \"newline\", end=\"bye\")         #==> no newlinebye\n\nnot True, False or True, False and True   #==> False, True, False\n# \u8fd0\u7b97\u987a\u5e8f\uff1a\u62ec\u53f7, {== !=}, not, and, or\n\nif booleanCondition:\n   x                      # \u7f29\u8fdb\u4e3b\u4f53\n   x                      # \u4e3b\u4f53\u4e2d\u7684\u6bcf\u4e00\u884c\u7f29\u8fdb\u7a0b\u5ea6\u4e00\u81f4\nelif anotherCondition:    # elif\u4e0d\u5fc5\u987b\u51fa\u73b0\uff0c\u4f7f\u7528\u6b21\u6570\u65e0\u9650\u5236\n   x                      # \u7f29\u8fdb\nelse:                     # \u53ef\u81ea\u884c\u9009\u62e9\u662f\u5426\u6dfb\u52a0else\n   x                      # \u7f29\u8fdb\n\nwhile booleanCondition:\n   x                      # \u4e3b\u4f53\n   break                  # \u8df3\u51fa\u5faa\u73af\uff08\u53ef\u9009)\n   continue               # \u4ece\u4e0b\u4e00\u6b21\u8fed\u4ee3\u91cd\u65b0\u5f00\u59cb(\u53ef\u9009)\n\nfor indexVariable in range(low, highPlus):\n   print(indexVariable)                   #==> low, low+1, ..., highPlus-1\n# \"for item in listOrString:\" forall\/foreach \u5faa\u73af\n# break, continue\u53ef\u4ee5\u7528\u5728for\u5faa\u73af\n\ndef nameOfNewFunction(argument1, argument2):\n    x                     # \u4e3b\u4f53\n    return y              # (\u53ef\u9009;\u5982\u679c\u4f60\u4e0d\u8fd4\u8fd8,\u65b9\u7a0b\u9ed8\u8ba4\u8fd4\u8fd8None)\n\ndef remember(bar):        # \u5199\u5165\u5168\u5c40\u53d8\u91cf\n    global saveBar        # \u5728\u8c03\u7528foo(3)\u540e, saveBar = 3\n    saveBar = bar         # \u5373\u4f7f\u8d85\u51fa\u51fd\u6570\u7684\u8303\u56f4\n\n# \u8fd9\u4e9b\u201c\u5207\u5272\u201d\u6307\u4ee4\u5728\u5217\u8868\u548crange() \u4e2d\u5341\u5206\u76f8\u4f3c\n\"0123456789\"[::2]         # \u5207\u5272          #==> \"02468\"\n\"0123456789\"[::-1]        # \u98a0\u5012          #==> \"9876543210\"\n\"0123456789\"[6:3:-1]                      #==> \"654\"\n\nx += 1        # \u4e5f\u53ef\u4ee5\u662f -=, \/=, *=, %=, **=, \/\/=. Python\u6ca1\u6709C++-\u7684\u8bbe\u5b9a\"x++\"\nx, y = y, x   # \u591a\u4e2a\u6307\u5b9a\n3 < x < 5     # \u4e0e\"(3 < x) and (x < 5)\"\u4e00\u81f4. can chain {< <= > >= == != is}\n\nimport math               # \u8f93\u5165\u4e00\u4e2a\u6a21\u5757,\u4f7f\u7528\u201c.\u201d\uff08\u82f1\u6587\u53e5\u53f7\uff09\u6765\u4f7f\u7528math\u4e0b\u7684\u51fd\u6570\nprint(math.sqrt(2))\nfrom math import sqrt     # \u8f93\u5165\u4e00\u4e2a\u6a21\u5757,\u65e0\u9700\u201c.\u201d\u4f7f\u7528\u65b9\u7a0b\nprint(sqrt(2))\n# math\u6a21\u5757\u8fd8\u6709\u4ee5\u4e0b\u51fd\u6570: pi, exp, log, sin, cos, tan, ceil, floor\u7b49\u7b49\n\nlist = ['zero', 'one', 'two']\nlist.index('one')                         #==> 1\nlist.index('three')                       #==> \u5bfc\u81f4\u9519\u8bef\n'three' in list, 'zero' in list           #==> False, True\nlist.count('two')                         #==> 1\ndel list[1]     # \u5217\u8868\u53d8\u6210 ['zero', 'two']\n\"string\" in \"superstring\"                 #==> True\n\"superstring\".index(\"string\")             #==> 5\n\n# \u5176\u4ed6\u5217\u8868\u8fd0\u7b97\u51fd\u6570: append(item), insert(item, index), extend(list),\n# remove(value), pop(), pop(index), reverse(), sort()\u7b49\u7b49\n\n# \u4e00\u4e9b\u5b57\u7b26\u4e32\u51fd\u6570: capitalize(), lower\/upper(), islower\/isupper(),\n# isalpha\/isdigit(), center\/ljust\/rjust(width, fillChar), strip(), split(),\n# splitlines(), endswith\/startswith(string), find(string), replace(old, new)\n# \u7b49\u7b49\n\nmyList = [11, 99]\nactuallyTheSameList = myList # \u4e0d\u662f\u5f7b\u5e95\u5730\u590d\u5236!\u53ea\u662f\u590d\u5236\u4e86\u5f15\u7528\nmyList is actuallyTheSameList             #==> True\nrealCopy = myList[:]         # \u6216\u8005list(myList), copy.copy(myList), deepcopy\nrealCopy is myList                        #==> False\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u6211\u4eec\u8bfe\u7a0b\u4e2d\u4f7f\u7528\u7684Python\u53e5\u6cd5(\u91cd\u65b0\u6392\u5e8f). \u8fd9\u662f\u53ef\u6253\u5370\u7684pdf\u6587\u6863. 3 * 4, 3 + 4, 3 - 4, 3 \/ 4 #==> 12, 7, -1, 0.75 3 ** 4, 3 \/\/ 4, 3 % 4 #==> 81, 0, 3 4 > 3, 4 >= 3, 3 == &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/cheatsheet-zh\/\">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-9477","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9477","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=9477"}],"version-history":[{"count":4,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9477\/revisions"}],"predecessor-version":[{"id":9546,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/9477\/revisions\/9546"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=9477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}