Conditional statements are an essential part of programming that allow you to control the flow of your code based on certain conditions. In Python, you can achieve this using if
, elif
, and else
statements. These statements help your program make decisions and execute different blocks of code based on whether specific conditions are true or false.
Table of Contents
- The
if
Statement - The
if-else
Statement - The
if-elif-else
Statement - Nested Conditional Statements
- Ternary Operator 6 Combining Conditions
- Using Logical Operators
1. The if
Statement
The if
statement is used to execute a block of code only if a certain condition is true.
Syntax:
if condition:
# Code to be executed if the condition is true
Here's a simple example that checks if a number is positive:
num = 10
if num > 0:
print("The number is positive")
2. The if
-else
Statement
The if
-else
statement allows you to execute one block of code when a condition is true, and another block when the condition is false.
Syntax:
if condition:
# Code to be executed if the condition is true
else:
# Code to be executed if the condition is false
Let's see an example that checks whether a number is even or odd:
num = 7
if num % 2 == 0:
print("The number is even")
else:
print("The number is odd")
3. The if
-elif
-else
Statement
The if
-elif
-else
statement is used when you have multiple conditions to check. elif
stands for "else if" and allows you to test multiple conditions in sequence.
Syntax:
if condition1:
# Code to be executed if condition1 is true
elif condition2:
# Code to be executed if condition2 is true
else:
# Code to be executed if none of the conditions are true
Here's an example that categorizes a student's score into different grades:
score = 85
if score >= 90:
print("A")
elif score >= 80:
print("B")
elif score >= 70:
print("C")
else:
print("F")
4. Nested Conditional Statements
You can also nest conditional statements within each other to handle more complex scenarios. Just be mindful of proper indentation to maintain code readability. Here's a basic example:
x = 10
y = 5
if x > 0:
if y > 0:
print("Both x and y are positive")
else:
print("Only x is positive")
else:
print("Neither x nor y is positive")
5. Ternary Operator
The ternary operator is a concise way to write simple if-else statements in a single line.
The ternary operator (expression if condition else expression
) is a concise way to write conditional expressions.
is_sunny = True
weather = "It's sunny!" if is_sunny else "It's cloudy."
print(weather)
6. Combining Conditions
You can combine conditions using logical operators and
, or
, and not
.
temperature = 25
is_raining = False
if temperature > 20 and not is_raining:
print("Perfect weather for a picnic!")
7. Using Logical Operators
Logical operators help you build complex conditions by combining simpler ones.
x = 10
y = 5
if x > 0 and y > 0:
print("Both x and y are positive.")
elif x > 0 or y > 0:
print("At least one of x or y is positive.")
else:
print("Neither x nor y is positive.")
Top comments (0)