Python | Conditional Statements

Python | Conditional Statements

In Python, conditional statements are used to control the flow of a program based on certain conditions. They allow you to check for certain conditions, and then execute code depending on whether or not those conditions are met. The most commonly used conditional statements in Python are if, elif, and else.

if statement: The if statement is used to check a condition, and if the condition is True, the code block under the if statement is executed. Here’s an example:

x = 5
if x > 0:
    print("x is positive")

Output:

 x is positive 

In this example, the if statement checks whether the value of x is greater than 0. Since it is, the code block under the if statement is executed, and the message “x is positive” is printed.

elif statement: The elif statement is short for “else if”, and is used to check multiple conditions. The code block under the elif statement is executed if the preceding if and elif conditions are False and this condition is True. Here’s an example:

x = 5
if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")

Output:

 x is positive 

In this example, the if statement checks whether the value of x is greater than 0. Since it is, the code block under the if statement is executed and the message “x is positive” is printed. But since the if statement is true, the elif statement is not executed.

else statement: The else statement is used to specify a code block to be executed if none of the preceding if and elif conditions are True. Here’s an example:

x = 0
if x > 0:
    print("x is positive")
elif x < 0:
    print("x is negative")
else:
    print("x is zero")

Output:

 x is zero 

In this example, since the x is equal to 0, none of the conditions in the if and elif statements are met, so the code block under the else statement is executed, and the message “x is zero” is printed.

It is important to note that Python uses indentation to indicate the code block, so the code block under an if, elif or else statement must be indented.

In addition to these basic conditional statements, Python also has the pass statement, which can be used as a placeholder when a code block is required but no action needs to be taken.

if x < 0:
    pass

This will do nothing if the x is less than zero.

you can also have nested conditionals which means that a conditional statement can contain another conditional statement. Here’s an example:

x = 5
y = 10

if x > 0:
    if y > 0:
        print("Both x and y are positive")
    else:
        print("x is positive, y is non-positive")
else:
    if y > 0:
        print("x is non-positive, y is positive")
    else:
        print("Both x and y are non-positive")

Output:

 Both x and y are positive 

In this example, the outer if statement checks whether x is greater than 0, and the inner if statement checks whether y is greater than 0. If both conditions are true, “Both x and y are positive” will be printed. If only the outer condition is true and inner is false, “x is positive, y is non-positive” will be printed and so on.

In summary, conditional statements in Python are used to control the flow of a program by checking conditions and executing code based on the results of those conditions. The if statement is used to check a single condition, the elif statement is used to check multiple conditions, and the else statement is used to specify a code block to be executed when none of the other conditions are met.

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds