Whta are if, elif and else Conditional Statements?

         Conditional statements help a program make decisions based on certain conditions. They are like asking "if this happens, do that; otherwise, do something else."

We apply conditions by using Python's conditional operators. We use conditional operators to apply conditions. These operators help compare values and decide what action to take based on the result of the comparison.

Here are the common conditional operators:

  • == : checks if two values are equal.
  • != : checks if two values are not equal.
  • > : checks if one value is greater than another.
  • < : checks if one value is less than another.
  • >= : checks if one value is greater than or equal to another.
  • <= : checks if one value is less than or equal to another.

Before learning about conditional operators there is one main thing you have to understand i.e. what exactly these operators return.

For Example:

print(10 == 10)

then, the output will be:

True

If i do:

print(10 == 100)

then, the output will be:

False

So, it is clear that when we use conditional operators for any condition to check they always return true if the condition matches and return false if the condition does not match.

How to use if condation in Python?

Syntex:

if condation:
    # your block of code

Here, if checks if something is true or false. After typing if and the condition, you press Enter, and the next line starts with a space (called indentation). This space shows that the code belongs to the if.

You write all the code that should run when the if condition is true after the indentation. The indentation shows what code belongs to the if block.

For Example:

Problem: Write a program to check if a person can buy alcohol. A person can buy alcohol if they are 21 years old or older.
Solution:

age = int(input("Enter your age: "))

if age >= 21:
    print("You can buy alcohol!")

Output:

Enter your age: 30
You can buy alcohol!
  • If the user enters their age as 21 or more, the program will print "You can buy alcohol!".
  • If the user enters an age less than 21, the program will not print anything.

 

How to use else condation in Python?

        Always remember: The else statement always comes after the if condition. The code inside the else block only executes if the if condition fails.

 Syntex:

if condation:
    # code when condation True
else:
    # execute only when condation is false

 

Problem: Write a program to check if a person can buy alcohol. A person can buy alcohol if they are 21 years old or older. If the person is eligible, print a message saying they can buy alcohol. If they are not eligible, print a message saying they cannot buy alcohol.
Solution:

# Get the user's age
age = int(input("Enter your age: "))

# Check if the person can buy alcohol
if age >= 21:
    print("You can buy alcohol!")
else:
    print("You cannot buy alcohol.")

Output:

Enter your age: 18
You cannot buy alcohol.
Enter your age: 30
You can buy alcohol!

Explanation:

  1. Input: The program asks the user to enter their age.
  2. Condition Check: It checks if the entered age is 21 or older.
    • If true, it prints "You can buy alcohol!".
    • If false, it prints "You cannot buy alcohol."

 

How to use elif condation in Python?

         elif stands for "else if." It allows you to check multiple conditions in a sequence. You can use it when you have more than two possible conditions.

Always remember: The elif statement comes after the if statement. The code in the else block runs only if the if condition is not true.

 Syntex:

if condition1:
    # code to execute if condition1 is True
elif condition2:
    # code to execute if condition2 is True (and condition1 is False)
else:
    # code to execute if all previous conditions are False

 

 Problem: Write a program to check a person's eligibility for a driver's license based on their age. A person can get a driver's license if they are at least 16 years old but younger than 70. If the person is under 16, print a message saying they are too young. If they are 70 or older, print a message saying they are too old.
Solution:

# Get the user's age
age = int(input("Please enter your age: "))

# Check the eligibility for a driver's license
if age >= 16 and age < 70:
    print("You are eligible to get a driver's license.")
elif age < 16:
    print("You are too young to get a driver's license.")
else:
    print("You are too old to get a driver's license.")

Output:

Please enter your age: 10
You are too young to get a driver's license.
Please enter your age: 80
You are too old to get a driver's license.

Explanation:

  1. Input: The program asks the user for their age.
  2. If Statement:
    • If the user is 16 or older but younger than 70, it prints that they are eligible for a driver's license.
  3. Elif Statement:
    • If the user is under 16, it prints that they are too young to get a driver's license.
  4. Else Statement:
    • If the user is 70 or older, it prints that they are too old to get a driver's license.

 

Leave a comment

You must be logged in to post a comment.

0 Comments