Conditional statements are an essential part of programming. They allow a program to execute certain blocks of code only when specific conditions are met. This article will cover the if, if...else, and if...elif...else statements in Python, complete with examples for clarity.
1. Python if
Statement
The if
statement is used to execute a block of code only if a condition evaluates to True
.
Syntax:
if condition:
# Code to execute if the condition is True
- The
condition
is a Boolean expression (e.g.,number > 0
) that evaluates to eitherTrue
orFalse
.
Example:
number = int(input('Enter a number: '))
if number > 0:
print(f'{number} is a positive number.')
print('This statement always executes.')
Output 1:
Enter a number: 10
10 is a positive number.
This statement always executes.
Output 2:
Enter a number: -5
This statement always executes.
2. Indentation in Python
Python uses indentation to define code blocks. Failing to use proper indentation will raise an error.
Correct Example:
x = 1
total = 0
if x != 0:
total += x
print(total)
print("This statement always executes.")
Incorrect Example:
x = 1
total = 0
if x != 0:
total += x
print(total) # This will cause an indentation error
3. Python if...else
Statement
The else
clause can be used with if
to specify an alternative block of code to execute if the condition evaluates to False
.
Syntax:
if condition:
# Code block if condition is True
else:
# Code block if condition is False
Example:
number = int(input('Enter a number: '))
if number > 0:
print('Positive number')
else:
print('Not a positive number')
print('This statement always executes.')
Output 1:
Enter a number: 10
Positive number
This statement always executes.
Output 2:
Enter a number: -2
Not a positive number
This statement always executes.
4. Python if...elif...else
Statement
When there are multiple conditions to evaluate, the if...elif...else
statement is used.
Syntax:
if condition1:
# Code block 1
elif condition2:
# Code block 2
else:
# Code block 3
Example:
number = int(input('Enter a number: '))
if number > 0:
print('Positive number')
elif number < 0:
print('Negative number')
else:
print('Zero')
print('This statement always executes.')
Output:
Enter a number: -5
Negative number
This statement always executes.
- Only one block of code is executed, even if multiple conditions are
True
.
5. Python Nested if
Statements
You can nest an if
statement inside another if
statement to handle more complex scenarios.
Example:
number = int(input('Enter a number: '))
if number >= 0:
if number == 0:
print('Number is 0')
else:
print('Number is positive')
else:
print('Number is negative')
Output:
Enter a number: 5
Number is positive
6. Real-World Example: Grading System
Let’s apply these concepts to a real-world scenario: assigning grades to students based on their scores.
Example:
score = int(input('Enter your score: '))
if score > 90:
grade = 'A'
elif score > 75:
grade = 'B'
elif score > 65:
grade = 'C'
else:
grade = 'F'
print(f'Your grade is: {grade}')
Output 1:
Enter your score: 88
Your grade is: B
Output 2:
Enter your score: 64
Your grade is: F
Key Takeaways
-
if
executes code only when the condition isTrue
. -
if...else
provides an alternative when the condition isFalse
. -
if...elif...else
handles multiple conditions. - Proper indentation is crucial in Python to define code blocks.
With these examples and explanations, you should now have a clear understanding of how to use conditional statements in Python effectively!
Top comments (0)