{"id":527,"date":"2013-09-20T19:20:00","date_gmt":"2013-09-20T19:20:00","guid":{"rendered":"https:\/\/staging.infragistics.com\/blogs\/?p=527"},"modified":"2025-02-25T13:17:25","modified_gmt":"2025-02-25T13:17:25","slug":"fundamentals-of-python","status":"publish","type":"post","link":"https:\/\/www.infragistics.com\/blogs\/fundamentals-of-python","title":{"rendered":"Fundamentals of Python: Functions, Formatting &amp; Assignment Statements (Week 2)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-1-visualizing-assignment-statements\">Lesson 1: Visualizing Assignment Statements<\/h2>\n\n\n\n<p>The most important note in this lesson is understanding that assignment of variables does not mean that a variable is *equal* to the value or function it is assigned to represent.<\/p>\n\n\n\n<p>Here are the rules for Executing an Assignment Statement:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Evaluate the expression on the right side of the equals sign to produce a value. This value also has a memory address. This value also has a memory address.<\/li>\n\n\n\n<li>This memory address will be stored in the right side of the = sign, with the variable who is referencing it remaining on the left side.<\/li>\n<\/ol>\n\n\n\n<p>REMEMBER! Assignment changes the value of a VARIABLE, not what is stored in the memory address!<\/p>\n\n\n\n<p><em>Additional Notes from this Lesson:<\/em><\/p>\n\n\n\n<p>&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In IDLE, you can access the visualizer tool by navigating through Explore &gt; Visualize to see the state of computer memory used during the execution of your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-2-typ-str\">Lesson 2: typ str<\/h2>\n\n\n\n<p>typ str is Python\u2019s representation of the String type. In order to define a string literal, which is a sequence of characters, it must begin and end with either \u2018 or \u201c. Additionally, string literals can be assigned to variables. It is important to note that if your string literal needs to include a contraction or possessive, you can use \u201cs to define them. Additionally, you have the option of using \\\u2019 to refer to your \u2018 in the string, and using \u2018s to define it completely.<\/p>\n\n\n\n<p>Strings also are able to be concatenated using the + operator, but you have to remember to include spaces where appropriate, as the + operator simply mashes the strings together and does not take any spacing into account. Additionally, you can use the * operator on strings to replicate a string any number of times. However, it\u2019s important to note that order of operations still applies when using these \u201cmathematical operators\u201d on your strings. And before you go running off to try \/ and -, no, those operators don\u2019t work and will only result in you achieving a TypeError.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-3-input-output-str-formatting\">Lesson 3: Input\/Output &amp; str Formatting<\/h2>\n\n\n\n<p>In order to print by passing a single function to argument call, you\u2019ll want to produce a statement something to the effect of this:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Print (\u201ctext\u201d) => text\n\nPrint(3+7-3) => 7\n\nPrint(\u201chello\u201d, \u201cthere\u201d) => hello there<\/pre>\n\n\n\n<p>Here\u2019s an example of how you would write functions to produce the square of a number, and print it!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"\/community\/cfs-filesystemfile.ashx\/__key\/CommunityServer.Blogs.Components.WeblogFiles\/d-coding\/1524.week2.png\" alt=\"example of how you would write functions to produce the square of a number, and print it\" title=\"example of how you would write functions to produce the square of a number, and print it\"\/><\/figure>\n\n\n\n<p>NOTE! If the end of a function body is reached without executing a return statement, that function call produces value \u201cNone\u201d.<\/p>\n\n\n\n<p><em>Additional Notes from this Lesson:<\/em><\/p>\n\n\n\n<p>Triple quoted strings can span several lines. Example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Print(\u2018\u2019\u2019 How\n\nAre\n\nYou?\u2019\u2019\u2019)<\/pre>\n\n\n\n<p>In memory, this is stored as \u2018How\\nAre\\nYou?\u2019 (\\n is the newline designation)<\/p>\n\n\n\n<p>Character Designations!<\/p>\n\n\n\n<p>\\t is an escape sequence for tabs<\/p>\n\n\n\n<p>\\\\ is to print a single backslash<\/p>\n\n\n\n<p>\\\u2019 is for \u2018 in a string<\/p>\n\n\n\n<p>\\\u201d is for \u201c in a string<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-4-docstring-function-help\">Lesson 4: Docstring &amp; Function Help<\/h2>\n\n\n\n<p>Docstring is documentation for your own functions! Make sure that these are triple quoted so they are formatted correctly. Something I thought was really neat here was that the first two lines of your docstring are what show up as the hint when you begin to type the function name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-5-function-design-recipe\">Lesson 5: Function Design Recipe<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Header \u2013 includes your function name &amp; parameters<\/li>\n\n\n\n<li>Type contract \u2013 types for values of parameters &amp; expected return type<\/li>\n\n\n\n<li>Description \u2013 C\u2019mon kids! J<\/li>\n\n\n\n<li>Examples \u2013 Function use examples.<\/li>\n\n\n\n<li>Function Body \u2013 your code to make things happen!<\/li>\n<\/ol>\n\n\n\n<p>Next comes the Design Recipe, which is crafted, of course, to make things easier for you to execute as a developer.<\/p>\n\n\n\n<p>1.&nbsp;&nbsp; &nbsp;Write examples<br>2.&nbsp;&nbsp; &nbsp;Write type contract<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.&nbsp;&nbsp; &nbsp;What times for the parameters?<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; b.&nbsp;&nbsp; &nbsp;What type needs to be returned?<br>3.&nbsp;&nbsp; &nbsp;Write header<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.&nbsp;&nbsp; &nbsp;Pick meaningful parameter names<br>4.&nbsp;&nbsp; &nbsp;Write description<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a.&nbsp;&nbsp; &nbsp;Mention every parameter and describe your return value<br>5.&nbsp;&nbsp; &nbsp;Write body!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-6-function-reuse\">Lesson 6: Function Reuse<\/h2>\n\n\n\n<p>Once defined, you can use your functions over and over again! You can even pass your function calls as arguments. Get. On. It.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"lesson-7-visualizing-function-calls\">Lesson 7: Visualizing Function Calls<\/h2>\n\n\n\n<p>Formatting note: All code belonging to a function should be indented 4 spaces.<\/p>\n\n\n\n<p>Stack Frame: a region of computer memory for keeping track of information about a function being executed<\/p>\n\n\n\n<p>Locak Variable: variable created inside a function body that can only be accessed within that function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"week-2-summary\">Week 2 Summary<\/h2>\n\n\n\n<p>This week was awesome because it took concepts that I\u2019ve worked with before and brought them into the Python context. I\u2019m excited to see what\u2019s on tap for next week, especially since this course is also incorporating a Design Recipe, similar to Systematic Programming.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" class=\"wp-block-heading\" id=\"questions-comments\"><strong>Questions\/Comments?<\/strong><\/h2>\n\n\n\n<p>Feel free to comment here on my blog, or find me on Twitter <a href=\"https:\/\/twitter.com\/DokiDara\" rel=\"noopener\">@DokiDara<\/a>.<\/p>\n\n\n\n<p><a href=\"https:\/\/plus.google.com\/102021025995449539449?rel=author\" rel=\"noopener\">By Dara Monasch<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we are at Week 2 of Fundamentals of Python! This week covered Functions, Formatting &#038; Assignment statements, which don&#8217;t need much explanation really, so let&#8217;s get right into it!<\/p>\n","protected":false},"author":65,"featured_media":2367,"comment_status":"publish","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-527","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to"],"_links":{"self":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/comments?post=527"}],"version-history":[{"count":3,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/527\/revisions"}],"predecessor-version":[{"id":2126,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/posts\/527\/revisions\/2126"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media\/2367"}],"wp:attachment":[{"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/media?parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/categories?post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.infragistics.com\/blogs\/wp-json\/wp\/v2\/tags?post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}