if statement can contain another if statement. This kind of branching is called a nested if statement." /> if statement can contain another if statement. This kind of branching is called a nested if statement." />
 Krivalar Tutorials 
Krivalar Tutorials

Python - Nested if Statemment



<if...elif    for loop >







One if statement can contain another if statement. This kind of branching is called a nested if statement.

Example with nested if Statement in Python

A = int(input('Enter the User value: '))
if (A<50):
	if ((A%2)==0):
	     print('The number A is even')
	else:
	     print('The number A is Odd')
else:
	print('The number A is greater than 50')
  • The outer condition contains two branches. The first branch contains another if statement, which has two branches.
  • The second branch contains a simple print statement.

While executing the above program with various user iputs. We will get the following outputs.

Test_Case 1:

F:\>python nest.py
Enter the User value: 44
The number A is even

Tesst_Case 2:

F:\>python nest.py
Enter the User value: 35
The Number A is Odd

Test_Caee 3:

F:\>python nest.py
Enter the User value: 66
The Number A is greater than 50

Another Example

To simplify the nested conditional if statement, you have to use logical operators(and,or, not).

For Example:
X = int(input('Enter the value'))
if X>0:
	If X<10:
             print('X is a number and less than 10')
else:
	print('X is not less than 10')

The above code will be rewritten by using logical operator.

X = int(input('Enter the value'))
if X>0 and X<10:
        print('X is a number and less than 10')
else:
	print('X is not less than 10')

<if...elif    for loop >





















Searching using Binary Search Tree