Aug
31
2023

Conditional Statements in Python

interview-questions-jinaldesai.com
  1. Getting Started with Python
  2. Variables and Data Types in Python
  3. Basic Input and Output in Python
  4. Conditional Statements in Python
  5. Loops in Python
  6. Lists and Tuples in Python
  7. Dictionaries and Sets in Python
  8. Functions and Modules in Python
  9. Exception Handling in Python
  10. File Handling in Python
  11. Working with Dates and Times in Python
  12. List Comprehensions in Python

Welcome to the fourth installment of our Python programming series for beginners! In this article, we’ll dive into the fascinating realm of conditional statements in Python. Conditional statements enable your programs to make decisions and take different actions based on specific conditions. We’ll explore the `if`, `elif`, and `else` statements, as well as touch upon the concept of switching.

Introduction to Conditional Statements

Making decisions is a core aspect of programming. Conditional statements allow you to control the flow of your Python program by executing specific code blocks when certain conditions are met. These conditions are evaluated as either `True` or `False`. In Python, we primarily use the `if`, `elif`, and `else` statements for decision-making.

The `if` Statement

The `if` statement is used to execute a block of code only if a specified condition is `True`. Here’s a simple example:


age = 20
if age >= 18:
    print("You are an adult.")

In this code, we check if the variable `age` is greater than or equal to 18. If the condition is satisfied (age is 18 or older), the indented block of code under the `if` statement is executed, and “You are an adult.” is printed.

The `elif` Statement

The `elif` statement, short for “else if,” allows you to check additional conditions if the initial `if` condition is not met. Here’s an example:


grade = 75
if grade >= 90:
    print("A")
elif grade >= 80:
    print("B")
elif grade >= 70:
    print("C")
else:
    print("F")

In this code, we determine a letter grade based on the value of the `grade` variable. The `elif` statements enable us to examine multiple conditions sequentially until one is `True`. If none of the conditions hold, the code inside the `else` block is executed.

The `else` Statement

The `else` statement is used to specify code that should be executed if none of the preceding conditions in an `if-elif-else` block are `True`. Here’s an example:


age = 15
if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")

In this code, if the `if` condition (age >= 18) is not satisfied, the code within the `else` block is executed, and “You are a minor.” is printed.

Switching in Python

Python does not have a built-in `switch` statement like some other programming languages. However, you can achieve similar functionality using dictionaries. Here’s a simple example:


def get_day_of_week(day_number):
    days = {
        1: "Monday",
        2: "Tuesday",
        3: "Wednesday",
        4: "Thursday",
        5: "Friday",
        6: "Saturday",
        7: "Sunday"
    }
    return days.get(day_number, "Invalid day")

day_number = 3
day_name = get_day_of_week(day_number)
print(f"Day {day_number} is {day_name}.")

In this code, we define a function `get_day_of_week()` that maps day numbers to their corresponding names using a dictionary. The `get()` method is used to retrieve the day name based on the given day number. If the day number is not found in the dictionary, it returns “Invalid day.”

Conclusion

In this article, we’ve delved into the essential concepts of conditional statements in Python. By mastering the `if`, `elif`, and `else` statements, you can create programs that make decisions and take different actions based on specific conditions.

As you continue your Python journey, you’ll find that conditional statements are vital for building dynamic and responsive programs. They empower you to automate tasks, validate user inputs, and handle various scenarios gracefully.

In the next article of our series, we’ll explore loops, another critical control structure in Python. Loops allow you to repeat tasks and iterate over data, making your programs more efficient and versatile. Stay curious, keep practicing, and happy coding!