If..Else Statements

If..Else Statements in Python Programming

Conditional statements are an essential part of programming languages. They allow developers to execute code based on certain conditions. One such condition statement is the if..else statement. In this article, we will discuss what if..else statements are, how they work in Python programming, and how to use them in various situations.

What are If..Else Statements?

If..else statement is a control structure in programming that performs an action based on the evaluation of a condition. The condition must be either true or false. If it is true, the if block statements will execute, and if it is false, the else block statements will execute.

How to Use If..Else Statements in Python

In Python, the if..else statement follows a specific syntax. The syntax is as follows:

if condition:
    # Executes when the condition is true
else:
    # Executes when the condition is false

The condition is an expression that evaluates to either a True or False value. If the condition is true, the code block inside the if statement executes, and if it is false, the code block inside the else statement executes.

Example:

Let’s take an example where we will ask a user to enter a number. If the number is even, we will print “The number is even,” and if it is odd, we will print “The number is odd.”

number = int(input("Enter a number: "))

if number % 2 == 0:
    print("The number is even")
else:
    print("The number is odd")

In the above example, we have used the modulo operator to check if the number is even or odd. If the number is divisible by 2, then it is even, and if it is not divisible by 2, it is odd.

Nested If..Else Statements

Nested if..else statements are used when we want to check multiple conditions. In this case, the if block contains another if..else statement. If the first condition is true, we move to the next condition, and so on.

Example:

Let’s take an example where we will ask the user to enter their age. Based on their age, we will check if they are eligible to vote or not, and then we will check their eligibility to drink alcohol.

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

if age < 18:
    print("You are not eligible to vote, and you cannot drink alcohol.")
else:
    if age < 21:
        print("You are eligible to vote, but you cannot drink alcohol.")
    else:
        print("You are eligible to vote and drink alcohol.")

In the above example, we have used two nested if..else statements to check eligibility for both voting and drinking alcohol.

If..Elif..Else Statements

If..elif..else structures are used when we have multiple conditions to check. In this case, the if block contains multiple elif statements, and the else statement is optional.

Example:

Let's take an example where we will ask the user to enter their marks. Based on their marks, we will check their grades.

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

if marks >= 90:
    print("Your grade is A+")
elif marks >= 80:
    print("Your grade is A")
elif marks >= 70:
    print("Your grade is B+")
elif marks >= 60:
    print("Your grade is B")
elif marks >= 50:
    print("Your grade is C")
else:
    print("Your grade is F")

In the above example, we have used multiple elif statements to check the grades based on the marks.

Conclusion

Conditional statements are an essential part of Python programming. The If..else statement is used to perform an action based on the evaluation of a condition. It is used in various situations such as nested if..else statements and if..elif..else statements. By mastering these statements, programmers can handle complex problems with ease and write efficient and robust code.

Leave a Reply

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

Scroll to Top