Conditional Statements in Python
Last Updated :
19 Mar, 2024
Understanding and mastering Python’s conditional statements is fundamental for any programmer aspiring to write efficient and robust code. In this guide, we’ll delve into the intricacies of conditional statements in Python, covering the basics, advanced techniques, and best practices.
What are Conditional Statements?
Conditional Statements are statements in Python that provide a choice for the control flow based on a condition. It means that the control flow of the Python program will be decided based on the outcome of the condition.
Now let us see how Conditional Statements are implemented in Python.
Types of Conditional Statements in Python
1. If Conditional Statement in Python
If the simple code of block is to be performed if the condition holds then the if statement is used. Here the condition mentioned holds then the code of the block runs otherwise not.
Syntax of If Statement:
if condition:
# Statements to execute if
# condition is true
Python
if 10 > 5 :
print ( "10 greater than 5" )
print ( "Program ended" )
|
Output
10 greater than 5
Program ended
2. If else Conditional Statements in Python
In a conditional if Statement the additional block of code is merged as an else statement which is performed when if condition is false.
Syntax of Python If-Else:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Python
x = 3
if x = = 4 :
print ( "Yes" )
else :
print ( "No" )
|
3. Nested if..else Conditional Statements in Python
Nested if..else means an if-else statement inside another if statement. Or in simple words first, there is an outer if statement, and inside it another if – else statement is present and such type of statement is known as nested if statement. We can use one if or else if statement inside another if or else if statements.
Python
letter = "A"
if letter = = "B" :
print ( "letter is B" )
else :
if letter = = "C" :
print ( "letter is C" )
else :
if letter = = "A" :
print ( "letter is A" )
else :
print ( "letter isn't A, B and C" )
|
4. If-elif-else Conditional Statements in Python
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final “else” statement will be executed.
Python
letter = "A"
if letter = = "B" :
print ( "letter is B" )
elif letter = = "C" :
print ( "letter is C" )
elif letter = = "A" :
print ( "letter is A" )
else :
print ( "letter isn't A, B or C" )
|
The Python ternary Expression determines if a condition is true or false and then returns the appropriate value in accordance with the result. The ternary Expression is useful in cases where we need to assign a value to a variable based on a simple condition, and we want to keep our code more concise — all in just one line of code.
Syntax of Ternary Expression
Syntax: [on_true] if [expression] else [on_false]
expression: conditional_expression | lambda_expr
Python
a, b = 10 , 20
print ( "Both a and b are equal" if a = = b else "a is greater than b"
if a > b else "b is greater than a" )
|
Output
b is greater than a
Best Practices for Using Conditional Statements
- Keep conditions simple and expressive for better readability.
- Avoid deeply nested conditional blocks; refactor complex logic into smaller, more manageable functions.
- Comment on complex conditions to clarify their purpose.
- Prefer the ternary operator for simple conditional assignments.
- Advanced Techniques:
- Using short-circuit evaluation for efficiency in complex conditions.
- Leveraging the any() and all() functions with conditions applied to iterables.
- Employing conditional expressions within list comprehensions and generator expressions.
Please Login to comment...