Python Conditional Statements

What are conditional statements?

Conditional Statements are used to terminate or changes the flow of program.


If the condition is True then all the statements written in the condition will get executed and after completing the statements it proceeds with the program.


And if the condition is False then program continues without executing the statements (statements are ignored/skipped).


Python conditional statements 

Conditional Statements with Python are, 'if' conditional statement, alternative execution, chained or ladder conditional execution, nested conditionals.


Conditional execution - 'if' statement

'if' is the simplest form of conditional statement. Which has the following syntax.


Syntax


if condition:
    statements

Program Example

>>> if True:
print("The statement is True")

The statement is True
>>> if False:
print("The statement is False and will not be executed")

>>> 


Explanation:- First Of All the 'if' keyword is used to initiate the conditional execution, then the required condition is checked using Boolean operation which returns 'True' or 'False'.


Followed by a colon (:), which will tell the compiler that the condition checking is over, now it's time to write statements.


Then each statement is written in new line with an indentation(Tab spacing), if your 'Tab' key is broken then you can give 4 spaces, which works same as Tab.


Indentation tells the compiler that a particular set of instruction belongs to a particular statement.


If the condition is True then the statements are executed otherwise it is skipped in False condition. 



If you forget indentation then Python will give you an error sayin "SyntaxError: expected an indented block".


That's why indents are very very important in Python.


Use '#' symbol to write comments, all the things written after '#' will be ignored.


Program


>>> x = 5
>>> if x > 0:
print(x, " is positive")

5  is positive
>>>


---------------------------------------


>>> if x < 0:
print(x, "is negative")

>>> #condition is False, that's why nothing got printed
>>> 


---------------------------------------


There is no limit for the number of statements, but there should be at least one. When you do not want to write any statements, you can use 'pass' statement, which does nothing.


>>> if x == 100:
pass

>>>
---------------------------------------
 



Note:- Now on words we will write the codes in a Python file.

If you don't know how to create a Python file (Without installing any IDE), click here.



Alternative execution

In alternative execution, there are 2 possibilities 'if' and 'else'. In which one of the statement gets executed for sure. Depending on the condition.


When 'if' statement is 'True' then the content in the 'if' statement gets executed and the 'else' statement is ignored/skipped.


And if the 'if' statement is 'False' then the content in the 'if' statement is ignored and the content in the 'else' statement is executed.

Program

Input


x = 5
if x > 0:
    print(x, " is greater than 0")
else:
    print(x, " is less than 0")



Output

5  is greater than 0

---------------------------------------


Input

x = -5
if x > 0:
    print(x, " is greater than 0")
else:
    print(x, " is less than 0")

Output

-5  is less than 0

---------------------------------------


Python Chained conditions

In the chained conditions there are more then one possibilities of execution.


To make more possibilities, we make use of 'elif' conditional statement in between 'if' and 'else' statements. 


The  format of 'elif' conditional statement is same as that of 'if' conditional statement, only the keyword's are different. 

Program

Input


x = 5
if x > 0:
    print(x, " is greater than 0")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")


Output 


5  is greater than 0


---------------------------------------


Input

x = 0
if x > 0:
    print(x, " is greater than 0")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")

Output

0  is equal to 0



In the chained conditional execution, if there  are 2 or more 'True' statements then, the first 'True' statement will get executed.

Input

x = 5
if x % 2 == 0:
    print(x, " is an even number")
elif x % 2 != 0:
    print(x, " is odd number")
elif x > 0:
    print(x, " is greater than 0")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")

Output

5  is odd number



---------------------------------------


Input

x = 2
if x % 2 == 0:
    print(x, " is an even number")
elif x % 2 != 0:
    print(x, " is odd number")
elif x > 0:
    print(x, " is greater than 0")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")

Output

2  is an even number


Nested conditions in Python

Conditional Statements within another conditional statement are called as nested conditions. Which is also possible with python.


Following is an example for nested conditions in Python.


Program 

Input 

x = 2
if x > 0:
    print(x, " is greater than 0")
    if x % 2 == 0:
        print(x, " is an even number")
    elif x % 2 != 0:
        print(x, " is odd number")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")

Output 

2  is greater than 0
2  is an even number


---------------------------------------


Input

x = -2
if x > 0:
    print(x, " is greater than 0")
    if x % 2 == 0:
        print(x, " is an even number")
    elif x % 2 != 0:
        print(x, " is odd number")
elif x == 0:
    print(x, " is equal to 0")
else:
    print(x, " is less than 0")
    if x % 2 == 0:
        print(x, " is an even number")
    elif x % 2 != 0:
        print(x, " is odd number")

Output

-2  is less than 0
-2  is an even number



Thank you for reading this article...




Conclusion

Putting conditions in program will terminate or will change the flow of program.

Topics converted-

  • if conditional execution.
  • Alternate if and else conditional execution. if  False then else is executed.
  • Chained condition in which we can put more conditions using elif conditions.
  • Nested condition in which we put condition inside another condition.





Previous                          Next

If you have any query, feel free to ask.

Post a Comment

If you have any query, feel free to ask.

Post a Comment (0)