Applying Conditions in Python (If - else statement) - Ryzen Hunt

If - else statement in Python

Conditions in Python

In some programs, we need to define some conditions. Here we have described, how you can apply conditions in your Python program.

There are certain keywords available in Python that are used to define one or more conditions in your Python program. Comparison operators are also used to give a condition with the conditional keyword.

If - statement

This is the simplest conditional statement. An if - statement is written by using the if keyword. The syntax for if - statement is:

if (Condition):
    (Statement)

Example:
>>> a = 45
>>> b = 56
>>> if a != b :
              print("Condtion is True.")

Output:
Condition is True.


If - else statement

In case, the condition defined in the if-statement is not true, your program will through an error.  In the previous example, if the condition would be false then you would receive an error as output.
To manage that error we use else keyword. The else keyword catches everything that is not caught by preceding conditions. The syntax for the if-else statement is:

if (Condition):
     (Statement1)
else:
    (Statement2):

In case the condition is true, Statement1 would be executed, otherwise Statement2 is executed.

Example:

>>> a = 4567
>>> b = 4567
>>> if a != b :
            print("Condtion is True.")
>>> else:
              print("Condition is False.")

Output:
Condition is False.


Elif - statement

Sometimes we need to define more than one condition in a program. We use elif keyword. Using elif keyword we can define any number of conditions in our program. The syntax is:

if (Condition1):
     (Statement1)
elif (Condition2):
      (Statement2)
else:
     (Statement3):


Example:

>>> a = 67
>>>b = 68
>>> if a > b :
          print("a is greater than b.")
>>> elif a < b:
            print("b is greater than a.")
>>> else:
          print("a equals b.")

Output
b is greater than a.


In some cases, we need to give condition inside a condition. To perform this  action, we use Nested-if Statement. if Statement inside an if Statement is called Nasted-if Statement.

Example:
>>> a = 69
>>> if a >25:
            print("a is greater than 25")
            if a > 50:
                 print("and also greater than 50.")
            else:
                 print("but not greater than 50")

Output:
a is greater than 25
and also greater than 50.




Hope you learned something new here. Please share this with your friends and Don't forget to share your comments and valuable suggestion and feedback. If you have any question or doubt then plz ask we would answer in next post.
For visiting our Python Course Series click here and for HTML Course Series click here.



Suggested Product: Hardly I find anyone who is not a fan of Ironman. Most of the like to have the things that Tony Stark has in his movies. In addition, you all uses Mobiles, Tablets, PCs and laptop whole day that is not much good for your eyes.
So here I found eye glasses that Tony Stark put on in Avengers: Infinity War.  Putting on these glasses not only makes you to look smarter but also these glasses save your eyes from the harmful rays that come form your screen of your electronic devices.
Click Here or on the given image to buy these glasses at a special price from a reliable website.

You may like these posts

  1. To insert a code use <i rel="pre">code_here</i>
  2. To insert a quote use <b rel="quote">your_qoute</b>
  3. To insert a picture use <i rel="image">url_image_here</i>