Nested if in Python


There may be a situation when you want to check for another condition after a condition to true. In such a situation, you can use the nested if construct.

It is perfectly all right if we write an entire if-else construct within either the body of the if statement or the body of an else statement.

Syntax :

if expression1:
    # Executes when the expression1 is true
    if expression2:
        # Executes when the expression2 is true
    else:
        # Nested if's else's
else:
    #Outer if's else

Example:

WAP to check the given natural number is even or odd.
n = int(input("Enter a number : "))
if n > 0:
    if n%2 == 0:
        print(n,"is Even Number")
    else:
        print(n,"is Odd Number")
else:
    print(n,"is not a natural Number")
OUTPUT 1:
Enter a number : 12
12 is Even Number

OUTPUT 2:
Enter a number : 13
13 is Odd Number

OUTPUT 3:
Enter a number : -12
-12 is not a natural Number

Next topic is while loop





 







Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-8527238801 , +91-9451396824

© 2017, prowessapps.in, All rights reserved