{"id":48,"date":"2020-03-26T23:58:04","date_gmt":"2020-03-26T23:58:04","guid":{"rendered":"https:\/\/www.mercerislandtech.com\/?page_id=48"},"modified":"2020-03-28T02:53:02","modified_gmt":"2020-03-28T02:53:02","slug":"py-lesson-2","status":"publish","type":"page","link":"https:\/\/www.mercerislandtech.com\/index.php\/corona-python\/py-lesson-2\/","title":{"rendered":"Py: Lesson 2"},"content":{"rendered":"\n<p>For the second lesson, we looked at:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Dictionaries<ul><li>Dictionary literal: <code>{\"Shreya\": 55, \"Nandini\": 72}<\/code><\/li><li>Dictionary variable: <code>a = {\"Shreya\": 55, \"Nandini\": 72}<\/code><\/li><li>Dictionary read\/access: <code>a[\"Shreya\"]<\/code><\/li><li>Dictionary write\/assignment: <code>a[\"Shreya\"] = 22<\/code><\/li><li>Dictionary has-key test: <code>\"Shreya\" in a<\/code> =&gt; (read &#8220;yields&#8221; or &#8220;outputs&#8221; or &#8220;returns&#8221;) <code>True<\/code>, <code>\"Fred\" in a<\/code> =&gt; <code>False<\/code><\/li><\/ul><\/li><li>Functions<ul><li>Defining a function: <code>def functionName(parameter1, p2, p3 = \"default value\"):<\/code><ul><li>The &#8220;body&#8221; of the function needs to be indented.<\/li><li>Default values can be provided when defining function parameters, but if a parameter has a default value, then all parameters after it must also have default values, so that there is an easy rule for matching arguments to a  function with the right parameter\/container\/variable name.<\/li><\/ul><\/li><li><em>Aside:<\/em> Any line following a colon (:) must be indented. When the indentation stops, this signifies the end of that &#8220;body&#8221; of code. E.g. if-then-else statements looks like this:<ul><li><code>if a[\"Shreya\"] &gt; a[\"Fred\"]:<\/code><ul><li><code>print(\"Give cane to Shreya\")<\/code><\/li><\/ul><\/li><li><code>else:<\/code><ul><li><code>print(\"Give cane to Fred\")<\/code><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><li>Classes<ul><li>Classes provide a nice way to organize programming code. Intuitively, they match concepts and words that we use in real life, like Person, Car, Address, Phone Number, etc.<\/li><li>Classes usually define both a set of variables that each &#8220;instance&#8221; of the class will have and a set of functions that can be used to get information about or change information about an instance of that class.<ul><li>Variables: E.g. all Persons shall have a name and password, and all Cars shall have a position and a heading\/direction, etc.<\/li><li>Functions (called &#8220;methods&#8221;): E.g. all Persons shall have a function that combines their first name and last name using the rule lastName + &#8220;,&#8221; + firstName, and all Cars shall have a function that moves it in the direction it is facing by 10 steps.<\/li><\/ul><\/li><li>Classes in Python are defined as follows:<ul><li><code>def Person:<\/code><ul><li><code>def __init__(self, firstName, lastName):<\/code><ul><li><code>self.firstName = firstName<\/code><\/li><li><code>self.lastName = lastName<\/code><\/li><\/ul><\/li><li><code>def lastNameCommaFirst(self):<\/code><ul><li><code>return self.lastName + \", \" + self.firstName<\/code><\/li><\/ul><\/li><\/ul><\/li><li> This is really shorthand for:<ul><li>Define a class type called &#8220;Person&#8221;, and define a &#8220;constructor&#8221; function that does something like this function:<\/li><li><code>def Person(firstName, lastName):<\/code><ul><li><code>person = new Person()<\/code><\/li><li><code>Person.__init__(person, firstName, lastName)<\/code><\/li><li><code>return person<\/code><\/li><\/ul><\/li><li>Where <code>Person.__init__<\/code> is the full name of the <code>__init__<\/code> function we defined.<\/li><li>If we create a Person and assign them to a variable called myFriend, <code>myFriend = Person(\"Bunny\", \"Hopscotch\")<\/code>, then we could get the string <code>\"Hopscotch, Bunny\"<\/code> from myFriend by typing <code>myFriend.lastNameCommaFirst()<\/code>.<ul><li>Note that <code>myFriend.lastNameCommaFirst()<\/code> gets converted internally to <code>Person.lastNameCommaFirst(myFriend)<\/code>, hence the &#8220;self&#8221; in the definition. People just prefer to type <code>myFriend.lastNameCommaFirst() <\/code>over <code>Person.lastNameCommaFirst(myFriend)<\/code>.<\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For the second lesson, we looked at: Dictionaries Dictionary literal: {&#8220;Shreya&#8221;: 55, &#8220;Nandini&#8221;: 72} Dictionary variable: a = {&#8220;Shreya&#8221;: 55, &#8220;Nandini&#8221;: 72} Dictionary read\/access: a[&#8220;Shreya&#8221;] Dictionary write\/assignment: a[&#8220;Shreya&#8221;] = 22 Dictionary has-key test: &#8220;Shreya&#8221; in a =&gt; (read &#8220;yields&#8221; or &#8220;outputs&#8221; or &#8220;returns&#8221;) True, &#8220;Fred&#8221; in a =&gt; False Functions Defining a function: def functionName(parameter1, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":46,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-48","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/pages\/48","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/comments?post=48"}],"version-history":[{"count":6,"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/pages\/48\/revisions"}],"predecessor-version":[{"id":80,"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/pages\/48\/revisions\/80"}],"up":[{"embeddable":true,"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/pages\/46"}],"wp:attachment":[{"href":"https:\/\/www.mercerislandtech.com\/index.php\/wp-json\/wp\/v2\/media?parent=48"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}