{"id":228,"date":"2019-05-31T08:56:04","date_gmt":"2019-05-31T08:56:04","guid":{"rendered":"http:\/\/askpython.com\/?p=228"},"modified":"2023-02-16T19:57:22","modified_gmt":"2023-02-16T19:57:22","slug":"python-statements","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-statements","title":{"rendered":"Python Statements &#8211; Multiline, Simple, and Compound Examples"},"content":{"rendered":"\n<p>Python statements are the code instructions that are executed by the Python interpreter. Python executes statements one by one as they appear in the code.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Statements Examples<\/h2>\n\n\n\n<p>Let&#8217;s look at some simple statement examples.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncount = 10  # statement 1\n\nclass Foo:  # statement 2\n    pass    # statement 3\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Multi-line Statements<\/h2>\n\n\n\n<p>Python statements are usually written in a single line. The newline character marks the end of the statement. If the statement is very long, we can explicitly divide it into multiple lines with the line continuation character (\\).<\/p>\n\n\n\n<p>Let&#8217;s look at some examples of multi-line statements.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmessage = &quot;Hello There.\\nYou have come to the right place to learn Python Programming.\\n&quot; \\\n          &quot;Follow the tutorials to become expert in Python. &quot; \\\n          &quot;Don&#039;t forget to share it with your friends too.&quot;\n\nmath_result = 1 + 2 + 3 + 4 + \\\n              5 + 6 + 7 + 8 + \\\n              9 + 10\n\nprint(message)\nprint(math_result)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"647\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/05\/python-statements-1024x647.png\" alt=\"Python Statements\" class=\"wp-image-229\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/05\/python-statements-1024x647.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/05\/python-statements-300x189.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/05\/python-statements-768x485.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/05\/python-statements.png 1498w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Python Statements<\/figcaption><\/figure>\n\n\n\n<p>Python supports multi-line continuation inside parentheses ( ), brackets [ ], and braces { }. The brackets are used by List and the braces are used by dictionary objects. We can use parentheses for expressions, <a href=\"https:\/\/www.askpython.com\/python\/tuple\/python-tuple\" data-type=\"post\" data-id=\"449\">tuples<\/a>, and strings.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nmessage = (&quot;Hello\\n&quot;\n           &quot;Hi\\n&quot;\n           &quot;Namaste&quot;)\n\nmath_result = (1 + 2 + 3 + 4 +\n               5 + 6 + 7 + 8 +\n               9 + 10)\n\nprime_numbers_tuple = (2, 3, 5, 7,\n                       11, 13, 17)\n\nlist_fruits = &#x5B;&quot;Apple&quot;, &quot;Banana&quot;,\n               &quot;Orange&quot;, &quot;Mango&quot;]\n\ndict_countries = {&quot;USA&quot;: &quot;United States of America&quot;, &quot;IN&quot;: &quot;India&quot;,\n                  &quot;UK&quot;: &quot;United Kingdom&quot;, &quot;FR&quot;: &quot;France&quot;}\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 statements in a single line?<\/h2>\n\n\n\n<p>We can use a semicolon (;) to have multiple statements in a single line.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nx = 1; y = 2; z = 3\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Simple Statements<\/h2>\n\n\n\n<p>Python simple statement is comprised of a single line. The multiline statements created above are also simple statements because they can be written in a single line. Let&#8217;s look at some important types of simple statements in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Python Expression Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ni = int(&quot;10&quot;)  # expression is evaluated and the result is assigned to the variable.\n\nsum = 1 + 2 + 3  # statement contains an expression to be evaluated first.\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Python Assignment Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncount = 10  # value is assigned to the variable, no expression is evaluated\n\nmessage = &quot;Hi&quot;\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Python Assert Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nassert 5 &lt; 10\nassert (True or False)\n<\/pre><\/div>\n\n\n<p>Read more at <a href=\"https:\/\/www.askpython.com\/python\/built-in-methods\/assertions-in-python\" data-type=\"post\" data-id=\"13010\">Python assertions<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Python pass Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef foo():\n    pass  # pass statement\n<\/pre><\/div>\n\n\n<p>Read more at <a href=\"https:\/\/www.askpython.com\/python\/python-pass-statement-keyword\" data-type=\"post\" data-id=\"295\">pass statement in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Python del Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nname = &quot;Python&quot;\ndel name  # del statement\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. Python return Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef foo():\n    return 10  # return statement\n<\/pre><\/div>\n\n\n<p>Recommended Read: <a href=\"https:\/\/www.askpython.com\/python\/python-return-statement\" data-type=\"post\" data-id=\"305\">return statement in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. Python yield Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef yield_statement():\n    yield &#039;Statement 1&#039;  # yield statement\n<\/pre><\/div>\n\n\n<p>Read more at <a href=\"https:\/\/www.askpython.com\/python\/python-yield-examples\" data-type=\"post\" data-id=\"607\">yield in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. Python raise Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef raise_example():\n    raise TypeError(&#039;Exception Example&#039;)  # raise statement\n<\/pre><\/div>\n\n\n<p>Read more about <a href=\"https:\/\/www.askpython.com\/python\/python-exception-handling\" data-type=\"post\" data-id=\"411\">exception handling in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">9. Python break Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers = &#x5B;1, 2, 3]\n\n\nfor num in numbers:\n    if num &gt; 2:\n        break  # break statement\n<\/pre><\/div>\n\n\n<p>Read more at <a href=\"https:\/\/www.askpython.com\/python\/python-break-statement\" data-type=\"post\" data-id=\"362\">Python break statement<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">10. Python continue Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumbers = &#x5B;1, 2, 3]\n\n\nfor num in numbers:\n    if num &gt; 2:\n        continue  # continue statement\n    print(num)\n<\/pre><\/div>\n\n\n<p>Further Reading: <a href=\"https:\/\/www.askpython.com\/python\/python-continue-statement\" data-type=\"post\" data-id=\"370\">Python continue statement<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">11. Python import Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport collections\nimport calendar as cal\nfrom csv import DictReader\n<\/pre><\/div>\n\n\n<p>Recommended Read: <a href=\"https:\/\/www.askpython.com\/python\/python-import-statement\" data-type=\"post\" data-id=\"2348\">import in Python<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">12. Python global Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nname = &quot;Python&quot;\n\n\ndef global_example():\n    global name  # global statement\n    name = &quot;Flask&quot;\n\n\nprint(name)  # prints Python\nglobal_example()\nprint(name)  # prints Flask\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">13. Python nonlocal Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef outer_function():\n    scope = &quot;local&quot;\n\n    def inner_function():\n        nonlocal scope  # nonlocal statement\n        scope = &quot;nonlocal&quot;\n        print(scope)\n\n    inner_function()\n    print(scope)\n\n\nouter_function()\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Python Compound Statements<\/h2>\n\n\n\n<p>Python compound statements contain a group of other statements and affect their execution. The compound statement generally spans multiple lines. Let&#8217;s briefly look into a few compound statements.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. Python if Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif 5 &lt; 10:\n    print(&quot;This will always print&quot;)\nelse:\n    print(&quot;Unreachable Code&quot;)\n<\/pre><\/div>\n\n\n<p>Recommended Read: <a href=\"https:\/\/www.askpython.com\/python\/python-if-else-elif-statement\" data-type=\"post\" data-id=\"312\">Python if-else statement<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. Python for Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfor n in (1, 2, 3):\n    print(n)\n<\/pre><\/div>\n\n\n<p>Further Reading: <a href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" data-type=\"post\" data-id=\"329\">Python for loop<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. Python while Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ncount = 5\nwhile count &gt; 0:\n    print(count)\n    count -= 1\n<\/pre><\/div>\n\n\n<p>Read more at <a href=\"https:\/\/www.askpython.com\/python\/python-while-loop\" data-type=\"post\" data-id=\"350\">Python while loop<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. Python try Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ntry:\n    print(&quot;try&quot;)\nexcept ValueError as ve:\n    print(ve)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. Python with Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nwith open(&#039;data.csv&#039;) as file:\n    file.read()\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">6. Python Function Definition Statement<\/h3>\n\n\n\n<p>A <a href=\"https:\/\/www.askpython.com\/python\/python-functions\" data-type=\"post\" data-id=\"285\">python function<\/a> definition is an executable statement. Its execution binds the function name in the current local namespace to a function object. The function is executed only when it&#8217;s called.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef useless():\n    pass\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">7. Python Class Definition Statement<\/h3>\n\n\n\n<p>It&#8217;s an executable statement. <a href=\"https:\/\/www.askpython.com\/python\/oops\/python-classes-objects\" data-type=\"post\" data-id=\"712\">Python class<\/a> definition defines the class object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Data:\n    id = 0\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">8. Python Coroutines Function Definition Statement<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport asyncio\n\nasync def ping(url):\n    print(f&#039;Ping Started for {url}&#039;)\n    await asyncio.sleep(1)\n    print(f&#039;Ping Finished for {url}&#039;)\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Python statements are used by the Python interpreter to run the code. It&#8217;s good to know about the different types of statements in Python.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a rel=\"noreferrer noopener\" aria-label=\"Simple Statements (opens in a new tab)\" href=\"https:\/\/docs.python.org\/3.7\/reference\/simple_stmts.html#simple-statements\" target=\"_blank\">Simple Statements<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/reference\/compound_stmts.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Compound Statements (opens in a new tab)\">Compound Statements<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python statements are the code instructions that are executed by the Python interpreter. Python executes statements one by one as they appear in the code. Python Statements Examples Let&#8217;s look at some simple statement examples. Python Multi-line Statements Python statements are usually written in a single line. The newline character marks the end of the [&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-228","post","type-post","status-publish","format-standard","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/228","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=228"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}