{"id":295,"date":"2019-06-26T08:55:24","date_gmt":"2019-06-26T08:55:24","guid":{"rendered":"http:\/\/askpython.com\/?p=295"},"modified":"2022-07-12T16:35:45","modified_gmt":"2022-07-12T16:35:45","slug":"python-pass-statement-keyword","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-pass-statement-keyword","title":{"rendered":"Python pass Statement &#8211; pass Keyword in Python"},"content":{"rendered":"\n<p>Python pass statement is a no-operation statement. It&#8217;s used to create empty code blocks and empty functions.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python pass Statement Examples<\/h2>\n\n\n\n<p>Let&#8217;s look at some examples of Python pass statements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. pass statement in a code block<\/h3>\n\n\n\n<p>Let&#8217;s say we have to write a <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" data-type=\"post\" data-id=\"285\">function<\/a> to remove all the even numbers from a <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" data-type=\"post\" data-id=\"511\">list<\/a>. In this case, we will use for loop to traverse through the numbers in the list. <\/p>\n\n\n\n<p>If the number is divided by 2, then we do nothing. Else, we add it to a temporary list. Finally, return the temporary list having only odd numbers to the caller.<\/p>\n\n\n\n<p>Python doesn&#8217;t support empty code blocks. So we can use the pass statement here for the no-operation in the if-condition block.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef remove_evens(list_numbers):\n    list_odds = &#x5B;]\n    for i in list_numbers:\n        if i % 2 == 0:\n            pass\n        else:\n            list_odds.append(i)\n    return list_odds\n\n\nl_numbers = &#x5B;1, 2, 3, 4, 5, 6]\nl_odds = remove_evens(l_numbers)\nprint(l_odds)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: [1, 3, 5]<\/p>\n\n\n\n<p>Here we don&#8217;t need any operation in the if-condition block. So we have used the pass statement for the no-operation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. pass statement for an empty function<\/h3>\n\n\n\n<p>Python doesn&#8217;t have the concept of abstract functions. If we have to define an empty function, we can&#8217;t write it like this.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef foo():\n    # TODO - implement later\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>: <a href=\"https:\/\/www.askpython.com\/python\/python-indentation\" target=\"_blank\" rel=\"noreferrer noopener\" label=\"IndentationError (opens in a new tab)\">IndentationError<\/a>: expected an indented block<\/p>\n\n\n\n<p>We can use a pass statement to define an empty <a label=\"function (opens in a new tab)\" href=\"https:\/\/www.askpython.com\/python\/python-functions\" target=\"_blank\" rel=\"noreferrer noopener\">function<\/a>. The function will have a statement but it won&#8217;t do anything.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef foo():\n    pass\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Can we have multiple pass statements in a function?<\/h2>\n\n\n\n<p>Yes, we can have multiple pass statements in a function or a code block. It&#8217;s because the pass statement doesn&#8217;t terminate the function. Its only work is to provide an empty statement.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef bar():\n    pass\n    print(&#039;bar&#039;)\n    pass\n\n\nif True:\n    pass\n    pass\n    print(&#039;True&#039;)\nelse:\n    print(&#039;False&#039;)\n    pass\n    pass\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why do we need a pass statement?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python pass statement is very helpful in defining an empty function or an empty code block.<\/li><li>The most important use of the pass statement is to create a contract for classes and functions that we want to implement later on. For example, we can define a Python module like this:<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass EmployeeDAO:\n\n    def get_emp_by_id(self, i):\n        &quot;&quot;&quot;\n        TODO: implement this function later on\n        :param i: employee id\n        :return: employee object\n        &quot;&quot;&quot;\n        pass\n\n    def delete_emp(self, i):\n        pass\n\n\n# This function will read Employees CSV Data file and return list of Employees\ndef read_csv_file(file):\n    pass\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"496\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-pass-statement-1024x496.png\" alt=\"Python Pass Statement\" class=\"wp-image-360\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-pass-statement-1024x496.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-pass-statement-300x145.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-pass-statement-768x372.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/07\/python-pass-statement.png 1244w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Python Pass Statement<\/figcaption><\/figure>\n\n\n\n<p>We can proceed with the implementation. The third-party code knows the functions and methods that we will implement, so they can proceed with their implementation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/www.askpython.com\/python\/python-loops-in-python\" data-type=\"post\" data-id=\"555\">Loops in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/python-operators\" data-type=\"post\" data-id=\"265\">Operators in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/python-built-in-functions-brief-overview\" data-type=\"post\" data-id=\"32052\">Built-in functions in Python<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\" data-type=\"post\" data-id=\"712\">Python Classes<\/a><\/li><li><a href=\"https:\/\/www.askpython.com\/python-modules\/python-modules\" data-type=\"post\" data-id=\"1222\">Modules in Python<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Resources<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/tutorial\/controlflow.html#pass-statements\" target=\"_blank\" rel=\"noopener\">Python.org Docs<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python pass statement is a no-operation statement. It&#8217;s used to create empty code blocks and empty functions. Python pass Statement Examples Let&#8217;s look at some examples of Python pass statements. 1. pass statement in a code block Let&#8217;s say we have to write a function to remove all the even numbers from a list. In [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}