{"id":3870,"date":"2012-03-13T13:09:07","date_gmt":"2012-03-13T17:09:07","guid":{"rendered":"http:\/\/cemclinux1.math.uwaterloo.ca\/~cscircles\/wordpress\/?page_id=3870"},"modified":"2013-07-14T02:25:03","modified_gmt":"2013-07-14T06:25:03","slug":"cheatsheet","status":"publish","type":"page","link":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/cheatsheet\/","title":{"rendered":"Cheatsheet"},"content":{"rendered":"<p>This is the subset of Python syntax we use in our lessons (re-ordered). Here is\u00a0<a href=\"..\/wp-content\/plugins\/pybox\/files\/cheatsheet.pdf\">a printable .pdf version<\/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# order of operations: brackets, **, {* \/ \/\/ %}, {+ -}, {== != <= < > >=}\nmin(3, 4), max(3, 4), abs(-10)            #==> 3, 4, 10\nsum([1, 2, 3])  # [1, 2, 3] is a list     #==> 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# the following statements work for lists too\nlen(string)                               #==> 5\nstring[0], string[4]    # get characters  #==> \"h\", \"o\"\nstring[1:3]             # get a substring #==> \"el\"\nstring[:2], string[2:]  # l\/r substrings  #==> \"he\", \"llo\"\nstring[-1], string[-2:] # negative indices#==> \"o\", \"lo\"\n\"con\" + \"cat\" + \"enation \" + str(123)     #==> \"concatenation 123\"\n\"boo\" * 2                                 #==> \"booboo\"\n\ngetLineOfInputAsString = input()          #==> read input (or EOF error)\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# order of operations: brackets, {== !=}, not, and, or\n\nif booleanCondition:\n   x                      # indent the body block\n   x                      # every line by the same amount\nelif anotherCondition:    # can do zero, one, or several elif blocks\n   x                      # multi-line block\nelse:                     # optional\n   x                      # multi-line block\n\nwhile booleanCondition:\n   x                      # the body block\n   break                  # jump out (optional)\n   continue               # restart from top of next iteration (optional)\n\nfor indexVariable in range(low, highPlus):\n   print(indexVariable)                   #==> low, low+1, ..., highPlus-1\n# \"for item in listOrString:\" forall\/foreach loops\n# break, continue can be used in for loops\n\ndef nameOfNewFunction(argument1, argument2):\n    x                     # the body block\n    return y              # (optional; if you don't return, it returns None)\n\ndef remember(bar):        # writing to global variables\n    global saveBar        # after calling foo(3), saveBar = 3\n    saveBar = bar         # even outside of the function's scope\n\n# these 'slice' commands have analogues for lists and range()\n\"0123456789\"[::2]         # slices        #==> \"02468\"\n\"0123456789\"[::-1]        # descending    #==> \"9876543210\"\n\"0123456789\"[6:3:-1]                      #==> \"654\"\n\nx += 1        # also -=, \/=, *=, %=, **=, \/\/=. Python has no C++-style \"x++\"\nx, y = y, x   # multiple assignment\n3 < x < 5     # same as \"(3 < x) and (x < 5)\". can chain {< <= > >= == != is}\n\nimport math               # import, to get everything with period\nprint(math.sqrt(2))\nfrom math import sqrt     # import, to get one thing without period\nprint(sqrt(2))\n# also in math module: pi, exp, log, sin, cos, tan, ceil, floor, and more\n\nlist = ['zero', 'one', 'two']\nlist.index('one')                         #==> 1\nlist.index('three')                       #==> causes an error\n'three' in list, 'zero' in list           #==> False, True\nlist.count('two')                         #==> 1\ndel list[1]     # list becomes ['zero', 'two']\n\"string\" in \"superstring\"                 #==> True\n\"superstring\".index(\"string\")             #==> 5\n\n# more list methods: append(item), insert(index, item), extend(list),\n# remove(value), pop(), pop(index), reverse(), sort(), and more\n\n# some string methods: 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# and more\n\nmyList = [11, 99]\nactuallyTheSameList = myList # not a true copy! just copies the reference\nmyList is actuallyTheSameList             #==> True\nrealCopy = myList[:]         # or list(myList), copy.copy(myList), deepcopy\nrealCopy is myList                        #==> False\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This is the subset of Python syntax we use in our lessons (re-ordered). Here is\u00a0a printable .pdf version. 3 * 4, 3 + 4, 3 - 4, 3 \/ 4 #==> 12, 7, -1, 0.75 3 ** 4, 3 \/\/ &hellip; <a href=\"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/cheatsheet\/\">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-3870","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/3870","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=3870"}],"version-history":[{"count":19,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/3870\/revisions"}],"predecessor-version":[{"id":3872,"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/pages\/3870\/revisions\/3872"}],"wp:attachment":[{"href":"https:\/\/cscircles.cemc.uwaterloo.ca\/dev\/wp-json\/wp\/v2\/media?parent=3870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}