Search This Blog

Conditional Statements in Python

 

Conditional Statements in Python

Conditional statements are used to execute certain blocks of code based on whether a condition is True or False. Python provides several ways to control the flow of your program using conditional statements, the most common of which are if, elif, and else.


1. The if Statement

The if statement is used to execute a block of code if a specified condition is True.

Syntax:

if condition:
    # Code to execute if condition is True

Example:

age = 18

if age >= 18:
    print("You are an adult.")

In this example, since the age is 18, which is greater than or equal to 18, the output will be You are an adult.


2. The else Statement

The else statement is used when the condition in the if statement is False. It runs the block of code that follows it.

Syntax:

if condition:
    # Code to execute if condition is True
else:
    # Code to execute if condition is False

Example:

age = 16

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

In this example, since age is 16, the condition age >= 18 is False, so the program will print You are a minor.


3. The elif Statement

The elif (short for "else if") statement allows you to check multiple conditions. It comes after an if statement and before an else block. It’s useful when you need to check for multiple conditions.

Syntax:

if condition1:
    # Code to execute if condition1 is True
elif condition2:
    # Code to execute if condition2 is True
else:
    # Code to execute if none of the conditions are True

Example:

age = 25

if age < 18:
    print("You are a minor.")
elif age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

Here, since age is 25, which is less than 65, the output will be You are an adult.


4. Nested Conditional Statements

Conditional statements can be nested, meaning you can place one if statement inside another if statement. This is useful when you need to check more specific conditions after an initial check.

Example:

age = 20
has_permission = True

if age >= 18:
    if has_permission:
        print("You can enter the club.")
    else:
        print("You need permission to enter the club.")
else:
    print("You are too young to enter the club.")

In this example, the program first checks if the age is greater than or equal to 18. If true, it then checks whether the user has permission. Based on these checks, it displays the appropriate message.


5. The and & or Logical Operators in Conditions

You can combine multiple conditions using the logical operators and, or, and not.

  • and: Returns True if both conditions are True.
  • or: Returns True if at least one condition is True.
  • not: Reverses the boolean value of the condition.

Example using and:

age = 20
has_id = True

if age >= 18 and has_id:
    print("You can enter the club.")
else:
    print("You cannot enter the club.")

This checks if both conditions (age being 18 or older, and having an ID) are True. If both are True, the user can enter the club.

Example using or:

age = 16
has_permission = False

if age >= 18 or has_permission:
    print("You can enter the club.")
else:
    print("You cannot enter the club.")

In this case, even though age is less than 18, the condition has_permission is False, so the program will print "You cannot enter the club."


6. The in Operator in Conditions

You can use the in operator to check if a value exists in a collection, such as a list, string, or tuple.

Example:

fruit = "apple"

if fruit in ["banana", "apple", "orange"]:
    print("This fruit is available.")
else:
    print("This fruit is not available.")

Here, the program checks if the value of fruit is in the list of available fruits.


7. The not Operator

The not operator reverses the boolean value of a condition.

Example:

is_raining = False

if not is_raining:
    print("You can go for a walk.")
else:
    print("It's better to stay indoors.")

In this example, the not operator is used to reverse the is_raining value. Since is_raining is False, the condition becomes True, and the program prints "You can go for a walk."


Summary

  • if: Checks a condition and executes a block of code if the condition is True.
  • else: Executes a block of code if the condition in the if statement is False.
  • elif: Used to check multiple conditions.
  • Logical Operators (and, or, not): Combine or reverse conditions for more complex checks.
  • in: Checks if a value is present in a collection.

Conditional statements are essential in making decisions in your programs and guiding the flow of execution based on different criteria. Understanding how to use if, elif, else, and logical operators will allow you to write dynamic, flexible Python code.

Popular Posts