{"id":1870,"date":"2019-06-06T16:58:25","date_gmt":"2019-06-06T16:58:25","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1870"},"modified":"2019-06-10T13:43:20","modified_gmt":"2019-06-10T13:43:20","slug":"1870","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1870","title":{"rendered":"Section 6: Methods and Functions"},"content":{"rendered":"<h1>Section 6 Menu<\/h1>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1856\" target=\"_blank\" rel=\"noopener\">&lt; Section 5<\/a><br \/>\n<a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1885\">Section 7 &gt;<\/a><\/p>\n<ol start=\"40\">\n<li style=\"list-style-type: none;\">\n<ol start=\"40\">\n<li>Methods and Python Documentation<\/li>\n<li>Functions in Python<\/li>\n<li>Overview of Quick Function Exercises #1 &#8211; 10<\/li>\n<li>*args and **kwargs in Python<\/li>\n<li>Function Practice Exercises &#8211; Overview<\/li>\n<li>Function Practice Exercises &#8211; Solutions<\/li>\n<li>Function Practice &#8211; Solutions Level One<\/li>\n<li>Function Practice &#8211; Solutions Level Two<\/li>\n<li>Function Practice Solutions &#8211; Challenge Problem<\/li>\n<li>Lambda Expressions, Map, and Filter Functions<\/li>\n<li>Nested Statements and Scope<\/li>\n<li>Functions and Methods &#8211; Homework Assignment<\/li>\n<li>Hints and Tips for Functions and Methods Assignment<\/li>\n<li>Functions and Methods Homework &#8211; Solutions<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<h1>Methods and Python Documentations<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442554?start=0#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442554?start=0#content<\/a><\/p>\n<p>Methods: Functions built into objects<\/p>\n<p>To find methods available for an object<\/p>\n<p>* Jupyter Notebook specific<br \/>\nList all methods:<\/p>\n<ul>\n<li>object.[Tab] *<\/li>\n<\/ul>\n<p>Help on a method:<\/p>\n<ul>\n<li>object.method [Shft][Tab] *<\/li>\n<li>help(object.method)<\/li>\n<li><a href=\"https:\/\/docs.python.org\/3\/\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/<\/a><\/li>\n<\/ul>\n<h1>Functions<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442630#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442630#content<\/a><\/p>\n<p>Functions all you to reuse blocks of code without rewriting or copy\/pasting it.<\/p>\n<ul>\n<li>function name should be all lower case with snake_case<\/li>\n<\/ul>\n<pre>def function_name(name):\r\n    '''\r\n    DOCSTRING: Information about the function\r\n    INPUT: name\r\n    OUTPUT Hello name\r\n    '''    \r\n    # enter code here\r\n    print(\"Hello \"+ name)<\/pre>\n<h3>Returning Values<\/h3>\n<p>Use the &#8216;return&#8217; keyword to &#8216;pass back&#8217; the result of the function.\u00a0 This can then be assigned to a new variable<\/p>\n<pre>def add_function(num1, num2):\r\n    return num1 + num2<\/pre>\n<pre>result = add_function(1, 2)\r\nprint(result)<\/pre>\n<p>3<\/p>\n<h3>Defining default values to parameters<\/h3>\n<h4>define the function<\/h4>\n<pre>def say_hello(name):\r\n    print(\"Hello \"+name)<\/pre>\n<h4>Call the function without parameters<\/h4>\n<pre>say_hello()<\/pre>\n<p>TypeError: say_hello() missing 1 required positional argument: &#8216;name&#8217;<\/p>\n<h4>Redefine with a default parameter<\/h4>\n<p>&nbsp;<\/p>\n<pre>def say_hello(name = \"Sofia\"):\r\n    print(\"Hello \"+name)<\/pre>\n<h4>Call the function without parameters<\/h4>\n<pre>say_hello()<\/pre>\n<p>Hello Sofia<\/p>\n<h4>Now pass another name<\/h4>\n<p>&nbsp;<\/p>\n<pre>say_hello('Thomas')<\/pre>\n<p>Hello Thomas<\/p>\n<h3>Pig Latin Word Translator<\/h3>\n<p>&nbsp;<\/p>\n<pre>def pig_latin(word):\r\n    if word[0].lower() in 'aeiou':\r\n        translate = word + 'ay'\r\n    else:\r\n        translate = word[1:] + word[0].lower() + 'ay'\r\n    # Check capitalization\r\n    if word[0] != word[0].lower():\r\n        translate = translate[0].upper() + translate[1:]\r\n    return translate<\/pre>\n<pre>pig_latin('hello\")<\/pre>\n<p>ellohay<\/p>\n<pre>pig_latin('Sofia\")<\/pre>\n<p>Ofiasay<\/p>\n<pre>pig_latin('apple\")<\/pre>\n<p>appleay<\/p>\n<h1>Overview of Quick Function Exercises #1 &#8211; 10<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9532168#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9532168#content<\/a><\/p>\n<div class=\"asset-container\">\n<div class=\"article-asset--article-content--1MwHd\">\n<p>Okay so you now know functions!<\/p>\n<p>A big part of this section of the course will be testing your new skills with exercises.<strong> We have 3 main parts of exercises.<\/strong><\/p>\n<p><strong>Part 1: 10 In Course Coding Exercises\u00a0<\/strong><\/p>\n<p>We&#8217;re going to start off with just the basics with a series of 10 problems. These problems should feel relatively easy, just some quick exercises to get you comfortable with the syntax of functions. If you feel uncomfortable with these, check out lecture 26 for some useful links for warm-up problems from codingbat.com , but hopefully these exercises should feel relatively easy.<\/p>\n<p>These are in-course coding exercises. Solutions can be found linked here:<\/p>\n<p><a href=\"https:\/\/docs.google.com\/document\/d\/181AMuP-V5VnSorl_q7p6BYd8mwXWBnsZY_sSPA8trfc\/edit?usp=sharing\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.google.com\/document\/d\/181AMuP-V5VnSorl_q7p6BYd8mwXWBnsZY_sSPA8trfc\/edit?usp=sharing<\/a><\/p>\n<p><em>In between these in-course coding exercises we&#8217;ll have a quick lecture on *args and **kwargs.<\/em><\/p>\n<p><strong>Part 2:\u00a0Function Practice Exercises<\/strong><\/p>\n<p>Here we&#8217;ll have a jupyter notebook with some exercises for you to answer, we&#8217;ll have a quick overview lecture, and then have you attempt problems, afterwards we&#8217;ll have an explanatory solutions video. These problems are ranked WarmUp, Level 1, Level 2, and Challenge. You should feel comfortable with Warmup and Level 1 and Level 2. Challenge problems here are very difficult, so don&#8217;t feel bad if you don&#8217;t want to attempt them yet!\u00a0\ud83d\ude42<\/p>\n<p><em>After this we&#8217;ll cover a few more topics through some videos.<\/em><\/p>\n<p><strong>Part 3:\u00a0 Function and Methods Homework<\/strong><\/p>\n<p>We finish off this section with even more exercises! Here we have various function word problems for you to solve, again in a notebook and we will cover the solutions in a video afterwards.<\/p>\n<p>Best of luck!\u00a0If you have any questions, post to the QA forums and we&#8217;ll be happy to help you out!<\/p>\n<h1>*args and **kwargs<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442732#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442732#overview<\/a><\/p>\n<h2>*args<\/h2>\n<p>Arbitrary number of arguments as a tuple<br \/>\n<b>Note: Recommended to use &#8216;args&#8217; although could be some other variable name!<\/b><\/p>\n<pre>def myfunc(*args):\r\n    return sum(args) *0.05<\/pre>\n<pre>myfunc(1,2,3,4)<\/pre>\n<p>10<\/p>\n<h2>**kwargs<\/h2>\n<p>Arbitrary number of arguments as a dictionary<\/p>\n<pre>def myfunc(**kwargs):\r\n    for x in kwargs.item():\r\n        print(x)<\/pre>\n<pre>myfunc(a='apple', b='banana')<\/pre>\n<p>(&#8216;a&#8217;, &#8216;apple&#8217;)<br \/>\n(&#8216;b&#8217;, &#8216;banana&#8217;)<\/p>\n<h2>Mixing *args and **kwargs<\/h2>\n<p><b>Note: The arguments \/ key word arguments must be entered in the order they are accepted in the function!<br \/>\nIn this case, you must first enter the list of arguments, then keyword arguments.<\/b><\/p>\n<pre>def myfunc(*args, **kwargs):\r\n    print(f\"I would like {args[0]} {kwargs['fruit']}\")<\/pre>\n<pre>myfunc(4,5,6, animal='dog', cheese='cheddar', fruit='bananas')<\/pre>\n<p>I would like 4 bananas<\/p>\n<h1>Functions Practice Problems<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442634#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442634#overview<\/a><\/p>\n<h1>Lambda Expressions, Map, and Filter Functions<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442692#overview\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9442692#overview<\/a><\/p>\n<h2>map<\/h2>\n<p>Applies a function to a list<\/p>\n<pre>def first_letter(word):\r\n    return word[0]<\/pre>\n<pre>words = ['Hello', 'Goodbye', 'Farewell', 'So Long']\r\nfor l in map(first_letter,words):\r\n    print(l)\r\nletterlist=list(map(first_letter,words))\r\nprint(letterlist)<\/pre>\n<p>H<br \/>\nG<br \/>\nF<br \/>\nS<br \/>\n[&#8216;H&#8217;, &#8216;G&#8217;, &#8216;F&#8217;, &#8216;S&#8217;]<\/p>\n<h2>filter<\/h2>\n<p>Similar to map, but function must only return a boolean<\/p>\n<pre>def starts_with_s(word):\r\n    return word[0].lower()=='s'<\/pre>\n<pre>words = ['Hello', 'Goodbye', 'Farewell', 'So Long']\r\nfor l in filter(starts_with_s,words):\r\n    print(l)\r\nletterlist=list(filter(starts_with_s,words))\r\nprint(letterlist)<\/pre>\n<h1>lambda<\/h1>\n<pre>lambda parameter : expression<\/pre>\n<p>Often used with Maps and Filters, lambda functions are functions you&#8217;ll only use once and do not wish to define<\/p>\n<p>Instead of:<\/p>\n<pre>def first_letter(word):\r\n    return word[0]<\/pre>\n<p>Use:<\/p>\n<pre>lambda word: word[0].lower()<\/pre>\n<p>Example with map and filter:<\/p>\n<pre>words = ['Hello', 'Goodbye', 'Farewell', 'So Long']\r\nfor l in map(lambda word: word[0], words):\r\n    print(l)\r\nletterlist=list(filter( lambda word: word[0].lower()=='s',words))\r\nprint(letterlist)<\/pre>\n<p>H<br \/>\nG<br \/>\nF<br \/>\nS<br \/>\n[&#8216;So Long&#8217;]<\/p>\n<h1>Nested Statements and Scope<\/h1>\n<p>In the following example, what do you exect to print, 25 or 50<\/p>\n<pre>x = 25\r\ndef new_x():\r\n    x = 50\r\n    return 50\r\nprint(x)<\/pre>\n<p>25<\/p>\n<h2>L.E.G.B<\/h2>\n<p>This is the order Python will use to look for variable names<\/p>\n<ul>\n<li>Local: names assigned in any way within a function (def or lambda) and not declared global in that function.<\/li>\n<li>Enclosing function locals &#8211; Names in the local scope of any and all enclosing functions def or lambda) from inner to outer.<\/li>\n<li>Global (module) &#8211; Names assigned at the top-level of a module file or declared global in a def within the file.<\/li>\n<li>Built-In (Python) &#8211; Names preassigned in the built-in names module : open, range, SyntaxError, &#8230;<\/li>\n<\/ul>\n<h3>Local Example<\/h3>\n<pre>name = 'global'\r\ndef hello():\r\n   name = 'enclosed'\r\n   def hi():\r\n      name = 'local'\r\n      print(\"Hello \"+name)\r\n   hi()\r\nhello()<\/pre>\n<p>Hello local<\/p>\n<h3>Enclosed example:<\/h3>\n<p>Note: #name = &#8216;local&#8217;<\/p>\n<pre>name = 'global'\r\ndef hello():\r\n   name = 'enclosed'\r\n   def hi():\r\n      #name = 'local'\r\n      print(\"Hello \"+name)\r\nhi()\r\nhello()<\/pre>\n<p>Hello enclosed<\/p>\n<h3>Global example:<\/h3>\n<p>Note: #name = &#8216;enclosed&#8217;<\/p>\n<pre>name = 'global'\r\ndef hello():\r\n   #name = 'enclosed'\r\n   def hi():\r\n      #name = 'local'\r\n      print(\"Hello \"+name)\r\nhi()\r\nhello()<\/pre>\n<p>Hello global<\/p>\n<h1>global keyword example<\/h1>\n<pre>name = 'Thomas'\r\ndef hello():\r\n   global name\r\n   print(f\"global 'name' = '{name}'\")\r\n   name = 'Sofia'\r\n   print(f\"I just changed 'name' to: '{name}'\")\r\nhello()\r\nprint(name)<\/pre>\n<p>global &#8216;name&#8217; = &#8216;Thomas&#8217;<br \/>\nI just changed &#8216;name&#8217; to: &#8216;Sofia&#8217;<br \/>\nSofia<\/p>\n<p>Course 35% Complete<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Section 6 Menu &lt; Section 5 Section 7 &gt; Methods and Python Documentation Functions in Python Overview of Quick Function Exercises #1 &#8211; 10 *args and **kwargs in Python Function Practice Exercises &#8211; Overview Function Practice Exercises &#8211; Solutions Function Practice &#8211; Solutions Level One Function Practice &#8211; Solutions Level Two Function Practice Solutions &#8211; ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1870\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[],"class_list":["post-1870","post","type-post","status-publish","format-standard","hentry","category-python-bootcamp-0-to-hero"],"_links":{"self":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1870","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1870"}],"version-history":[{"count":13,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1870\/revisions"}],"predecessor-version":[{"id":1889,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1870\/revisions\/1889"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1870"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1870"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1870"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}