Python Nested If-Else Tutorial

What is Nested If-Else?

Nested if-else statements are if-else conditions placed inside another if or else block. They allow checking multiple conditions in a hierarchical manner for complex decision making.

Nested if-else statements, एक if या else block के अंदर दूसरा if-else condition होते हैं। ये जटिल निर्णय लेने के लिए कई conditions को hierarchical तरीके से जांचने में मदद करते हैं।

Syntax of Nested If-Else

if condition1:
    if condition2:
        # code if both conditions true
    else:
        # code if condition1 true and condition2 false
else:
    # code if condition1 false

10 Examples of Nested If-Else Statements

Example 1: Check Positive, Negative, or Zero
num = 0
if num >= 0:
    if num == 0:
        print('Zero')
    else:
        print('Positive')
else:
    print('Negative')

Checks if a number is zero, positive or negative using nested if-else.

संख्या शून्य, धनात्मक या ऋणात्मक है nested if-else से जांचता है।

Output:

Zero
Example 2: Student Grade Classification
marks = 75
if marks >= 50:
    if marks >= 75:
        print('Distinction')
    else:
        print('Pass')
else:
    print('Fail')

Classifies student grade as Distinction, Pass, or Fail based on marks.

छात्र के ग्रेड को distinction, pass या fail के रूप में वर्गीकृत करता है।

Output:

Pass
Example 3: Check Leap Year
year = 2024
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print('Leap Year')
        else:
            print('Not Leap Year')
    else:
        print('Leap Year')
else:
    print('Not Leap Year')

Determines if a year is a leap year with nested conditions.

वर्ष लीप वर्ष है या नहीं nested conditions से जांचता है।

Output:

Leap Year
Example 4: User Login Role Check
username = 'admin'
password = '1234'
if username == 'admin':
    if password == '1234':
        print('Admin login successful')
    else:
        print('Wrong password')
else:
    print('User not found')

Checks username and password with nested if-else for login validation.

लॉगिन सत्यापन के लिए यूज़रनेम और पासवर्ड nested if-else से जांचता है।

Output:

Admin login successful
Example 5: Check Number Range
num = 45
if num >= 0:
    if num <= 50:
        print('Number between 0 and 50')
    else:
        print('Number greater than 50')
else:
    print('Negative number')

Checks if number lies in certain ranges using nested conditions.

संख्या विभिन्न रेंज में है या नहीं nested conditions से जांचता है।

Output:

Number between 0 and 50
Example 6: Nested If with String Comparison
color = 'red'
if color == 'red':
    if len(color) == 3:
        print('Color is red and length is 3')
    else:
        print('Color is red but length is not 3')
else:
    print('Color is not red')

Checks color and its length with nested if-else.

रंग और उसकी लंबाई nested if-else से जांचता है।

Output:

Color is red and length is 3
Example 7: Nested If for Age and Gender
age = 20
gender = 'female'
if age >= 18:
    if gender == 'male':
        print('Adult male')
    else:
        print('Adult female')
else:
    print('Minor')

Classifies adult male or female or minor using nested conditions.

व्यक्ति को वयस्क पुरुष, वयस्क महिला या नाबालिग वर्गीकृत करता है।

Output:

Adult female
Example 8: Check Password Strength
password = 'abc123'
if len(password) >= 6:
    if any(char.isdigit() for char in password):
        print('Password is strong')
    else:
        print('Password must contain digits')
else:
    print('Password too short')

Checks password length and presence of digits for strength.

पासवर्ड की लंबाई और अंकों की उपस्थिति nested if-else से जांचता है।

Output:

Password is strong
Example 9: Nested If with Boolean Conditions
is_raining = True
have_umbrella = False
if is_raining:
    if have_umbrella:
        print('Take umbrella')
    else:
        print('Stay inside')
else:
    print('No rain today')

Decides actions based on rain and umbrella availability.

बारिश और छाता उपलब्धता के आधार पर कार्य करता है।

Output:

Stay inside
Example 10: Nested If for Exam Results
marks = 85
if marks >= 50:
    if marks >= 80:
        print('First Division')
    else:
        print('Second Division')
else:
    print('Failed')

Categorizes exam results into divisions or fail status.

परीक्षा परिणाम को डिवीजन या फेल स्थिति में वर्गीकृत करता है।

Output:

First Division