Open In App

Conditional Statements in Python

Last Updated : 19 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 statement example
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




# if..else statement example
x = 3
if x == 4:
    print("Yes")
else:
    print("No")


Output

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




# if..else chain statement
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")


Output

letter is A


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




# if-elif statement example
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")


Output

letter is A


5. Ternary Expression Conditional Statements in Python

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




# Python program to demonstrate nested ternary operator
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

  1. Keep conditions simple and expressive for better readability.
  2. Avoid deeply nested conditional blocks; refactor complex logic into smaller, more manageable functions.
  3. Comment on complex conditions to clarify their purpose.
  4. Prefer the ternary operator for simple conditional assignments.
  5. Advanced Techniques:
  6. Using short-circuit evaluation for efficiency in complex conditions.
  7. Leveraging the any() and all() functions with conditions applied to iterables.
  8. Employing conditional expressions within list comprehensions and generator expressions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads