Showing posts with label Decision-making. Show all posts
Showing posts with label Decision-making. Show all posts

Sunday, 18 August 2024

Python Nested If Examples

Nested-If Statements in Python

When we write if statements inside another if statement, it is called Nested If Statements. These can be very useful to make complex decisions in your program. They can be several levels deep. They are also a type of decision making statement in python. 

Followings are some popular examples related to usage of the python nested-if statements.

Example 1: Python program to print student's grade.

# Python program to print student's grade

score = int(input("Enter your marks:"))

if score >= 90:
    grade = 'A'
else:
    if score >= 80:
        grade = 'B'
    else:
        if score >= 70:
            grade = 'C'
        else:
            grade = 'F'

print("Grade:", grade)


Output

Enter your marks:85 Grade: B


Example 2: Python program to check weather conditions. 

# Checking Weather Conditions
temperature = 30
raining = False

if temperature > 25:
    if raining:
        print("It's warm but raining. Bring an umbrella!")
    else:
        print("It's warm and sunny. Enjoy the weather!")
else:
    if raining:
        print("It's cold and raining. Better stay indoors!")
    else:
        print("It's cold but dry. Dress warmly!")

Output

It's warm and sunny. Enjoy the weather!


Example 3: Python program to check user authentication (username and password).
 
# User Authentication
username = "admin"
password = "12345"

if username == "admin":
    if password == "12345":
        print("Access granted")
    else:
        print("Incorrect password")
else:
    print("Username not found")

Output
Access granted


Example 4: Python program to check eligibility for a loan based on given Age and Income.

# We assume that a person is eligible for loan if age is
# above 18 years and income greater than or equal to 40000.

age = int(input("Enter your age in years:"))
income = int(input("Enter your income in Rs:"))

if age > 18:
    if income > 40000:
        print("You are eligible for the loan")
    else:
        print("You are not eligible for the loan due to low income")
else:
    print("You are not eligible for the loan due to age restriction")

Output
Enter your age in years:30 Enter your income in Rs:50000 You are eligible for the loan


Example 5:  Python program to check Positive, Negative or Zero number.

# Checking for Positive, Negative, or Zero Number
number = int(input("Enter one number:"))
if number >= 0:
    if number == 0:
        print("The number is zero")
    else:
        print("The number is positive")
else:
    print("The number is negative")

Output
Enter a number:5 The number is positive

* * *

Friday, 29 June 2018

Python nested if statement

Python nested-if statement

Nested if statements in any programming language means an if statement inside another if statement. Python also allows us to nest if statements within if statements. i.e, we can write an if statement inside another if statement.

Syntax of nested-if statement:

if (condition 1):
   # Code to be executed when condition 1 is true
   if (condition 2):
      # Code to be executed when condition 2 is true
   # end of inner if Block
# end of outer if Block

Example of Nested-if Statement: Python program to check Positive, Negative or Zero number.

# Checking for Positive, Negative, or Zero Number
number = int(input("Enter one number:"))
if number >= 0:
    if number == 0:
        print("The number is zero")
    else:
        print("The number is positive")
else:
    print("The number is negative")

Output of program

Enter a number:5
The number is positive


if-elif-else ladder

The if statements are executed from the top to the bottom sequentially. When one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the last else statement written will be executed.

Syntax of if-elif-else

if (condition 1):
    statements for condition 1
elif (condition 2):
    statements for condition 2
.
.
else:
    statements for else

Example of Python if-elif-else ladder

number = int(input("Enter number in range of 1 to 5:"))
if (number == 1):
 print ("It’s One")
elif (number == 2):
 print ("It’s Two")
elif (number == 3):
 print ("It’s Three")
elif (number == 4):
 print ("It’s Four")
elif (number == 5):
 print ("It’s Five")
else:
 print ("It’s not in range of 1 to 5")

Output of program

Enter number in range of 1 to 5:3
It’s Three

Suggested Articles for you: