IF Statement in Python

 


The if statement in Python is used to test a condition and execute a block of code if the condition is True. It allows you to control the flow of the program based on certain conditions.

Basic Syntax:

  • if condition:

  •     # Code block to execute if the condition is True


  • condition: The expression that you want to test. It can be any expression that returns a boolean value (True or False).

  • The code block under the if statement will only be executed if the condition evaluates to True.

Example 1: Basic if Statement

  • x = 10

  • if x > 5:

  •     print("x is greater than 5")


Output: x is greater than 5

In this example, the condition x > 5 is True, so the code inside the if block is executed.

else Statement

The else statement is used to define a block of code that will execute if the if condition is False.

Syntax:

  • if condition:

  •     # Code block to execute if condition is True

  • else:

  •     # Code block to execute if condition is False


Example 2: Using if and else

  • x = 3

  • if x > 5:

  •     print("x is greater than 5")

  • else:

  •     print("x is not greater than 5")


Output: x is not greater than 5

Since x is not greater than 5, the code inside the else block is executed.

elif Statement (else if)

The elif (short for "else if") statement allows you to check multiple conditions. If the if condition is False, it checks the condition in elif.

Syntax:

  • if condition1:

  •     # Code block to execute if condition1 is True

  • elif condition2:

  •     # Code block to execute if condition1 is False and condition2 is True

  • else:

  •     # Code block to execute if all conditions are False


Example 3: Using if, elif, and else

  • x = 7

  • if x > 10:

  •     print("x is greater than 10")

  • elif x > 5:

  •     print("x is greater than 5 but less than or equal to 10")

  • else:

  •     print("x is less than or equal to 5")


Output: x is greater than 5 but less than or equal to 10

In this case, x is 7, so the elif condition x > 5 is True, and the code block under elif is executed.

Nested if Statements

You can also use if statements inside other if statements. These are called nested if statements.

Syntax:

  • if condition1:

  •     if condition2:

  •         # Code block to execute if both conditions are True


Example 4: Nested if Statement

  • x = 10

  • y = 5


  • if x > 5:

  •     if y < 10:

  •         print("x is greater than 5 and y is less than 10")


Output: x is greater than 5 and y is less than 10

In this case, both x > 5 and y < 10 are True, so the nested if block is executed.

Multiple Conditions in an if Statement

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

Example 5: Multiple Conditions

  • x = 10

  • y = 20


  • if x > 5 and y > 15:

  •     print("Both conditions are True")


Output: Both conditions are True

Here, both conditions x > 5 and y > 15 are True, so the code inside the if block is executed.

Key Points:

  • if is used to test a condition and execute code if the condition is True.

  • else provides an alternative block to execute when the if condition is False.

  • elif allows you to test multiple conditions in sequence.

  • Logical operators (and, or, not) can be used to combine multiple conditions in an if statement.

Let me know if you need further examples or explanations! 😊


Post a Comment

0 Comments