HOW EASY TO MAKE PYTHON IF-ELSE CONDITION !

HOW EASY TO MAKE PYTHON IF-ELSE CONDITION !

IF-ELSE Conditional Statement in Python

if 6 > 0:
    print("The number is positive")
else:
    print("The number is 0 or negative")

Let’s have an example:

number = float(input("Enter a random number: "))
if number % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

Nested if-else condition structure:

If condition 1:
    Statement 1
    If condition 2:
        Statement 2
    Else (2):
        Statement 3
Else (1):
    Statement 4
height = int(input("What is your height?"))
if height > 3:
    print("You can ride")
    age = int(input("Enter your age: "))
    if age <= 12:
        print("Pay 150 taka")
    else:
        print("Pay 300 taka")
else:
    print("You can't ride")

Another structure & example:

if condition1:
    # Code to execute if condition1 is true
    if condition2:
        # Code to execute if condition2 is true
    else:
        # Code to execute if condition2 is false
else:
    # Code to execute if condition1 is false
    if condition3:
        # Code to execute if condition3 is true
    else:
        # Code to execute if condition3 is false
age = 25
is_student = True

if age < 18:
    print("You are a minor.")
    if is_student:
        print("You are a student.")
    else:
        print("You are not a student.")
else:
    print("You are an adult.")
    if is_student:
        print("You are a student.")
    else:
        print("You are not a student.")
  • The outer if-else condition checks if age is less than 18.
  • If true, it then checks the is_student condition.
  • If is_student is true, it prints “You are a student.”
  • If is_student is false, it prints “You are not a student.”
  • If age is not less than 18, it then checks the is_student condition again.
  • If is_student is true, it prints “You are a student.”
  • If is_student is false, it prints “You are not a student.”
  • Validating multiple user inputs.
  • Determining actions based on a combination of different conditions.
  • Implementing complex business logic.

Elif Conditional Statement:

if condition1:
    # Code to execute if condition1 is true
elif condition2:
    # Code to execute if condition2 is true
elif condition3:
    # Code to execute if condition3 is true
else:
    # Code to execute if all conditions are false

Example:

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Explanation:

Important Points:

administrator

Related Articles

Leave a Reply

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