{"id":2946,"date":"2018-03-16T08:13:31","date_gmt":"2018-03-16T08:13:31","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2946"},"modified":"2018-03-18T08:34:01","modified_gmt":"2018-03-18T08:34:01","slug":"python-2-7-fundamentals-control-structures","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/16\/python-2-7-fundamentals-control-structures\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Control Structures"},"content":{"rendered":"<p>Now that we have finished looking at data types, lets look at writing logic in Python. A control structure is a language construct which controls the execution of code. Without control structures , python would execute statements from the first to the last statement one by one. Control structures enable us to divert that execution into different paths.<\/p>\n<p>There are two kinds of control structures:<\/p>\n<ul>\n<li>iteration<\/li>\n<li>selection<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h3>Iteration<\/h3>\n<p>An iteration is a control structure which repeats statements a certain number of times. Iteration consists of two statements:<\/p>\n<p><strong>while<\/strong><\/p>\n<p>The while statement will execute statements within it as long as a condition returns True. Some examples are shown below:<\/p>\n<pre>&gt;&gt;&gt; \r\n&gt;&gt;&gt; \r\n&gt;&gt;&gt; i = 10;\r\n&gt;&gt;&gt; while i &lt;= 100:\r\n... print \"i=%d\" %i\r\n... i = i + 10\r\n... \r\ni=10\r\ni=20\r\ni=30\r\ni=40\r\ni=50\r\ni=60\r\ni=70\r\ni=80\r\ni=90\r\ni=100\r\n&gt;&gt;&gt;<\/pre>\n<pre>&gt;&gt;&gt; end = False\r\n&gt;&gt;&gt; while end &lt;&gt; True:\r\n... enter = input(\"Shall I stop? \")\r\n... if enter == \"yes\":\r\n... end = True\r\n... \r\nShall I stop? \"no\"\r\nShall I stop? \"maybe\"\r\nShall I stop? \"wait for a while\"\r\nShall I stop? \"yes\"\r\n&gt;&gt;&gt; \r\n\r\n<\/pre>\n<p>While stops execution when a condition becomes True. This can be used for a lot of complex code where multiple conditions need to be checked.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>for<\/strong><\/p>\n<p>The for control structure is simpler than the while statement since it iterates for a fixed number of times in sequence. It does not work with logical conditions. It works on a range of values.<\/p>\n<pre>&gt;&gt;&gt; \r\n&gt;&gt;&gt; for i in range(0,10):\r\n... print i\r\n... \r\n0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre>&gt;&gt;&gt; list = [\"a\", \"b\", \"d\", \"e\"]\r\n&gt;&gt;&gt; for item in list:\r\n... print item\r\n...\r\na\r\nb\r\nd\r\ne\r\n&gt;&gt;&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>The advantage of <em>for<\/em> over <em>while<\/em> is that it is faster in execution because the iteration is fixed and the python compiler knows when the iteration will end. The drawback of <em>for<\/em> loop is that it can only work on a range of values. and <em>while<\/em> is more suited for complex iterations.<\/p>\n<h3>Selection<\/h3>\n<p>Selection enables the code to make a decision about which execution path to take based on a logical condition. We use the <em>if<\/em> statement<\/p>\n<pre>&gt;&gt;&gt; \r\n&gt;&gt;&gt; age = 25\r\n&gt;&gt;&gt; if age &lt; 20:\r\n... print \"You are too young\"\r\n\r\n... \r\nYou are too old\r\n&gt;&gt;&gt;<\/pre>\n<p><em>If <\/em>can be used with <em>else <\/em><\/p>\n<pre>&gt;&gt;&gt; \r\n&gt;&gt;&gt; \r\n&gt;&gt;&gt; age = 10\r\n&gt;&gt;&gt; if age &lt; 20:\r\n... print \"less than 20\"\r\n... else:\r\n... print \"more than 20\"\r\n... \r\nless than 20\r\n&gt;&gt;&gt;<\/pre>\n<p><em>If <\/em>can be used with multiple <em>elif\u00a0 (<\/em>short for <em>else if<\/em>)<\/p>\n<pre>&gt;&gt;&gt; \r\n&gt;&gt;&gt; type = \"vegetarian\"\r\n&gt;&gt;&gt; if type == \"vegetarian\":\r\n... print \"veg\"\r\n... elif type == \"non-vegetarian\":\r\n... print \"non-veg\"\r\n... else:\r\n... print \"what on earth are you\"\r\n... \r\nveg\r\n&gt;&gt;&gt;<\/pre>\n<p>Here is an example which uses both <em>while<\/em> and <em>if<\/em><\/p>\n<pre>&gt;&gt;&gt; counter = 0\r\n&gt;&gt;&gt; while counter &lt;= 20:\r\n... if counter % 2 == 0:\r\n... print \"%d is divisible by 2\" % counter\r\n... if counter % 3 == 0:\r\n... print \"%d is divisible by 3\" % counter\r\n... if counter % 4 == 0:\r\n... print \"%d is divisible by 4\" % counter\r\n... if counter % 5 == 0:\r\n... print \"%d is divisible by 5\" % counter\r\n... counter = counter + 2\r\n... \r\n0 is divisible by 2\r\n0 is divisible by 3\r\n0 is divisible by 4\r\n0 is divisible by 5\r\n2 is divisible by 2\r\n4 is divisible by 2\r\n4 is divisible by 4\r\n6 is divisible by 2\r\n6 is divisible by 3\r\n8 is divisible by 2\r\n8 is divisible by 4\r\n10 is divisible by 2\r\n10 is divisible by 5\r\n12 is divisible by 2\r\n12 is divisible by 3\r\n12 is divisible by 4\r\n14 is divisible by 2\r\n16 is divisible by 2\r\n16 is divisible by 4\r\n18 is divisible by 2\r\n18 is divisible by 3\r\n20 is divisible by 2\r\n20 is divisible by 4\r\n20 is divisible by 5\r\n&gt;&gt;&gt;<\/pre>\n<p>In the next post we will see how to handle <a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/18\/python-2-7-fundamentals-exceptions\/\" target=\"_blank\" rel=\"noopener\">Exceptions<\/a> .<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Now that we have finished looking at data types, lets look at writing logic in Python. A control structure is a language construct which controls <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/16\/python-2-7-fundamentals-control-structures\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Control Structures\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2946","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2946"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2946\/revisions"}],"predecessor-version":[{"id":3104,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2946\/revisions\/3104"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}