{"id":229,"date":"2020-10-05T02:29:20","date_gmt":"2020-10-05T02:29:20","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=229"},"modified":"2025-03-26T10:21:03","modified_gmt":"2025-03-26T10:21:03","slug":"python-continue","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-basics\/python-continue\/","title":{"rendered":"Python continue"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about the Python <code>continue<\/code> statement and how to use it to control the loop.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-continue-statement'>Introduction to the Python continue statement <a href=\"#introduction-to-the-python-continue-statement\" class=\"anchor\" id=\"introduction-to-the-python-continue-statement\" title=\"Anchor for Introduction to the Python continue statement\">#<\/a><\/h2>\n\n\n\n<p>The <code>continue<\/code> statement is used inside a <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-for-range\/\">for<\/a><\/code> loop or a <code><a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-while\/\">while<\/a><\/code> loop. The <code>continue<\/code> statement skips the current iteration and starts the next one.<\/p>\n\n\n\n<p>Typically, you use the <code>continue<\/code> statement with an <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-if\/\"><code>if<\/code> statement<\/a> to skip the current iteration once a condition is <code>True<\/code>.<\/p>\n\n\n\n<p>The following shows how to use the <code>continue<\/code> statement in a <code>for<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> index <span class=\"hljs-keyword\">in<\/span> range(n):\n    <span class=\"hljs-keyword\">if<\/span> condition:\n       <span class=\"hljs-keyword\">continue<\/span>\n    <span class=\"hljs-comment\"># more code here<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And the following illustrates how to use the <code>continue<\/code> statement in a <code>while<\/code> loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">while<\/span> condition1:\n    <span class=\"hljs-keyword\">if<\/span> condition2:\n        <span class=\"hljs-keyword\">continue<\/span>\n    <span class=\"hljs-comment\"># more code here<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='using-python-continue-in-a-for-loop-example'>Using Python continue in a for loop example <a href=\"#using-python-continue-in-a-for-loop-example\" class=\"anchor\" id=\"using-python-continue-in-a-for-loop-example\" title=\"Anchor for Using Python continue in a for loop example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to use the <code>for<\/code> loop to display even numbers from 0 to 9:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">for<\/span> index <span class=\"hljs-keyword\">in<\/span> range(<span class=\"hljs-number\">10<\/span>):\n    <span class=\"hljs-keyword\">if<\/span> index % <span class=\"hljs-number\">2<\/span>:\n        <span class=\"hljs-keyword\">continue<\/span>\n\n    print(index)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pythontutorial.net\/playground\/?q=Zm9yIGluZGV4IGluIHJhbmdlKDEwKToKICAgIGlmIGluZGV4ICUgMjoKICAgICAgICBjb250aW51ZQoKICAgIHByaW50KGluZGV4KQ%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-number\">2<\/span>\n<span class=\"hljs-number\">4<\/span>\n<span class=\"hljs-number\">6<\/span>\n<span class=\"hljs-number\">8<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, iterate over a range of numbers from 0 to 9 using a <code>for<\/code> loop with the <code>range()<\/code> function.<\/li>\n\n\n\n<li>Second, if the index is an odd number, skip the current iteration and start a new one. Note that the <code>index % 2<\/code> returns <code>1<\/code> if the <code>index<\/code> is an odd number and 0 if the <code>index<\/code> is an even number.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-python-continue-in-a-while-loop-example'>Using Python continue in a while loop example <a href=\"#using-python-continue-in-a-while-loop-example\" class=\"anchor\" id=\"using-python-continue-in-a-while-loop-example\" title=\"Anchor for Using Python continue in a while loop example\">#<\/a><\/h2>\n\n\n\n<p>The following example shows how to use the <code>continue<\/code> statement to display odd numbers between 0 and 9 to the screen:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-comment\"># print the odd numbers <\/span>\ncounter = <span class=\"hljs-number\">0<\/span>\n<span class=\"hljs-keyword\">while<\/span> counter &lt; <span class=\"hljs-number\">10<\/span>:\n    counter += <span class=\"hljs-number\">1<\/span>\n\n    <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">not<\/span> counter % <span class=\"hljs-number\">2<\/span>:\n        <span class=\"hljs-keyword\">continue<\/span>\n\n    print(counter)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-number\">1<\/span>\n<span class=\"hljs-number\">3<\/span>\n<span class=\"hljs-number\">5<\/span>\n<span class=\"hljs-number\">7<\/span>\n<span class=\"hljs-number\">9<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the <code>counter<\/code> variable with an initial value of zero<\/li>\n\n\n\n<li>Second, start the loop as long as the <code>counter<\/code> is less than 10. <\/li>\n\n\n\n<li>Third, inside the loop, increase the <code>counter<\/code> by one in each iteration. If the <code>counter<\/code> is an even number, skip the current iteration. Otherwise, display the <code>counter<\/code> to the screen.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the Python <code>continue<\/code> statement to skip the current iteration and start the next one.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=continue\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"229\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-continue\/\"\n\t\t\t\tdata-post-title=\"Python continue\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"229\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-basics\/python-continue\/\"\n\t\t\t\tdata-post-title=\"Python continue\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn about the Python continue statement and how to use it to control the loop. Introduction to the Python continue statement # The continue statement is used inside a for loop or a while loop. The continue statement skips the current iteration and starts the next one. Typically, you use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":37,"menu_order":17,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-229","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=229"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/229\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/37"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}