{"id":5136,"date":"2022-03-05T10:45:14","date_gmt":"2022-03-05T09:45:14","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5136"},"modified":"2025-05-05T17:34:47","modified_gmt":"2025-05-05T15:34:47","slug":"loops","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2022\/03\/05\/loops\/","title":{"rendered":"Loops"},"content":{"rendered":"<div class=\"small-12 large-6 columns\">\n<p>1. There are two types of loops in Python: <code>while<\/code> and <code>for<\/code>:<\/p>\n<\/div>\n<p><!--more--><\/p>\n<div class=\"small-12 large-6 columns\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>the <code>while<\/code> loop executes a statement or a set of statements as long as a specified boolean condition is true, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\"># Example 1\r\nwhile True:\r\n   print(\"Stuck in an infinite loop.\")\r\n\r\n\r\n# Example 2\r\ncounter = 5\r\nwhile counter &gt; 2:\r\n   print(counter)\r\n   counter -= 1<\/pre>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>the <code>for<\/code> loop executes a set of statements many times; it&#8217;s used to iterate over a sequence (e.g., a list, a dictionary, a tuple, or a set &#8211; you will learn about them soon) or other objects that are iterable (e.g., strings). You can use the <code>for<\/code> loop to iterate over a sequence of numbers using the built-in <code>range<\/code> function. Look at the examples below:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\"># Example 1\r\nword = \"Python\"\r\nfor letter in word:\r\n   print(letter, end=\"*\")\r\n\r\n\r\n# Example 2\r\nfor i in range(1, 10):\r\n   if i % 2 == 0:\r\n      print(i)<\/pre>\n<\/div>\n<\/div>\n<p>2. You can use the <code>break<\/code> and <code>continue<\/code> statements to change the flow of a loop:<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>You use <code>break<\/code> to exit a loop, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">text = \"Coding in  Python\"\r\nfor letter in text:\r\n   if letter == \"P\":\r\n      break\r\n   print(letter, end=\"\")<\/pre>\n<\/div>\n<\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>You use <code>continue<\/code> to skip the current iteration, and continue with the next iteration, e.g.:<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">text = \"pyxpyxpyx\"\r\nfor letter in text:\r\n   if letter == \"x\":\r\n      continue\r\n   print(letter, end=\"\")<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"small-12 large-6 columns\">\n<p>3. The <code>while<\/code> and <code>for<\/code> loops can also have an <code>else<\/code> clause in Python. The <code>else<\/code> clause executes after the loop finishes its execution as long as it has not been terminated by <code>break<\/code>, e.g.:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true\">n = 0\r\nwhile n != 3:\r\n   print(n)\r\n   n += 1\r\nelse:\r\n   print(n, \"else\")\r\n<\/pre>\n<\/div>\n<\/div>\n<p>0<\/p>\n<p>1<\/p>\n<p>2<\/p>\n<p>3 else<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true\">for i in range(0, 3): \r\n  print(i) \r\nelse: \r\n  print(i, \"else\")<\/pre>\n<\/div>\n<p>0<\/p>\n<p>1<\/p>\n<p>2<\/p>\n<p>2 else<\/p>\n<p>&nbsp;<\/p>\n<div class=\"small-12 large-6 columns\">\n<pre class=\"lang:default decode:true\">for i in range(5):\r\n    print(i)\r\nelse:\r\n    print(\"else:\", i)\r\n\r\n0\r\n1\r\n2\r\n3\r\n4\r\nelse: 4\r\n<\/pre>\n<p>The <code>i<\/code> variable retains its last value.<\/p>\n<\/div>\n<pre class=\"lang:default decode:true \">i = 111\r\nfor i in range(2, 1):\r\n    print(i)\r\nelse:\r\n    print(\"else:\", i)\r\n\r\nelse: 111\r\n<\/pre>\n<p>The loop&#8217;s body won&#8217;t be executed here at all. Note: we&#8217;ve assigned the <code>i<\/code> variable before the loop. When the loop&#8217;s body isn&#8217;t executed, the control variable retains the value it had before the loop.<\/p>\n<p>Note: <strong>if the control variable doesn&#8217;t exist before the loop starts, it won&#8217;t exist when the execution reaches the <code>else<\/code> branch<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<div class=\"small-12 large-6 columns\">\n<p>4. The <code>range()<\/code> function generates a sequence of numbers. It accepts integers and returns range objects. The syntax of <code>range()<\/code> looks as follows: <code>range(start, stop, step)<\/code>, where:<\/p>\n<ul>\n<li><code>start<\/code> is an optional parameter specifying the starting number of the sequence (<span style=\"font-family: Courier New;\">0<\/span> by default)<\/li>\n<li><code>stop<\/code> is an optional parameter specifying the end of the sequence generated (it is not included),<\/li>\n<li>and <code>step<\/code> is an optional parameter specifying the difference between the numbers in the sequence (<span style=\"font-family: Courier New;\">1<\/span> by default.)<\/li>\n<\/ul>\n<p>Example code:<\/p>\n<div class=\"ace-tm\">\n<div class=\"ace_static_highlight\">\n<pre class=\"lang:default decode:true \">for i in range(3):\r\n   print(i, end=\" \") # Outputs: 0 1 2\r\n\r\nfor i in range(6, 1, -2):\r\n   print(i, end=\" \") # Outputs: 6, 4, 2<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Scenario 1<\/p>\n<p>In 1937, a German mathematician named Lothar Collatz formulated an intriguing hypothesis (it still remains unproven) which can be described in the following way:<\/p>\n<p>take any non-negative and non-zero integer number and name it c0;<br \/>\nif it&#8217;s even, evaluate a new c0 as c0 \u00f7 2;<br \/>\notherwise, if it&#8217;s odd, evaluate a new c0 as 3 \u00d7 c0 + 1;<br \/>\nif c0 \u2260 1, skip to point 2.<\/p>\n<p>&nbsp;<\/p>\n<p>The hypothesis says that regardless of the initial value of c0, it will always go to 1.<\/p>\n<p>Of course, it&#8217;s an extremely complex task to use a computer in order to prove the hypothesis for any natural number (it may even require artificial intelligence), but you can use Python to check some individual numbers. Maybe you&#8217;ll even find the one which would disprove the hypothesis.<\/p>\n<p>Write a program which reads one natural number and executes the above steps as long as c0 remains different from 1. We also want you to count the steps needed to achieve the goal. Your code should output all the intermediate values of c0, too.<\/p>\n<p>Hint: the most important part of the problem is how to transform Collatz&#8217;s idea into a while loop &#8211; this is the key to success.<\/p>\n<p>Test your code using the data we&#8217;ve provided.<br \/>\nTest Data<\/p>\n<p>Sample input: 15<\/p>\n<p>Expected output:<br \/>\n46<br \/>\n23<br \/>\n70<br \/>\n35<br \/>\n106<br \/>\n53<br \/>\n160<br \/>\n80<br \/>\n40<br \/>\n20<br \/>\n10<br \/>\n5<br \/>\n16<br \/>\n8<br \/>\n4<br \/>\n2<br \/>\n1<br \/>\nsteps = 17<\/p>\n<p>Sample input: 16<\/p>\n<p>Expected output:<br \/>\n8<br \/>\n4<br \/>\n2<br \/>\n1<br \/>\nsteps = 4<\/p>\n<p>Sample input: 1023<\/p>\n<p>Expected output:<br \/>\n3070<br \/>\n1535<br \/>\n4606<br \/>\n2303<br \/>\n6910<br \/>\n3455<br \/>\n10366<br \/>\n5183<br \/>\n15550<br \/>\n7775<br \/>\n23326<br \/>\n11663<br \/>\n34990<br \/>\n17495<br \/>\n52486<br \/>\n26243<br \/>\n78730<br \/>\n39365<br \/>\n118096<br \/>\n59048<br \/>\n29524<br \/>\n14762<br \/>\n7381<br \/>\n22144<br \/>\n11072<br \/>\n5536<br \/>\n2768<br \/>\n1384<br \/>\n692<br \/>\n346<br \/>\n173<br \/>\n520<br \/>\n260<br \/>\n130<br \/>\n65<br \/>\n196<br \/>\n98<br \/>\n49<br \/>\n148<br \/>\n74<br \/>\n37<br \/>\n112<br \/>\n56<br \/>\n28<br \/>\n14<br \/>\n7<br \/>\n22<br \/>\n11<br \/>\n34<br \/>\n17<br \/>\n52<br \/>\n26<br \/>\n13<br \/>\n40<br \/>\n20<br \/>\n10<br \/>\n5<br \/>\n16<br \/>\n8<br \/>\n4<br \/>\n2<br \/>\n1<br \/>\nsteps = 62<\/p>\n<p>&nbsp;<\/p>\n<h2>Scenario 2<\/h2>\n<p>Listen to this story: a boy and his father, a computer programmer, are playing with wooden blocks. They are building a pyramid.<\/p>\n<p>Their pyramid is a bit weird, as it is actually a pyramid-shaped wall &#8211; it&#8217;s flat. The pyramid is stacked according to one simple principle: each lower layer contains one block more than the layer above.<\/p>\n<p>The figure illustrates the rule used by the builders:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/edube.org\/uploads\/media\/default\/0001\/01\/26238ebafe7c913f177040785f30d7dde4a1c69a.png\" width=\"300\" \/><\/p>\n<p>Your task is to write a program which reads the number of blocks the builders have, and outputs the height of the pyramid that can be built using these blocks.<\/p>\n<p>Note: the height is measured by the number of <strong>fully completed layers<\/strong> &#8211; if the builders don&#8217;t have a sufficient number of blocks and cannot complete the next layer, they finish their work immediately.<\/p>\n<p>Test your code using the data we&#8217;ve provided.<\/p>\n<p>&nbsp;<\/p>\n<h2>Test Data<\/h2>\n<p>&nbsp;<\/p>\n<p>Sample input: <code>6<\/code><\/p>\n<p>Expected output: <code>The height of the pyramid: 3<\/code><\/p>\n<hr \/>\n<p>Sample input: <code>20<\/code><\/p>\n<p>Expected output: <code>The height of the pyramid: 5<\/code><\/p>\n<hr \/>\n<p>Sample input: <code>1000<\/code><\/p>\n<p>Expected output: <code>The height of the pyramid: 44<\/code><\/p>\n<hr \/>\n<p>Sample input: <code>2<\/code><\/p>\n<p>Expected output: <code>The height of the pyramid: 1<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 3<\/p>\n<p>Create a program with a <code>for<\/code> loop and a <code>break<\/code> statement. The program should iterate over characters in an email address, exit the loop when it reaches the <code>@<\/code> symbol, and print the part before <code>@<\/code> on one line. Use the skeleton below:<\/p>\n<p>&nbsp;<\/p>\n<p>Solutions 1<\/p>\n<pre class=\"lang:default decode:true\">c0=int(input(\"c0= \"))\r\ni=0\r\nwhile c0 != 1:\r\n    if c0%2==0:\r\n        c0\/=2\r\n    else:\r\n        c0=3*c0 + 1\r\n    c0=int(c0)\r\n    print(c0)\r\n    i+=1\r\nprint(\"steps = \", i)<\/pre>\n<p>Solution 2<\/p>\n<pre class=\"lang:default decode:true\">blocks = int(input(\"Enter the number of blocks: \"))\r\n\r\nheight=0\r\ni=0\r\n\r\nwhile blocks&gt;0:\r\n    height+=1\r\n    blocks-=height\r\n    print (height, blocks)\r\n    if height+1 &gt; blocks: \r\n        break\r\n    \r\nprint(\"The height of the pyramid:\", height)<\/pre>\n<p>Solution 3<\/p>\n<pre class=\"lang:default decode:true \">for ch in \"john.smith@pythoninstitute.org\":\r\n    if ch == \"@\":\r\n        break\r\n    print(ch, end=\"\")<\/pre>\n<p>&nbsp;<\/p>\n<p>Exercise 5<\/p>\n<p>What is the output of the following code?<\/p>\n<pre class=\"lang:default decode:true\">n = 3\r\n\r\nwhile n &gt; 0:\r\n  print(n + 1)\r\n  n -= 1\r\nelse:\r\n  print(n)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol5\"><code class=\"codep \">4<br \/>\n3<br \/>\n2<br \/>\n0<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 6<\/p>\n<p>What is the output of the following code?<\/p>\n<pre class=\"lang:default decode:true\">n = range(4)\r\n\r\nfor num in n:\r\n  print(num - 1)\r\nelse:\r\n  print(num)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol6\"><code class=\"codep \">-1<br \/>\n0<br \/>\n1<br \/>\n2<br \/>\n3<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Exercise 7<\/p>\n<p>What is the output of the following code?<\/p>\n<pre class=\"lang:default decode:true\">for i in range(0, 6, 3):\r\n  print(i)<\/pre>\n<p>&nbsp;<\/p>\n<p id=\"sol7\"><code class=\"codep \">0<br \/>\n3<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Ex. 8<\/p>\n<div class=\"result-pane--question-result-pane-wrapper--2bGiz\">\n<div class=\"result-pane--question-result-pane--sIcOh result-pane--accordion-panel--TEJg7\">\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following Python <strong>keywords<\/strong> is used as an <strong>empty statement<\/strong> that does nothing?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>pass<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>None<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>return<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>continue<\/strong><\/code><\/p>\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>pass<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<p>&nbsp;<\/p>\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li><code><strong>pass<\/strong><\/code> is a <strong>null<\/strong> statement and can be used as a placeholder where Python&#8217;s syntax requires an instruction, even if it doesn&#8217;t have to perform any action. For example, you can use <code><strong>pass<\/strong><\/code> as a temporary placeholder for the body of a newly defined function:\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true \">def fun():\r\n  pass<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"domain-pane--domain-pane--Pw9dK\" data-purpose=\"domain-pane\">Ex. 9<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div data-purpose=\"domain-pane\">\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>Which of the following statements about the <strong>arguments<\/strong> <strong>of a function<\/strong> are <strong>true<\/strong>?(Select two answers)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li><em>The default value of a parameter cannot be overridden by a new argument when the function is invoked<\/em><\/li>\n<li><em>Positional arguments must be passed according to the order of the function parameters<\/em><\/li>\n<li><em>You cannot pass a mix of positional and keyword arguments to a function<\/em><\/li>\n<li><em>Keyword arguments must be passed to functions after any required positional arguments<\/em><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct selection<\/span><\/div>\n<div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Positional arguments must be passed according to the order of the function parameters<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><em>Keyword arguments must be passed to functions after any required positional arguments<\/em><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>It is <strong>true <\/strong>that <strong>positional<\/strong> arguments can be passed to a function along with <strong>keyword<\/strong> arguments. You can combine <strong>positional<\/strong> and <strong>keyword<\/strong> arguments as long as they are passed to the function in the correct order: <strong>keyword<\/strong> arguments must follow <strong>positional<\/strong> arguments.<\/li>\n<li>It is <strong>true<\/strong> that the <strong>default<\/strong> value of a parameter can\u00a0 be overridden. A <strong>default<\/strong> <strong>parameter<\/strong> can be overridden by a new argument when a function is invoked.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 10<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"lang:default decode:true\">str = \"Monty\"\r\n\u00a0\r\nfor i in str:\r\n  print(i)\r\n  break\r\nelse:\r\n  print(\"Python\")\r\n<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<pre class=\"lang:default decode:true\">Python\r\n\r\nM\r\n\r\nM\r\nPython\r\n\r\nMonty\r\nPython<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<p><span class=\"pln\"><code>M<\/code> <\/span><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>break<\/strong><\/code> statement unconditionally terminates the program during the <strong>first<\/strong> iteration of the <code><strong>for<\/strong><\/code> loop, after the letter <code><strong>M<\/strong><\/code> is printed on the console. The <code><strong>else<\/strong><\/code> statement\u00a0 is not executed because the loop is terminated by a <code><strong>break<\/strong><\/code>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Ex. 11<\/p>\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">str = \"XyZ\"\r\nc = \"x\"\r\n\r\n\r\nwhile c not in str:\r\n  print(\"A\")\r\n  break\r\nelse:\r\n  print(\"B\")<\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-incorrect--vFyOv\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>AB<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>B<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code>A<\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>The code has no output<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\"><\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><\/div>\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>A<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-skipped--1NDPn\" data-purpose=\"answer\">\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code>\"<strong>x<\/strong>\"<\/code> (lowercase) character is not present in the string <code><strong>str<\/strong><\/code>. Consequently, the condition of the <code><strong>while<\/strong><\/code> statement is <code><strong>True<\/strong><\/code>:<br \/>\n<strong>step by step<\/strong>Step 1 &#8211;\u00a0 <code><strong>while c not in str:<\/strong><\/code>Step 2 &#8211; <code><strong>while \"x\" not in \"XyZ\":<\/strong><\/code><\/p>\n<p>Result &#8211; <code><strong>while True:<\/strong><\/code><\/li>\n<\/ul>\n<ul>\n<li>The loop prints the letter <code><strong>A<\/strong><\/code> and then terminates due to the <code><strong>break<\/strong><\/code> statement. Also, executing <code><strong>break<\/strong><\/code> prevents the program from executing the <code><strong>else<\/strong><\/code> clause as well.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div data-purpose=\"domain-pane\"><\/div>\n<div data-purpose=\"domain-pane\">Ex. 12<\/div>\n<div data-purpose=\"domain-pane\">\n<div class=\"result-pane--question-header-wrapper--3DCpC\">\n<div class=\"result-pane--question-header--pTUOx\">\n<div id=\"question-prompt\" class=\"result-pane--question-format--PBvdY ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>What is the output of the following code snippet?<\/p>\n<div class=\"ud-component--base-components--code-block\">\n<div>\n<pre class=\"lang:default decode:true\">for ch in \"Python\":\r\n  pass\r\n\r\nprint(ch)<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--answer-result-pane--Niazi\">\n<div class=\"answer-result-pane--answer-correct--PLOEU\" data-purpose=\"answer\">\n<div><span class=\"result-pane--answer-by-user-label--PSH86 ud-heading-xs\" data-purpose=\"answer-result-header-user-label\">Correct answer<\/span><\/div>\n<div class=\"answer-result-pane--answer-body--cDGY6\" data-purpose=\"answer-body\">\n<div id=\"answer-text\" class=\"ud-heading-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p><code><strong>n<\/strong><\/code><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<ul>\n<li>The <code><strong>pass<\/strong><\/code> statement does nothing. While the interpreter ignores the comments, it does not ignore the <code><strong>pass<\/strong><\/code> statement, but nothing happens when pass is executed. It serves as a <strong>NOP<\/strong> (<strong>N<\/strong>o <strong>OP<\/strong>eration) placeholder.<\/li>\n<\/ul>\n<ul>\n<li>In this code snippet, the only effect of the <code><strong>for<\/strong><\/code> loop is to assign a letter of the string <code>\"<strong>Python<\/strong>\"<\/code> to the variable <code><strong>ch<\/strong><\/code> at each iteration. The loop is executed <strong>six<\/strong> times, once for each letter of <code>\"<strong>Python<\/strong>\"<\/code>. During the last iteration, the variable <code><strong>ch<\/strong><\/code> receives the letter <code><strong>n<\/strong><\/code>, which is then output to the console by the <code><strong>print<\/strong><\/code> statement.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"result-pane--question-result-pane-wrapper--2bGiz\">\n<div class=\"result-pane--question-result-pane--sIcOh result-pane--accordion-panel--TEJg7\">\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div data-purpose=\"domain-pane\">\n<div class=\"result-pane--question-result-pane-expanded-content--Og5Vc\">\n<div class=\"result-pane--question-related-fields--c3m--\">\n<div class=\"overall-explanation-pane--overall-explanation--G-hLQ ud-form-group\">\n<div id=\"overall-explanation\" class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\">\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"ud-text-md rt-scaffolding\" data-purpose=\"safely-set-inner-html:rich-text-viewer:html\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. There are two types of loops in Python: while and for:<\/p>\n","protected":false},"author":2,"featured_media":5930,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5136"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5136"}],"version-history":[{"count":30,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5136\/revisions"}],"predecessor-version":[{"id":5138,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5136\/revisions\/5138"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media\/5930"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5136"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}