{"id":1322,"date":"2024-05-10T06:28:20","date_gmt":"2024-05-10T06:28:20","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=1322"},"modified":"2024-05-10T06:28:20","modified_gmt":"2024-05-10T06:28:20","slug":"what-will-be-the-output-of-the-following-python-code","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/what-will-be-the-output-of-the-following-python-code\/","title":{"rendered":"What Will Be The Output Of The Following Python Code?"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>i = 0\n\nwhile i &lt; 5:\n\n    print(i)\n\n    i += 1\n\n    if i == 3:\n\n        break\n\nelse:\n\n    print(0)<\/code><\/pre>\n\n\n\n<p>A) 0 1 2 0<\/p>\n\n\n\n<p>B) 0 1 2<\/p>\n\n\n\n<p>C) error<\/p>\n\n\n\n<p>D) None of the mentioned<\/p>\n\n\n\n<p>Correct Answer: (B). 0 1 2<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Will Be The Output Of The Following Python Code<\/h2>\n\n\n\n<p>The given Python code is a while loop that prints the value of <code>i<\/code> in each iteration until <code>i<\/code> becomes equal to 3. Let&#8217;s break down the code step by step:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>i = 0<\/code> &#8211; Initialize the variable <code>i<\/code> with the value 0.<\/li>\n\n\n\n<li>The while loop starts:<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The condition <code>i &lt; 5<\/code> is checked. Since <code>i<\/code> is initially 0, this condition is true.<\/li>\n\n\n\n<li>The code inside the loop is executed:\n<ul class=\"wp-block-list\">\n<li><code>print(i)<\/code> prints the current value of <code>i<\/code>, which is 0.<\/li>\n\n\n\n<li><code>i += 1<\/code> increments the value of <code>i<\/code> to 1.<\/li>\n\n\n\n<li>The condition <code>i == 3<\/code> is checked. Since <code>i<\/code> is now 1, this condition is false.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>   3. The loop repeats:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The condition <code>i &lt; 5<\/code> is checked. Since <code>i<\/code> is now 1, this condition is true.<\/li>\n\n\n\n<li>The code inside the loop is executed:\n<ul class=\"wp-block-list\">\n<li><code>print(i)<\/code> prints the current value of <code>i<\/code>, which is 1.<\/li>\n\n\n\n<li><code>i += 1<\/code> increments the value of <code>i<\/code> to 2.<\/li>\n\n\n\n<li>The condition <code>i == 3<\/code> is checked. Since <code>i<\/code> is now 2, this condition is false.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>   4. The loop repeats:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The condition <code>i &lt; 5<\/code> is checked. Since <code>i<\/code> is now 2, this condition is true.<\/li>\n\n\n\n<li>The code inside the loop is executed:\n<ul class=\"wp-block-list\">\n<li><code>print(i)<\/code> prints the current value of <code>i<\/code>, which is 2.<\/li>\n\n\n\n<li><code>i += 1<\/code> increments the value of <code>i<\/code> to 3.<\/li>\n\n\n\n<li>The condition <code>i == 3<\/code> is checked. Since <code>i<\/code> is now 3, this condition is true.<\/li>\n\n\n\n<li>The <code>break<\/code> the statement is executed, which immediately exits the loop.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>   5. The loop ends.<\/p>\n\n\n\n<p>So, the output of the code will be:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>0\n1\n2<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A) 0 1 2 0 B) 0 1 2 C) error D) None of the mentioned Correct Answer: (B). 0 1 2 What Will Be The Output Of The Following Python Code The given Python code is a while loop that prints the value of i in each iteration until i becomes equal to 3. &#8230; <a title=\"What Will Be The Output Of The Following Python Code?\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/what-will-be-the-output-of-the-following-python-code\/\" aria-label=\"More on What Will Be The Output Of The Following Python Code?\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":1332,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[224],"tags":[226],"class_list":["post-1322","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-question-answer","tag-what-will-be-the-output-of-the-following-python-code","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=1322"}],"version-history":[{"count":5,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1322\/revisions"}],"predecessor-version":[{"id":1329,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/1322\/revisions\/1329"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/1332"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=1322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=1322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=1322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}