Conditional Statements in Python
In Python, you can create a conditional structure using the if statement.
The Syntax of the if Statement
if (expression):
block of code
If the condition is true, the if statement executes the block of code.
If the condition is false, the program skips to the first statement after the conditional structure.
Note: In Python, the block of code is not enclosed in braces or other keywords. Instead, Python uses indentation, moving the start of the lines to the right to indicate that they are part of the conditional structure.
An Example
In the following code, I ask the user to enter a number.
The entered number is then assigned to the variable n.
n = int(input('Enter a number: '))
if (n == 5):
print('You entered 5')
print('You did not enter 5')
In the second line, the if statement checks if the variable n is equal to 5.
- If the condition (n == 5) is true, the Python interpreter executes the block of code inside the if statement. In this case, the block consists only of the first print statement. The result will be:
You entered 5
- If the condition (n == 5) is false, it means the user entered a different number. The interpreter skips to the first statement after the if block, which is the second print statement. The result will be:
You did not enter 5
The else Clause
The if statement can also include an alternative block of code to execute when the condition is not met.
This second block is placed after the else clause:
if (condition):
block of code 1
else:
block of code 2
If the condition is true, the if statement executes block 1.
If the condition is false, the if statement executes block 2.
An Example
In the following code, I ask the user to enter an integer and assign it to the variable n.
n = int(input('Enter a number: '))
if (n > 0):
print('The number is positive')
else:
print('The number is not positive')
Here, I check if the number is greater than 0.
- If the condition is true, the if statement executes the first block and prints the message "The number is positive".
The number is positive
- If the condition is false, the if statement executes the second block and prints the message "The number is not positive".
The number is not positive
Chained Conditions (ELIF)
In an if statement, you can also include multiple blocks for chained conditions.
In this case, you use the elif clause:
if (condition):
block of code 1
elif (condition2):
block of code 2
elif (condition3):
block of code 3
Elif stands for "else if".
There is no limit to the number of elif clauses you can use.
An Example
In the following code, I ask the user to enter a number and assign it to the variable n.
n = int(input('Enter a number: '))
if (n > 0):
print('The number is positive')
elif (n < 0):
print('The number is negative')
else:
print('The number is zero')
The program executes as follows:
- If the first condition is true (n > 0), the program prints "The number is positive" and skips to the end of the conditional structure.
The number is positive
- If the first condition is false, the elif statement checks if n is less than zero (n < 0). If true, the program prints "The number is negative" and skips to the end of the conditional structure.
The number is negative
- If the elif is also false, the program executes the block after the else clause, printing "The number is zero".
The number is zero
Nested Conditions
In Python, you can also nest multiple if statements.
if (condition):
if (condition2):
if (condition3):
There is no limit to the number of nested if statements.
An Example
In the following code, I ask the user to enter a number.
Then I check if the number is positive, negative, or zero.
n = int(input('Enter a number: '))
if (n > 0):
print('The number is positive')
else:
if (n == 0):
print('The number is zero')
else:
print('The number is negative')
Inside the else clause, there is a nested if statement.
- If the condition (n > 0) is true, the program displays the message "The number is positive".
- If the condition (n > 0) is false, the program executes the else block, where there is a nested if statement that checks if the number is zero (n == 0). If it is not zero, then the number must be negative.