Python IF-ELSE Condition – Decision Making Statements

In Python programming, the IF-ELSE condition is a fundamental construct that allows you to make decisions based on specific conditions. By using IF-ELSE statements, you can control the flow of your program and execute different blocks of code depending on whether certain conditions are met. In this blog post, we will delve into the world of Python IF-ELSE conditions, understand their syntax, and provide examples to demonstrate their practical implementation.

Syntax of Py IF-ELSE Condition:

The syntax of the IF-ELSE condition in Python is as follows:

if condition:
    # code block executed if the condition is True
else:
    # code block executed if the condition is False

Example 1: Checking if a Number is Even or Odd:

num = 7

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

Output:

The number is odd.

Example 2: Determining if a Student Passed or Failed an Exam:

marks = 75

if marks >= 50:
    print("Congratulations! You passed the exam.")
else:
    print("Unfortunately, you failed the exam.")

Output:

Congratulations! You passed the exam.

Nested IF-ELSE Statements:

You can also nest IF-ELSE statements within each other to handle more complex conditions. This allows for multiple levels of decision-making within your program.

Example: Determining the Grade of a Student based on Marks:

marks = 75

if marks >= 90:
    grade = "A"
elif marks >= 80:
    grade = "B"
elif marks >= 70:
    grade = "C"
else:
    grade = "D"

print("Your grade is:", grade)

Output:

Your grade is: C

Conclusion:

The IF-ELSE condition is a crucial concept in Python programming that enables you to make decisions and execute specific blocks of code based on certain conditions. In this blog post, we covered the syntax of the IF-ELSE condition, provided examples of checking for even/odd numbers and determining pass/fail status and grade based on marks. By mastering IF-ELSE conditions, you can create more dynamic and responsive programs. Remember to experiment and apply IF-ELSE conditions to suit the specific requirements of your projects. Happy coding!