How to write if else statement in Python?

Writing If-Else Statements in Python: A Comprehensive Guide

Introduction

In Python, if-else statements are a fundamental concept used to control the flow of a program based on certain conditions. They are used to make decisions, perform different actions, or execute specific blocks of code based on the outcome of a condition. In this article, we will explore how to write if-else statements in Python, including the syntax, examples, and best practices.

Basic Syntax

The basic syntax of an if-else statement in Python is as follows:

if condition:
# code to execute if condition is true
else:
# code to execute if condition is false

Example 1: Simple If-Else Statement

Let’s consider a simple example where we want to print "Hello" if the user’s age is greater than 18 and "Goodbye" if the user’s age is less than or equal to 18.

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

if age > 18:
print("Hello")
else:
print("Goodbye")

Example 2: If-Else Statement with Multiple Conditions

In this example, we want to print "Hello" if the user’s age is greater than 18, 20, or 25, and "Goodbye" if the user’s age is less than or equal to 18, 20, or 25.

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

if age > 18 and age > 20:
print("Hello")
elif age > 18 and age <= 20:
print("Goodbye")
elif age > 18 and age < 20:
print("You are a teenager")
elif age <= 18:
print("Goodbye")

Example 3: If-Else Statement with Multiple Conditions and Loops

In this example, we want to print "Hello" if the user’s age is greater than 18, 20, or 25, and "Goodbye" if the user’s age is less than or equal to 18, 20, or 25. We also want to print a message if the user’s age is between 19 and 25.

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

if age > 18 and age > 20:
print("Hello")
elif age > 18 and age <= 20:
print("Goodbye")
elif age > 18 and age < 20:
print("You are a teenager")
elif age <= 18:
print("Goodbye")
elif age <= 19 and age <= 25:
print("You are a young adult")

Best Practices

Here are some best practices to keep in mind when writing if-else statements in Python:

  • Use clear and descriptive variable names.
  • Use comments to explain the purpose of the code.
  • Use whitespace to make the code readable.
  • Avoid using if-else statements for complex logic; instead, use loops and conditional statements.
  • Use the if __name__ == "__main__": guard to ensure that the code is only executed when the script is run directly, not when it is imported as a module.

Table: Common If-Else Statements in Python

Condition Code
Age > 18 print("Hello")
Age > 20 and Age <= 25 print("Goodbye")
Age > 18 and Age < 20 print("You are a teenager")
Age <= 18 print("Goodbye")
Age <= 19 and Age <= 25 print("You are a young adult")

Conclusion

In conclusion, if-else statements are a fundamental concept in Python that allow you to control the flow of a program based on certain conditions. By following the best practices outlined in this article, you can write efficient and effective if-else statements in Python. Remember to use clear and descriptive variable names, use comments to explain the purpose of the code, and avoid using if-else statements for complex logic. With practice and experience, you will become proficient in writing if-else statements in Python.

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

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

Scroll to Top