Python – Decision Making

Introduction to Decision Making in Python

In programming, decision making is a fundamental concept that allows the execution of different code blocks based on certain conditions. Python provides several constructs to implement decision-making logic, such as if-else statements, nested if-else statements, and the switch case statement.

if-else Statements

The if-else statement is the most basic decision-making construct in Python. It allows the program to execute a specific block of code if a certain condition is true, and a different block of code if the condition is false.

Here’s an example:

age = 18
if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote.")

In this example, if the variable “age” is greater than or equal to 18, the program will print “You are eligible to vote.” Otherwise, it will print “You are not eligible to vote.”

Nested if-else Statements

Nested if-else statements allow for more complex decision-making scenarios. In this construct, an if-else statement is nested inside another if-else statement. This allows for multiple conditions to be checked and different code blocks to be executed accordingly.

Here’s an example:

age = 25
if age >= 18:
    if age <= 25:
        print("You are eligible for a youth discount.")
    else:
        print("You are not eligible for a youth discount.")
else:
    print("You are not eligible for a discount.")

In this example, if the variable “age” is greater than or equal to 18, the program will check if it is also less than or equal to 25. If both conditions are true, it will print “You are eligible for a youth discount.” Otherwise, it will print “You are not eligible for a youth discount.” If the initial condition is false, it will print “You are not eligible for a discount.”

Switch Case Statement

Unlike some other programming languages, Python does not have a built-in switch case statement. However, you can achieve similar functionality using if-elif-else statements.

Here’s an example:

day = "Tuesday"
if day == "Monday":
    print("It's the start of the week.")
elif day == "Tuesday":
    print("It's the second day of the week.")
elif day == "Wednesday":
    print("It's the middle of the week.")
else:
    print("It's the end of the week.")

In this example, the program checks the value of the variable “day” and executes the corresponding code block based on the condition. If none of the conditions are met, the else block is executed.

Conclusion

Decision making is an essential aspect of programming, and Python provides various constructs to implement it. The if-else statement allows for simple decision-making scenarios, while nested if-else statements enable more complex conditions. Although Python does not have a built-in switch case statement, if-elif-else statements can be used to achieve similar functionality.

By understanding and utilizing these decision-making constructs, you can create more dynamic and efficient programs in Python.

Scroll to Top