{"id":235,"date":"2022-05-24T14:34:47","date_gmt":"2022-05-24T13:34:47","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/?page_id=235"},"modified":"2022-05-24T14:57:03","modified_gmt":"2022-05-24T13:57:03","slug":"pre-defined-functions","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/pre-defined-functions\/","title":{"rendered":"Pre-defined Functions"},"content":{"rendered":"<p>A pre-defined is a function already built into the programming language to perform mathematical calculation, manipulate text, format values etc.<\/p>\n<h2 style=\"color: red\">Slicing Strings into Substrings<\/h2>\n<p>A string is essentially just an array of characters. Many operations and functions that work with an array also work with a string. A single character in a string is accessed by identifying its index position (in exactly the same way as an array). A sequence of characters (a substring) can also be defined by stating the range positions [start:finish]. The sample lines of code below highlight these ideas.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-239\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24144552\/Screenshot-2022-05-24-144534-300x189.png\" alt=\"\" width=\"498\" height=\"314\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24144552\/Screenshot-2022-05-24-144534-300x189.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24144552\/Screenshot-2022-05-24-144534.png 407w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/p>\n<p>scots = &#8220;Whisky&#8221;<\/p>\n<p>print(scots[1]) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # &#8216;h&#8217;<\/p>\n<p>print(scots[-2]) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u00a0 &#8216;k&#8217;<\/p>\n<p>scots[2] == scots[-4]\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # True<\/p>\n<p># When first slice value is omitted, it defaults to 0<\/p>\n<p># When second value is omitted, it defaults to end<\/p>\n<p>print(scots[1:4]) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u00a0 &#8216;his&#8217;<\/p>\n<p>print(scots[-3:]) \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #\u00a0 &#8216;sky&#8217;<\/p>\n<p>irish = scots[:5] + &#8220;e&#8221; + scots[5]\u00a0\u00a0\u00a0\u00a0\u00a0 # &#8216;Whiskey&#8217;<\/p>\n<p>irish = scots[-6:] + &#8220;e&#8221; + scots[-1]\u00a0\u00a0\u00a0 # &#8216;Whiskey&#8217;<\/p>\n<h2 style=\"color: red\">Converting between ASCII Codes and Characters<\/h2>\n<p>Any programs that manipulate text or work with single keystroke inputs often need to work at ASCII level. The two functions described below can facilitate this.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-245\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145023\/Screenshot-2022-05-24-145013-300x71.png\" alt=\"\" width=\"676\" height=\"160\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145023\/Screenshot-2022-05-24-145013-300x71.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145023\/Screenshot-2022-05-24-145013.png 595w\" sizes=\"auto, (max-width: 676px) 100vw, 676px\" \/><\/p>\n<h2 style=\"color: red\">Modulus<\/h2>\n<p>The <strong>modulus<\/strong> operator is used to retrieve the remainder after division by a number.\u00a0 It is useful for identifying if a number is even or odd, doing clock arithmetic (e.g. convert seconds into hours: minutes: seconds).\u00a0 The <strong>floor division <\/strong>operator is also listed below since it is often used together with modulus.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-247\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145159\/Screenshot-2022-05-24-145145-300x68.png\" alt=\"\" width=\"662\" height=\"150\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145159\/Screenshot-2022-05-24-145145-300x68.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145159\/Screenshot-2022-05-24-145145.png 605w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/p>\n<h2 style=\"color: red\">Data Type Casting<\/h2>\n<p>There are situations in programming where it is necessary to change the data type of a variable. The technical term for this is <strong>casting<\/strong>. For example user entries made in response to the input() function in Python are returned as strings. This is no good if they need to be manipulated as numbers in calculations. Similarly, when reading data from a text file into variables, everything (even numbers) is read as strings.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone  wp-image-249\" src=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145657\/Screenshot-2022-05-24-145646-300x155.png\" alt=\"\" width=\"650\" height=\"336\" srcset=\"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145657\/Screenshot-2022-05-24-145646-300x155.png 300w, https:\/\/blogs.glowscotland.org.uk\/glowblogs\/public\/phshighercomputingscience\/uploads\/sites\/10042\/2022\/05\/24145657\/Screenshot-2022-05-24-145646.png 595w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A pre-defined is a function already built into the programming language to perform mathematical calculation, manipulate text, format values etc. Slicing Strings into Substrings A string is essentially just an array of characters. Many operations and functions that work with an array also work with a string. A single character in a string is accessed [&hellip;]<\/p>\n","protected":false},"author":79984,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-235","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/users\/79984"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/comments?post=235"}],"version-history":[{"count":8,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/235\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/pages\/235\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/glowblogs\/phshighercomputingscience\/wp-json\/wp\/v2\/media?parent=235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}