{"id":47,"date":"2024-01-24T14:11:55","date_gmt":"2024-01-24T14:11:55","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=47"},"modified":"2024-01-24T14:11:59","modified_gmt":"2024-01-24T14:11:59","slug":"python-pass","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/24\/python-pass\/","title":{"rendered":"Python pass"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn how to use the Python&nbsp;<code>pass<\/code>&nbsp;statement as a placeholder.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python pass statement<\/h2>\n\n\n\n<p>Suppose that you have the following\u00a0<code>if...else<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>counter = 1 max = 10 if counter &lt;= max: counter += 1 else: <em># implement later<\/em> <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In the&nbsp;<code>else<\/code>&nbsp;clause, you haven\u2019t got any code yet. But you\u2019ll write code for this&nbsp;<code>else<\/code>&nbsp;clause later.<\/p>\n\n\n\n<p>In this case, if you run the code, you\u2019ll get a syntax error (<code>SyntaxError<\/code>).<\/p>\n\n\n\n<p>This is where the Python\u00a0<code>pass<\/code>\u00a0statement comes into play:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>counter = 1 max = 10 if counter &lt;= max: counter += 1 else: pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>pass<\/code>&nbsp;statement is a statement that does nothing. It\u2019s just a placeholder for the code that you\u2019ll write in the future.<\/p>\n\n\n\n<p>When you run the code that contains a&nbsp;<code>pass<\/code>&nbsp;statement, the Python interpreter will treat the&nbsp;<code>pass<\/code>&nbsp;statement as a single statement. As a result, it doesn\u2019t issue a syntax error.<\/p>\n\n\n\n<p>Technically, you can use the&nbsp;<code>pass<\/code>&nbsp;statement in many statement in Python.<\/p>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>pass<\/code>&nbsp;statement.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Using the Python pass statement with the if statement example<\/h3>\n\n\n\n<p>The following shows how to use the\u00a0<code>pass<\/code>\u00a0statement with an\u00a0<code>if<\/code>\u00a0statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>if condition: pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2) Using the Python pass statement with the for statement<\/h3>\n\n\n\n<p>This example shows how to use the\u00a0<code>pass<\/code>\u00a0statement in a\u00a0<code>for<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>for i in range(1,100): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3) Using the Python pass statement with the while statement<\/h3>\n\n\n\n<p>The following example shows how to use the\u00a0<code>pass<\/code>\u00a0statement with a\u00a0<code>while<\/code>\u00a0loop:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>while condition: pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4) Using the Python pass statement with functions and classes<\/h3>\n\n\n\n<p>Later, you\u2019ll learn how to define a\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>def fn(): pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>and a\u00a0class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>class Stream: pass<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>In these examples, you use the&nbsp;<code>pass<\/code>&nbsp;statement to mark the function and class empty.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn how to use the Python&nbsp;pass&nbsp;statement as a placeholder. Introduction to the Python pass statement Suppose that you have the following\u00a0if&#8230;else\u00a0statement: In the&nbsp;else&nbsp;clause, you haven\u2019t got any code yet. But you\u2019ll write code for this&nbsp;else&nbsp;clause later. In this case, if you run the code, you\u2019ll get a syntax error (SyntaxError). [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-47","post","type-post","status-publish","format-standard","hentry","category-3-control-flow"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/47","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=47"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/47\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/47\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=47"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=47"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=47"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}