{"id":1856,"date":"2019-06-05T20:10:42","date_gmt":"2019-06-05T20:10:42","guid":{"rendered":"http:\/\/wiki.thomasandsofia.com\/?p=1856"},"modified":"2019-06-06T16:59:41","modified_gmt":"2019-06-06T16:59:41","slug":"section-5-python-statements","status":"publish","type":"post","link":"https:\/\/wiki.thomasandsofia.com\/?p=1856","title":{"rendered":"Section 5: Python Statements"},"content":{"rendered":"<h1>Section 5 Menu<\/h1>\n<p><a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1848\">&lt; Section 4<\/a><br \/>\n<a href=\"http:\/\/wiki.thomasandsofia.com\/?p=1870\" target=\"_blank\" rel=\"noopener\">Section 6 &gt;<\/a><\/p>\n<ol start=\"33\">\n<li style=\"list-style-type: none;\">\n<ol start=\"33\">\n<li>if, elif and else Statements in Python<\/li>\n<li>for Loops in Python<\/li>\n<li>while Loops in Python<\/li>\n<li>Useful Operators in Python<\/li>\n<li>List Comprehensions in Python<\/li>\n<li>Python Statements Test Overview<\/li>\n<li>Python Statements Test Solutions<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Control Flow allows us to execute specific blocks of code under specific conditions<\/p>\n<h1>if, elif and else Statements in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407960#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407960#content<\/a><\/p>\n<p>These statements require a &#8216;:&#8217; and white space (tabs)<\/p>\n<pre>if some_condition_is_true:\r\n    # execute some code that is indented\r\n    # this line would execute as well\r\nelif previous_was_false_but_this_is_true:\r\n    #execute different code\r\nelif both_above_are_false_but_this_is_true:\r\n    # execute this block\r\n    # of code\r\nelse:\r\n    # execute this code if all conditions above are false<\/pre>\n<p>Example<\/p>\n<pre>hungry = True\r\nif hungry:\r\n   print(\"Feed me!\")\r\nelse:\r\n   print(\"I'll eat later.\")<\/pre>\n<p>Feed me!<\/p>\n<pre>hungry = False\r\nif hungry:\r\n   print(\"Feed me!\")\r\nelse:\r\n   print(\"I'll eat later.\")<\/pre>\n<p>I&#8217;ll eat later.<\/p>\n<h4>Note: Non 0 results are True, 0 results are False<\/h4>\n<pre>hungry = 5\r\nif hungry:\r\n   print(\"Feed me!\")\r\nelse:\r\n   print(\"I'll eat later.\")<\/pre>\n<p>Feed me!<\/p>\n<pre>hungry = 0\r\nif hungry:\r\n   print(\"Feed me!\")\r\nelse:\r\n   print(\"I'll eat later.\")<\/pre>\n<p>I&#8217;ll eat later.<\/p>\n<h1>for Loops in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407962#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407962#content<\/a><\/p>\n<ul>\n<li>Many objects in Python are &#8216;iterable&#8217;, meaning we can iterate (step through) over every element in the object.\n<ul>\n<li>Such as every\n<ul>\n<li>element in a list<\/li>\n<li>character in a string<\/li>\n<li>key in a dictionary<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>We can use &#8216;for&#8217; loops to execute a block of code for every iteration.<\/li>\n<\/ul>\n<pre>mylist = [1, 2, 3]\r\nfor value in mylist:\r\n    print(value)<\/pre>\n<p>1<br \/>\n2<br \/>\n3<\/p>\n<pre>myname = \"Sofia\"\r\nfor value in myname:\r\n    print(value)<\/pre>\n<p>S<br \/>\no<br \/>\nf<br \/>\ni<br \/>\na<\/p>\n<h4>Note: You can use an underscore character for variable names you do not use<\/h4>\n<pre>mylist = [1, 2, 3]\r\nfor _ in mylist:\r\n    print('Hello')<\/pre>\n<p>Hello<br \/>\nHello<br \/>\nHello<\/p>\n<h3>tuple unpacking<\/h3>\n<p>Below we &#8216;unpack&#8217; the tuple by assigning each index in the tuple to a variable.<\/p>\n<pre>mylist = [(1,2), (3,4), (5,6)]\r\nprint(len(mylist))\r\nfor value in mylist:\r\n    print(value)\r\nfor a, b in mylist:\r\n    print(f\"{a} and {b}\")<\/pre>\n<pre>3\r\n(1, 2)\r\n(3, 4)\r\n(5, 6)\r\n1 and 2\r\n3 and 4\r\n5 and 6<\/pre>\n<h4>Note: The number of variables must match the tuple count or an error will occur!<\/h4>\n<pre>mylist = [(1,2,3), (3,4), (5,6)]\r\nprint(len(mylist))\r\nfor value in mylist:\r\n    print(value)\r\nfor a, b in mylist:\r\n    print(f\"{a} and {b}\")<\/pre>\n<pre>3\r\n(1, 2, 3)\r\n(3, 4)\r\n(5, 6)\r\nValueError: too many values to unpack (expected 2)<\/pre>\n<h3>for loops with dictionaries<\/h3>\n<p>&nbsp;<\/p>\n<h4>Note: Keep in mind these are not ordered! There is no guarantee you&#8217;ll get your items back in the order you entered them!<\/h4>\n<p>This only return the keys!!<\/p>\n<pre>mydict = {'k1': '1', 'k2': 2, 'k3':3}\r\nfor value in mydict:\r\n    print(value)<\/pre>\n<p>k1<br \/>\nk2<br \/>\nk3<\/p>\n<p>To get the key:value pairs:<\/p>\n<pre>mydict = {'k1': '1', 'k2': 2, 'k3':3}\r\nfor value in mydict.items():\r\n    print(value)<\/pre>\n<p>(&#8216;k1&#8217;, 1)<br \/>\n(&#8216;k2&#8217;, 2)<br \/>\n(&#8216;k3&#8217;, 3)<\/p>\n<p>To get JUST the values:<\/p>\n<pre>mydict = {'k1': '1', 'k2': 2, 'k3':3}\r\nfor key, value in mydict.items():\r\n    print(value)<\/pre>\n<p>1<br \/>\n2<br \/>\n3<br \/>\n&#8230;or use an underscore for the key variable&#8230;<\/p>\n<pre>mydict = {'k1': '1', 'k2': 2, 'k3':3}\r\nfor _, value in mydict.items():\r\n    print(value)<\/pre>\n<p>1<br \/>\n2<br \/>\n3<br \/>\n&#8230;or use .values()<\/p>\n<pre>mydict = {'k1': '1', 'k2': 2, 'k3':3}\r\nfor value in mydict.values():\r\n    print(value)<\/pre>\n<p>1<br \/>\n2<br \/>\n3<\/p>\n<h1>while Loops in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407964#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407964#content<\/a><\/p>\n<ul>\n<li>while loops will continue to execute a block of code &#8216;while&#8217; a condition remains True\n<ul>\n<li>while my_pool != full, keep_filling_pool = true<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre>while some_condition:\r\n    # do something\r\n    # and continue to do it until the condition is false!\r\nelse:\r\n    # do something else one time<\/pre>\n<h3>Example<\/h3>\n<pre>x = 0\r\nwhile x &lt; 5:\r\n    print(x)\r\n    x += 1\r\nelse:\r\n    print(f\"'{x}' is not less than 5!\")<\/pre>\n<p>0<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<br \/>\n&#8216;5&#8217; is not less than 5!<\/p>\n<h2>break, continue and pass<\/h2>\n<ul>\n<li>break: Breaks out of the current closest enclosing loop.<\/li>\n<li>continue: Goes to the top of the current closest enclosing loop.<\/li>\n<li>pass: does nothing at all.\u00a0 o.O\u00a0\u00a0 O.o\n<ul>\n<li>it is a filler for blocks of code that have no code!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>pass<\/h3>\n<pre>x = [1,2,3]\r\nfor value in x:\r\n    # nothing here\r\nprint('done')<\/pre>\n<p>IndentationError: expected an indented block<\/p>\n<pre>x = [1,2,3]\r\nfor value in x:\r\n    pass\r\nprint('done')<\/pre>\n<p>done<\/p>\n<h3>continue<\/h3>\n<pre>name = 'Sophia'\r\nfor letter in name:\r\n    if letter == 'p':\r\n        print('f')\r\n        continue\r\n    elif letter == 'h':\r\n        #don't print anything\r\n        continue\r\n    print(letter)<\/pre>\n<p>S<br \/>\no<br \/>\nf<br \/>\ni<br \/>\na<\/p>\n<h3>break<\/h3>\n<pre>name = 'Sofia Roberts'\r\nfor letter in name:\r\n    if letter == ' ':\r\n        break\r\n    print(letter)<\/pre>\n<p>S<br \/>\no<br \/>\nf<br \/>\ni<br \/>\na<\/p>\n<h1>Useful Operators in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407966#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407966#content<\/a><\/p>\n<h3>range<\/h3>\n<p>range(start,stop,step)<\/p>\n<pre>for num in range(5):\r\n    print(num)<\/pre>\n<p>0<br \/>\n1<br \/>\n2<br \/>\n3<br \/>\n4<\/p>\n<pre>for num in range(2,5):\r\n    print(num)<\/pre>\n<p>2<br \/>\n3<br \/>\n4<\/p>\n<pre>for num in range(0,5,2):\r\n    print(num)<\/pre>\n<p>0<br \/>\n2<br \/>\n4<\/p>\n<h4>Create a list with range<\/h4>\n<pre>x = list(range(5))\r\nprint(x)<\/pre>\n<p>[0, 1, 2, 3, 4]<\/p>\n<h3>enumerate<\/h3>\n<pre>i = 0\r\nfor letter in 'abcde':\r\n    print(f\"Index {i} = {letter}\")\r\n    i += 1<\/pre>\n<p>Index 0 = a<br \/>\nIndex 1 = b<br \/>\nIndex 2 = c<br \/>\nIndex 3 = d<br \/>\nIndex 4 = e<\/p>\n<pre>for item in enumerate('abcde'):\r\n    print(item)<\/pre>\n<p>(0, &#8216;a&#8217;)<br \/>\n(1, &#8216;b&#8217;)<br \/>\n(2, &#8216;c&#8217;)<br \/>\n(3, &#8216;d&#8217;)<br \/>\n(4, &#8216;e&#8217;)<\/p>\n<pre>for i, letter in enumerate('abcde'):\r\n    print(f\"Index {i} = {letter}\")<\/pre>\n<p>Index 0 = a<br \/>\nIndex 1 = b<br \/>\nIndex 2 = c<br \/>\nIndex 3 = d<br \/>\nIndex 4 = e<\/p>\n<h3>zip<\/h3>\n<p>Returns sets of tuples paired from multiple lists<br \/>\n* Note: it stops pairing to the list with the smallest count.<\/p>\n<pre>mylist1 = [1, 2, 3, 4]\r\nmylist2 = ['a', 'b', 'c', 'd']\r\nmylist3 = ['Thomas', 'Sofia', 'Roberts']\r\nfor item in zip(mylist1, mylist2, mylist3):\r\n    print(item)<\/pre>\n<p>(1, &#8216;a&#8217;, &#8216;Thomas&#8217;)<br \/>\n(2, &#8216;b&#8217;, &#8216;Sofia&#8217;)<br \/>\n(3, &#8216;c&#8217;, &#8216;Roberts&#8217;)<\/p>\n<pre>list(zip(mylist1, mylist2, mylist3))<\/pre>\n<p>[(1, &#8216;a&#8217;, &#8216;Thomas&#8217;), (2, &#8216;b&#8217;, &#8216;Sofia&#8217;), (3, &#8216;c&#8217;, &#8216;Roberts&#8217;)]<\/p>\n<h3>in<\/h3>\n<pre>mylist = ['Thomas', 'Sofia', 'Roberts']\r\n'Jim' in mylist<\/pre>\n<p>False<\/p>\n<pre>'Sofia' in mylist<\/pre>\n<p>True<\/p>\n<pre>'f' in \"Sofia\"<\/pre>\n<p>True<\/p>\n<pre>'k1' in {'k1': 123}<\/pre>\n<p>True<\/p>\n<pre>123 in {'k1': 123}<\/pre>\n<p>False<\/p>\n<pre>123 in {'k1': 123}.values()<\/pre>\n<p>True<\/p>\n<h3>min and max<\/h3>\n<pre>mylist = [1, 2, 3, 4]\r\nmin(mylist)<\/pre>\n<p>1<\/p>\n<pre>max(mylist)<\/pre>\n<p>4<\/p>\n<h2>Random Library<\/h2>\n<h3>shuffle<\/h3>\n<p>This is an in place function and does not return anything.<\/p>\n<pre>from random import shuffle\r\nmylist = [1,2,3,4,5,6,7,8,9,10]\r\nshuffle(mylist)\r\nmylist<\/pre>\n<p>[6, 3, 9, 2, 1, 8, 5, 10, 4, 7]<\/p>\n<pre>shuffle(mylist)\r\nmylist<\/pre>\n<p>[4, 6, 2, 7, 5, 10, 8, 3, 9, 1]<\/p>\n<h3>randint<\/h3>\n<p>randint(min, max)<\/p>\n<pre>from random import randint\r\nx = randint(0,10)\r\nx<\/pre>\n<p>4<\/p>\n<h2>Other Functions<\/h2>\n<h3>input<\/h3>\n<p>* Note: Input results are always strings!<\/p>\n<pre>name = input('What is your name? ')<\/pre>\n<p>What is your name? Sofia<\/p>\n<pre>name<\/pre>\n<p>&#8216;Sofia&#8217;<\/p>\n<h4>Convert input results to an int or float<\/h4>\n<pre>number = input('What is your favorite number? ')<\/pre>\n<p>What is your favorite number? 24.7<\/p>\n<pre>type(number)<\/pre>\n<p>str<\/p>\n<pre>float(number)<\/pre>\n<p>24.7<\/p>\n<pre>int(number)<\/pre>\n<p>24.7<br \/>\nValueError: invalid literal for int() with base 10: &#8216;24.7&#8217;<\/p>\n<pre>int(float(number))<\/pre>\n<p>24<\/p>\n<h4>Casting input values<\/h4>\n<pre>number = int(input('What is your favorite number? '))<\/pre>\n<p>What is your favorite number? 24.7<br \/>\nValueError: invalid literal for int() with base 10: &#8216;24.7&#8217;<\/p>\n<pre>number = int(float((input('What is your favorite number? ')))<\/pre>\n<p>What is your favorite number? 24.7<\/p>\n<pre>number<\/pre>\n<p>24<\/p>\n<h1>List Comprehensions in Python<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407968#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407968#content<\/a><\/p>\n<ul>\n<li>List comprehensions are a unique way of quickly creating a list with Python.<\/li>\n<li>If you find yourself usinf a for loop along with .append() to create a list, List Comprehensions are a good alternative.<\/li>\n<\/ul>\n<pre>mystring = 'Sofia'\r\nmylist = []\r\nfor letter in mystring:\r\n    mylist.append(letter)\r\nmylist<\/pre>\n<p>[&#8216;S&#8217;, &#8216;o&#8217;, &#8216;f&#8217;, &#8216;i&#8217;, &#8216;a&#8217;]<\/p>\n<p>Shortcut version<\/p>\n<pre>mylist = [letter for letter in mystring]\r\nmylist<\/pre>\n<p>[&#8216;S&#8217;, &#8216;o&#8217;, &#8216;f&#8217;, &#8216;i&#8217;, &#8216;a&#8217;]<\/p>\n<pre>mylist = [num for num in range(0,5)]\r\nmylist<\/pre>\n<p>[0, 1, 2, 3, 4]<\/p>\n<pre>mylist = [num**2 for num in range(0,5)]\r\nmylist<\/pre>\n<p>[0, 1, 4, 9, 16]<\/p>\n<pre>fahrenheit = [ (9\/5*temp+32) for temp in [0, 10, 20, 34.5])\r\nmylist<\/pre>\n<p>[32.0, 50.0, 68.0, 94.1]<br \/>\n<b>this is the same as:<\/b><\/p>\n<pre>fahrenheit = []\r\nfor temp in [0, 10, 20, 34.5]:\r\n    fahrenheit.append(9\/5*temp+32)\r\nfahrenheit<\/pre>\n<p>[32.0, 50.0, 68.0, 94.1]<\/p>\n<h4>Using if, else statements<\/h4>\n<p><b>if<\/b><\/p>\n<pre>mylist = [num for num in range(0,11) if num%2==0]\r\nmylist<\/pre>\n<p>[0, 2, 4, 6, 8, 10]<br \/>\n<b>if else<\/b><br \/>\nNotice the order is reversed!<\/p>\n<pre>mylist = [num if num%2==0 else 'ODD' for num in range(0,11)]\r\nmylist<\/pre>\n<p>[0, &#8216;ODD&#8217;, 2, &#8216;ODD&#8217;, 4, &#8216;ODD&#8217;, 6, &#8216;ODD&#8217;, 8, &#8216;ODD&#8217;, 10]<\/p>\n<h2>Nested for loops<\/h2>\n<pre>mylist = [x * y for x in [2, 4, 6] for y in [1, 10, 100]]\r\nmylist<\/pre>\n<p>[2, 20, 200, 4, 40, 400, 6, 60, 600]<\/p>\n<h1>Python Statements Test Overview<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407970#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407970#content<\/a><\/p>\n<h1>Python Statements Test Solutions<\/h1>\n<p><a href=\"https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407974#content\" target=\"_blank\" rel=\"noopener\">https:\/\/www.udemy.com\/complete-python-bootcamp\/learn\/lecture\/9407974#content<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Section 5 Menu &lt; Section 4 Section 6 &gt; if, elif and else Statements in Python for Loops in Python while Loops in Python Useful Operators in Python List Comprehensions in Python Python Statements Test Overview Python Statements Test Solutions Control Flow allows us to execute specific blocks of code under specific conditions if, elif ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/wiki.thomasandsofia.com\/?p=1856\" 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-1856","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\/1856","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=1856"}],"version-history":[{"count":14,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1856\/revisions"}],"predecessor-version":[{"id":1873,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=\/wp\/v2\/posts\/1856\/revisions\/1873"}],"wp:attachment":[{"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wiki.thomasandsofia.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}