In Python, the if statement is a widely used control structure that allows you to execute a block of code conditionally based on a certain condition. It enables your program to make decisions and perform different actions based on whether a specific condition is true or false.
The syntax of the if statement in Python is as follows:
if condition: # code block to execute if the condition is true
The condition can be any expression that evaluates to either True
or False
. If the condition is true, the code block indented under the if
statement will be executed.
Let’s look at some examples to understand how the if statement works:
Example 1: Checking if a Number is Positive or Negative
num = -5 if num >= 0: print("The number is positive.") else: print("The number is negative.")
In this example, we have a variable num
initialized with the value -5
. The if statement checks if num
is greater than or equal to zero. Since -5
is less than zero, the condition evaluates to False
, and the code block under the else
statement is executed. Therefore, the output will be The number is negative.
Example 2: Checking if a Number is Even or Odd
num = 10 if num % 2 == 0: print("The number is even.") else: print("The number is odd.")
In this example, we have a variable num
initialized with the value 10
. The if statement checks if num
is divisible by 2 without any remainder. Since 10
is divisible by 2, the condition evaluates to True
, and the code block under the if
statement is executed. Therefore, the output will be The number is even.
Example 3: Nested If Statements
num = 15 if num > 0: print("The number is positive.") if num % 2 == 0: print("The number is also even.") else: print("The number is odd.") else: print("The number is negative.")
In this example, we have a variable num
initialized with the value 15
. The outer if statement checks if num
is greater than zero. Since 15
is greater than zero, the condition evaluates to True
, and the code block under the outer if statement is executed. Inside this code block, there is another if statement that checks if num
is divisible by 2. Since 15
is not divisible by 2, the inner if statement’s condition evaluates to False
, and the code block under the else
statement of the inner if statement is executed. Therefore, the output will be:
The number is positive.
The number is odd.
Example 4: Using elif for Multiple Conditions
num = 7 if num > 0: print("The number is positive.") elif num == 0: print("The number is zero.") else: print("The number is negative.")
In this example, we have a variable num
initialized with the value 7
. The if statement checks if num
is greater than zero. Since 7
is greater than zero, the condition evaluates to True
, and the code block under the if
statement is executed. Therefore, the output will be The number is positive.
If the condition was False
, the if statement would move to the next condition using the elif
statement. If none of the conditions are true, the code block under the else
statement will be executed.
The if statement in Python provides a powerful way to control the flow of your program based on different conditions. By using various comparison operators and logical operators, you can create complex conditions to handle different scenarios. Remember to indent the code blocks properly to ensure they are executed within the correct scope.