Python Programming - Nested If Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better.

Python Programming – Nested If Statement

There may be a situation when you want to check for another condition after one condition evaluates to true. In such a situation, you can use the nested if construct. In a nested, if construct, you can have an if…elif…else construct inside another if…elif..else construct.

Example 16.
Program to test whether the entered character is an alphabetic character or not.

Suppose you want to test the ASCII code of an entered character to check if it is an alphabetic character. After checking this, you would like to further check if it is in lowercase or uppercase. Knowing that the ASCII codes for the uppercase letters are in the range of 65-90, and those for lowercase letters are in the range of 97-122, a program shown in Figure 5.20 establishes this logic.

# Program to test – character is alphabetic character or not
ch = input(“Enter an alphabetic character: “)
if (ch>=’A’ and ch<=’Z’):
print(“The character is an upper-case letter”)
elif (ch>=’a’ and ch<=’z’):
print(“The character is a lower-case letter”)
else:
print(“This is not an alphabetic character!”)RUN
>>>
Enter an alphabetic character: A
The character is an upper-case letter
>>>
Enter an alphabetic character: b
The character is a lower-case letter
>>>
Enter an alphabetic character: 9
This is not an alphabetic character!
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *