{"id":167,"date":"2018-01-25T03:11:31","date_gmt":"2018-01-24T20:11:31","guid":{"rendered":"http:\/\/class.ajarnsunny.com\/?page_id=167"},"modified":"2018-01-25T03:31:17","modified_gmt":"2018-01-24T20:31:17","slug":"for-and-while-loops-in-python","status":"publish","type":"page","link":"http:\/\/class.ajarnsunny.com\/?page_id=167","title":{"rendered":"For and While loops in Python"},"content":{"rendered":"<div id=\"themify_builder_content-167\" data-postid=\"167\" class=\"themify_builder_content themify_builder_content-167 themify_builder\">\n\n    \t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_0 themify_builder_167_row module_row_167-0\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_167_column module_column_0 module_column_167-0-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-167-0-0-0\" class=\"module module-text text-167-0-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Example 1 &#8211; for loop<\/strong><\/span><br \/>This is an example of a for loop in python (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/Example1.txt\">Example1<\/a>). The for loop repeats ten times the two indented lines under the colon.<br \/>for n in range(0,10):<br \/>\u00a0 print(n)<br \/>\u00a0 print(&#8216;Today is a beautiful day&#8217;)<br \/>print(&#8216;Program finished!&#8217;)<\/p>\n<p>This is the program output:<\/p>\n<p>\u00a0<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-167-0-0-1\" class=\"module module-image image-167-0-0-1  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"311\" height=\"486\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02.png\" class=\" wp-post-image wp-image-170\" alt=\"ex01-02\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02.png 311w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02-192x300.png 192w\" sizes=\"(max-width: 311px) 100vw, 311px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t\t\n\t\t<!-- module_row -->\n\t\t\t<div  class=\"themify_builder_row module_row clearfix module_row_1 themify_builder_167_row module_row_167-1\">\n\t\t\t\t                                <div class=\"row_inner \" >\n\n                                    \n\t\t<div  class=\"tb-column col-full first module_column tb_167_column module_column_0 module_column_167-1-0\" >\n                                                                <div class=\"tb-column-inner\">\n                            \n\n<!-- module text -->\n<div  id=\"text-167-1-0-0\" class=\"module module-text text-167-1-0-0  repeat \">\n    \n    <p><span style=\"color: #000080;\"><strong>Example 2 &#8211; while loop<\/strong><\/span><br \/>This is an example of a while loop in Python (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/Example2.txt\">Example2<\/a>). The while loop repeats ten times the three indented lines under the colon.<\/p>\n<p>x = 0<br \/>while x < 10:<br \/>\u00a0 \u00a0 \u00a0<span style=\"color: #ff0000;\">print(x)<\/span><br \/><span style=\"color: #ff0000;\">\u00a0 \u00a0 \u00a0print(&#8216;Today is a beautiful day&#8217;)<\/span><br \/><span style=\"color: #ff0000;\">\u00a0 \u00a0 \u00a0x = x + 1<\/span><br \/>print(&#8216;Program finished!&#8217;)<\/p>\n<p>The output is identical to the output of the previous example:<\/p>\n<p>\u00a0<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-167-1-0-1\" class=\"module module-image image-167-1-0-1  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"311\" height=\"486\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02.png\" class=\" wp-post-image wp-image-170\" alt=\"ex01-02\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02.png 311w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/ex01-02-192x300.png 192w\" sizes=\"(max-width: 311px) 100vw, 311px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n\n\n<!-- module text -->\n<div  id=\"text-167-1-0-2\" class=\"module module-text text-167-1-0-2  repeat \">\n    \n    <p>\u00a0<\/p>\n<p><span style=\"color: #000080;\"><strong>Example 3 &#8211; infinite loop with while<\/strong><\/span><br \/>This example shows an infinite loop (file <a href=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/Example3.txt\">Example3<\/a>). The program keeps repeating the two indented lines under the colon forever, or until the program is interrupted by pressing CTRL+C. The third print statement never gets executed, because the program never gets out from the while loop.<\/p>\n<p>while True:<br \/>\u00a0 \u00a0 \u00a0<span style=\"color: #ff0000;\">print(&#8216;Today is a beautiful day&#8217;)<\/span><br \/><span style=\"color: #ff0000;\">\u00a0 \u00a0 \u00a0print(&#8216;&#8230; and yesterday was too!&#8217;)<\/span><br \/>print(&#8216;This line will never be printed!&#8217;) # This line will never be printed<br \/># because the program never get out<br \/># from the while loop<\/p>\n<p>Below is the output. The program is interrupted pressing CTRL+C.<\/p><\/div>\n<!-- \/module text -->\n\n\n    <!-- module image -->\n    <div  id=\"image-167-1-0-3\" class=\"module module-image image-167-1-0-3  image-top  \">\n\n        \n        <div class=\"image-wrap\">\n                            <img loading=\"lazy\" decoding=\"async\" width=\"641\" height=\"441\" src=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/infloop-1.png\" class=\" wp-post-image wp-image-177\" alt=\"infloop\" srcset=\"http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/infloop-1.png 641w, http:\/\/class.ajarnsunny.com\/wp-content\/uploads\/2018\/01\/infloop-1-300x206.png 300w\" sizes=\"(max-width: 641px) 100vw, 641px\" \/>            \n                        <\/div>\n            <!-- \/image-wrap -->\n        \n        \n            <\/div>\n    <!-- \/module image -->\n\n                        <\/div>\n                    \t\t<\/div>\n\t\t<!-- \/.tb-column -->\n\t\t\n\t\t\n                                <\/div>\n                                <!-- \/row_inner -->\n                        <\/div>\n                        <!-- \/module_row -->\n\t\t<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Example 1 &#8211; for loopThis is an example of a for loop in python (file Example1). The for loop repeats ten times the two indented lines under the colon.for n in range(0,10):\u00a0 print(n)\u00a0 print(&#8216;Today is a beautiful day&#8217;)print(&#8216;Program finished!&#8217;) This is the program output: \u00a0 Example 2 &#8211; while loopThis is an example of a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-167","page","type-page","status-publish","hentry","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/167"}],"collection":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":5,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/167\/revisions"}],"predecessor-version":[{"id":179,"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=\/wp\/v2\/pages\/167\/revisions\/179"}],"wp:attachment":[{"href":"http:\/\/class.ajarnsunny.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}