{"id":33230,"date":"2020-03-16T12:41:58","date_gmt":"2020-03-16T07:11:58","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=33230"},"modified":"2020-03-16T12:41:58","modified_gmt":"2020-03-16T07:11:58","slug":"indentation-in-python-with-examples","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/indentation-in-python-with-examples\/","title":{"rendered":"Indentation in Python with Examples"},"content":{"rendered":"<p>It is generally good practice for you to not mix tabs and spaces when coding in Python. Doing this will possibly cause a <span style=\"color: #ff0000;\">TabError<\/span>, and your program will crash. Be consistent once you code &#8211; choose either to indent using tabs or spaces and follow your chosen convention throughout your program.<\/p>\n<h2 id=\"code-blocks-and-indentation\">Code Blocks and Indentation<\/h2>\n<p>One of the foremost distinctive features of Python is its use of indentation to mark blocks of code. Consider the if-statement from our simple password-checking program:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">if pwd == &#039;apple&#039;:<br\/>    print(&#039;Logging on ...&#039;)<br\/>else:<br\/>    print(&#039;Incorrect password.&#039;)<br\/><br\/>print(&#039;All done!&#039;)<\/code><\/pre> <\/div>\n<p>&nbsp;<\/p>\n<p>The lines print(<strong>\u2018Logging on \u2026<\/strong>\u2019) and print(\u2018<strong>Incorrect password<\/strong>.\u2019) are two separate code blocks. These ones happen to be only one line long, but Python allows you to write code blocks consisting of any number of statements.<\/p>\n<p>To indicate a block of code in Python, you want to indent each line of the block by an equivalent amount. the 2 blocks of code in our example if-statement are both indented four spaces, which may be a typical amount of indentation for Python.<\/p>\n<p>In most other programming languages, indentation is employed only to assist make the code look pretty. But in Python, it&#8217;s required for indicating what block of code a press release belongs to. as an example , the ultimate print(\u2018<strong>All done!<\/strong>\u2019) isn&#8217;t indented, then isn&#8217;t a part of the else-block.<\/p>\n<p>Programmers conversant in other languages often bridle at the thought that indentation matters: Many programmers just like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to form their code readable. Python simply takes this concept one step further and provides aiming to the indentation.<\/p>\n<h2 id=\"if-elif-statements\">If\/elif-statements<\/h2>\n<p>An if\/elif-statement may be a generalized if-statement with quite one condition. it&#8217;s used for creating complex decisions. for instance , suppose an airline has the subsequent \u201c<strong>child<\/strong>\u201d ticket rates: Kids 2 years old or younger fly for free of charge , kids older than 2 but younger than 13 pay a reduced child fare, and anyone 13 years or older pays a daily adult fare. the subsequent program determines what proportion a passenger should pay:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\"># airfare.py<br\/>age = int(input(&#039;How old are you? &#039;))<br\/>if age &lt;= 2:<br\/>    print(&#039; free&#039;)<br\/>elif 2 &lt; age &lt; 13:<br\/>    print(&#039; child fare)<br\/>else:<br\/>    print(&#039;adult fare&#039;)<\/code><\/pre> <\/div>\n<p>After Python gets age from the user, it enters the<strong> if\/elif-statement<\/strong> and checks each condition one after the opposite within the order they&#8217;re given. So first it checks if age is a smaller amount than 2, and if so, it indicates that the flying is free and jumps out of the elif-condition. If age isn&#8217;t but 2, then it checks subsequent elif-condition to ascertain if age is between 2 and 13. If so, it prints the acceptable message and jumps out of the <strong>if\/elif-statement.<\/strong> If neither the if-condition nor the<strong> elif-<\/strong>condition is <strong>True<\/strong>, then it executes the code within the else-block.<\/p>\n<h2 id=\"conditional-expressions\">Conditional expressions<\/h2>\n<p>Python has another <strong>logical operator<\/strong> that some programmers like (and some don\u2019t!). It\u2019s essentially a shorthand notation for if-statements which will be used directly within expressions. Consider this code:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">food = input(&quot;What&#039;s your favorite food? &quot;)<br\/>reply = &#039;yuck&#039; if food == &#039;lamb&#039; else &#039;yum&#039;<\/code><\/pre> <\/div>\n<p>The expression on the right-hand side of = within the second line is named a conditional expression, and it evaluates to either \u2018yuck\u2019 or \u2018yum\u2019. It\u2019s like the following:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">food = input(&quot;What&#039;s your favorite food? &quot;)<br\/>if food == &#039;lamb&#039;:<br\/>   reply = &#039;yuck&#039;<br\/>else:<br\/>   reply = &#039;yum&#039;<\/code><\/pre> <\/div>\n<p>Conditional expressions are usually shorter than the corresponding if\/else-statements, although almost as flexible or easy to read. generally , you ought to use them once they make your code simpler.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is generally good practice for you to not mix tabs and spaces when coding in Python. Doing this will possibly cause a TabError, and your program will crash. Be consistent once you code &#8211; choose either to indent using tabs or spaces and follow your chosen convention throughout your program. Code Blocks and Indentation [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":33256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4148],"tags":[86831,86761],"class_list":["post-33230","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-indentation-in-python","tag-python"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=33230"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/33230\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/33256"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=33230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=33230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=33230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}