Python Condition Structure By albro

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@albro·
0.000 HBD
Python Condition Structure By albro
<center>![Python-Conditions-Structure.jpg](https://files.peakd.com/file/peakd-hive/albro/EoCfh4tuV5cyGt1NJN6W2EFWBsb9xzeLRs2E7g5988rsCw6YCP6VY3zJtqJk9C2EkJ7.jpg)</center>

<p>Table of Contents</p>
<ul>
<li><a href="#csip">Condition statement in Python</a><ul><li><a href="#hacd">How are conditions defined?</a></li></ul></li>
<li><a href="#tisip">The <code>if</code> statement in Python</a></li>
<li><a href="#tesip">The <code>else</code> statement in Python</a></li>
<li><a href="#pecs">Python <code>elif</code> conditional statement</a></li>
<li><a href="#spcip">Summary: Post condition in Python</a></li>
</ul>
<hr />
<p>We can make decisions with the help of conditions in programming languages. Using the <code>if</code> statement in Python, you can specify that if one or more conditions are met, to execute certain codes. In this post, I will talk about the <strong>condition in Python</strong> and the methods of using it.</p>
<p>In programming languages, just like the things we say every day, we have many buts and ifs! for example:</p>
<ul>
<li>If the user approves the message, log the information.</li>
<li>If the entered number is less than <i>100</i>, double it.</li>
<li>If the coefficient number is <i>3</i> or more than <i>999</i>, show a thank you message.</li>
<li>If the number is odd, display it and if it is even, subtract one from it.</li>
</ul>
<p>These are simple and different conditions that can be implemented using the <code>if</code> statement and the <code>if else</code> statement in Python. If you don't know what a statement is, you can see the post on <a href="https://peakd.com/hive-169321/@albro/types-of-programming-statements-by-albro">types of programming statements</a>.</p>
At first, we will get acquainted with the conditional statement and then we will learn 3 types of Python conditional statement.
<h3 id="csip">Condition statement in Python</h3>
<p>As you have noticed so far, with the help of a condition, if a certain situation exists, we will execute certain codes.</p>
<p>The diagram below shows how to execute the simplest type of conditional statement (with one condition).</p>

<center>![Single-IF-Overal-Diagram.jpg](https://files.peakd.com/file/peakd-hive/albro/EouD8Gw1vkNQjPZzEGZxGDyBkwVfgsyH1N94mPkKfNFsNU4iebTewRX6tyQ4ieyEAM1.jpg)</center>

<p id="hacd"><strong>How are conditions defined?</strong></p>
<p>To execute the condition, we must first specify a condition. By condition, I mean what we want other code to execute if it happens.</p>
<p>Conditions are either <code>True</code> or <code>False</code>. These types of values are called <strong>Boolean</strong> values.</p>
<p>To compare numerical values, we must use comparison operators. Comparison operators are:</p>
<ul>
<li><code>==</code> to check if two values are equal</li>
<li><code>!=</code> to compare two values that are not equal</li>
<li><code><</code> and <code>></code> for greater and less comparison</li>
<li><code><=</code> and <code>>=</code> to compare greater equal and less equal</li>
</ul>
<p>Suppose we want to do something special if the value of variable <code>i</code> is equal to <i>1</i>. So our condition becomes:</p>
<pre><code>i == 1</code></pre>
<p><i>The result of this comparison will be a Boolean expression.</i></p>
<p>If we want to use several conditions at the same time, we will use the keywords <code>and</code> and <code>or</code> between the conditions.</p>
<ul>
<li><code>and</code>: All conditions must be met at the same time.</li>
<li><code>or</code>: At least one of the conditions must be equal.</li>
</ul>
<p>Now it's time to get familiar with the structure of Python's conditional statements. Let's get acquainted with the 3 main condition structure in Python.</p>
<h3 id="tisip">The <code>if</code> statement in Python</h3>
<p>The foundation of all conditionals in Python begins with the <code>if</code> keyword. The structure of a simple condition in Python is as follows:</p>
<pre><code>if condition:
    doSomeThing()</code></pre>
<ul>
<li><code>condition</code> is our condition or conditions.</li>
<li>And <code>doSomeThing()</code> is a piece of code or a <a href="https://peakd.com/hive-169321/@albro/concept-of-programming-function-and">function</a> call.</li>
</ul>
<p>As you can see, defining the condition in Python is very simple and similar to conversational language!</p>
<p>Suppose we want to print a <code>Accepted</code> message if <code>i</code> is greater than <code>10</code>. For this, we do the following:</p>
<pre><code>if i >= 10:
    print("Accepted!")</code></pre>

<center>![python-condition-if-1.png](https://files.peakd.com/file/peakd-hive/albro/23tmmXSdPsTUHgELi6ma1WDAg8kvTHmpFG7XKfbUt7ft1MqE7hFGaYF6XeJUs4M3Zf5TH.png)</center>

<p>simply!</p>
<p>Now we want to print a <code>Accepted</code> message if the value of <code>i</code> is greater than <code>10</code> and the value of <code>j</code> is exactly equal to <code>25</code>:</p>
<pre><code>if i >= 10 and j == 25:
    print("Accepted!")</code></pre>

<center>![python-condition-if-2.png](https://files.peakd.com/file/peakd-hive/albro/EoAh2PNgqS5NzNRdG7YVbz27vCEKdc8Qvv3aYRsuo1P4FV3KDbQokjH4rbxcyjzdo9k.png)</center>

<h3 id="tesip">The <code>else</code> statement in Python</h3>
<p>In everyday conversations, we also use more complicated conditions. For example, we say:</p>

> If the weather is sunny tomorrow, I will go to the street and otherwise I will stay at home!

<p>Pay attention to the second part of the said phrase. What caused the separation of the second part of the phrase?</p>
<p>You guessed it: "Otherwise."</p>
<p>The equivalent of "Otherwise" in Python conditional structure is known as the <code>else</code> keyword and its usage is as follows:</p>
<pre><code>if condition:
    # code 1
else:
    # code 2</code></pre>
<p>If the <strong>condition</strong> was true (<code>True</code>), <strong>code 1</strong> will be executed, otherwise <strong>code 2</strong> will be executed.</p>
<p>Suppose we have a number in the <code>num</code> variable. We want to check if this number is an whole number or negative? Whole numbers are in the range <i>0</i> and greater than that.</p>
<p>So we have to check that if the value of the variable is greater than or equal to <i>0</i>, then it will be an whole number and otherwise it will be a negative number.</p>
<pre><code>num = 7
if num >= 0:
    print("Number is Whole!")
else:
    print("Number is Negative!")</code></pre>
<p>After <a href="https://peakd.com/hive-169321/@albro/how-codes-are-executed-methods">executing the code</a>, the condition is satisfied and the third line is executed:</p>

<center>![python-condition-if-else-1.png](https://files.peakd.com/file/peakd-hive/albro/23twAKVLEXRzwfSdvT39WoP8MSY3FJoXfkPZpcQG63sTzdP6M1xZHE43VQQm7ExDE7HR3.png)</center>

<p>If we set the value of the variable equal to a negative number, line 5 will be executed.</p>
<pre><code>num = -21
if num >= 0:
    print("Number is Whole!")
else:
    print("Number is Negative!")</code></pre>

<center>![python-condition-if-else-2.png](https://files.peakd.com/file/peakd-hive/albro/EoAgzdnMMFbkjJRWKjnbKQeLA5pGnNtphSFTvEVQ3RkvKYV9qLdQKjvRVweubbhkhCY.png)</center>

<p><strong>The order of executed lines in the <code>if...else</code> conditional structure above is as follows: 1 -> 2 -> 4 -> 5</strong></p>
<h3 id="pecs">Python <code>elif</code> conditional statement</h3>
<p>So far, we have learned about the basic structure of condition in Python. Another keyword is for conditional structures where we want to check several times and several different conditions.</p>
<p>The word <code>elif</code> is an abbreviation of the <code>else if</code> statement. Consider the following colloquial sentence:</p>

> If it's sunny tomorrow, I'll go outside, if it's rainy I'll go to the park, otherwise I'll stay at home.

<p>The above statement is divided into three parts: if [part 1] else if [part 2] otherwise [part 3]</p>
<p>Depending on the situation, the second part can be repeated over and over again. But in each conditional structure, the first part and the third part exist only once.</p>
<p>The <code>if...elif...else</code> structure in Python is defined as follows:</p>
<pre><code>if condition1:
    # code 1
elif condition2:
    # code 2
else:
    # code 3</code></pre>
<p>By repeating the second part, we can check different conditions over and over again; Similar to the following:</p>
<pre><code>if condition1:
    # code 1
elif condition2:
    # code 2
elif condition3:
    # code 3
elif condition4:
    # code 4
else:
    # code 5</code></pre>
<p>Consider the same numerical example as before, except that if the number is positive, it will display one message, if it is zero, another message, and otherwise (if it is negative) an associated message.</p>
<p>Let's get the number from the user:</p>
<pre><code>num = int(input())
if num > 0:
    print("Number is Positive!")
elif num == 0:
    print("Number is Zero!")
else:
    print("Number is Negative!")</code></pre>
<p>We run this code and give it some different numbers as input. You can see the result in the image below.</p>

<center>![python-condition-if-else-3.png](https://files.peakd.com/file/peakd-hive/albro/23sxpMFs9uJyHDJsaidkSUnUHiPYSkzyr5Aszc3yrAjUVQbWnQH5qRVkUUL32egkc8ETM.png)</center>

<h3 id="spcip">Summary: Post condition in Python</h3>
<p>In this post, we got acquainted with the structure of terms in Python. Conditions are used when we want a specific piece of code to be executed if a certain condition is met. The condition structure in Python uses three main keywords:</p>
<ul>
<li><code>if</code>: to start the conditional structure and check the condition</li>
<li><code>elif</code>: checks another condition if the main condition is not met. (optional)</li>
<li><code>else</code>: will be executed if the condition (or conditions) is not met. (optional)</li>
</ul>
<p>Conditionals are used a lot in any programming language. So, in addition to reviewing the examples in this post, try to code some examples for yourself.</p>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,