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!