DEV Community

Shikha Gupta
Shikha Gupta

Posted on

Practical Guide to Python Conditional Statements

Practical Guide to Python Conditional Statements

Conditional statements are fundamental in programming, allowing the flow of execution to change based on certain conditions. In Python, the primary conditional statements are if, elif, and else.

  1. The if Statement

The if statement allows you to execute a block of code only if a specified condition is true.

Syntax:

if condition:
     code to execute if condition is true
Enter fullscreen mode Exit fullscreen mode

Example:

x = 10
if x > 5:
    print("x is greater than 5")
Enter fullscreen mode Exit fullscreen mode
  1. The elif Statement

The elif (short for "else if") statement allows you to check multiple expressions for truth value and execute a block of code as soon as one of the conditions is true.

Syntax:

if condition1:
     code to execute if condition1 is true
elif condition2:
     code to execute if condition2 is true
Enter fullscreen mode Exit fullscreen mode

Example:

x = 10
if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is greater than 5 but less than or equal to 15")
Enter fullscreen mode Exit fullscreen mode
  1. The else Statement

The else statement catches anything which isn't caught by the preceding conditions.

Syntax:

if condition1:
     code to execute if condition1 is true
elif condition2:
     code to execute if condition2 is true
else:
     code to execute if none of the above conditions are true
Enter fullscreen mode Exit fullscreen mode

Example:

x = 3
if x > 15:
    print("x is greater than 15")
elif x > 5:
    print("x is greater than 5 but less than or equal to 15")
else:
    print("x is 5 or less")
Enter fullscreen mode Exit fullscreen mode
  1. Nested Conditional Statements

You can nest if, elif, and else statements within each other to check for multiple conditions.

Example:

x = 10
if x > 5:
    if x > 15:
        print("x is greater than 15")
    else:
        print("x is greater than 5 but less than or equal to 15")
else:
    print("x is 5 or less")
Enter fullscreen mode Exit fullscreen mode
  1. Conditional Expressions (Ternary Operator)

Python also provides a way to write conditional statements in a single line, known as conditional expressions or ternary operators.

Syntax:

value_if_true if condition else value_if_false
Enter fullscreen mode Exit fullscreen mode

Example:

x = 10
result = "x is greater than 5" if x > 5 else "x is 5 or less"
print(result)
Enter fullscreen mode Exit fullscreen mode
  1. Using and and or in Conditions

You can combine multiple conditions using and and or logical operators.

Example:

x = 10
y = 20

if x > 5 and y > 15:
    print("x is greater than 5 and y is greater than 15")

if x > 15 or y > 15:
    print("Either x or y is greater than 15")
Enter fullscreen mode Exit fullscreen mode

Practical Tips

  • Indentation: Proper indentation is crucial in Python, as it determines the block of code associated with each condition.
  • Readability: Always aim for readability. Nested conditions can become complex, so consider refactoring if your code becomes difficult to follow.
  • Boolean Values: Remember that conditions in if statements can be any expression that evaluates to True or False.

Summary

Python's conditional statements (if, elif, and else) allow you to execute code based on specific conditions. By mastering these constructs, you can control the flow of your programs more effectively and make decisions dynamically during runtime.

https://www.youtube.com/watch?v=RjlgfjKNl4g

Top comments (0)