The If… Else Statement in Python
In Python, the if... else statement is a fundamental construct that allows programmers to create conditional logic within their code. With this statement, you can make your program choose between different paths based on specific conditions.
This module aims to provide a detailed understanding of the if... else statement in Python, with help from some code examples.
Understanding the if... else Statement
The if... else statement in Python is used to control the flow of execution based on a condition.
It allows the program to make decisions and execute different blocks of code based on whether the condition evaluates to True or False.
The general syntax of the if... else statement is as follows:
>>> if condition:
>>> # code block executed if condition is True
>>> else:
>>> # code block executed if condition is False
The condition can be any expression that evaluates to either True or False. If the condition is True, the code block indented under the if statement is executed. If the condition is False, the code block indented under the else statement is executed.
Let's explore some code examples to better understand the if... else statement in Python:
Example 1: Checking if a number is even or odd
>>> num = int(input("Enter a number: "))
>>> if num % 2 == 0:
>>> print("The number is even.")
>>> else:
>>> print("The number is odd.")
In this example, the program prompts the user to enter a number. It then uses the if... else statement to check if the number is divisible by 2. If the condition num % 2 == 0 is True, the program prints "The number is even." Otherwise, it prints "The number is odd."
Example 2: Determining the grade based on a score
>>> score = int(input("Enter your score: "))
>>> if score >= 90:
>>> grade = 'A'
>>> elif score >= 80:
>>> grade = 'B'
>>> elif score >= 70:
>>> grade = 'C'
>>> elif score >= 60:
>>> grade = 'D'
>>> else:
>>> grade = 'F'
>>> print("Your grade is:", grade)
In this example, the program asks the user to enter their score. It uses multiple if... elif... else statements to assign a grade based on the score.
The first condition that evaluates to True is executed, and the corresponding grade is assigned. Finally, the program displays the grade.
Example 3: Checking if a year is a leap year
>>> year = int(input("Enter a year: "))
>>> if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
>>> print("The year is a leap year.")
>>> else:
>>> print("The year is not a leap year.")
In this example, the program prompts the user to enter a year. It uses the if... else statement with a compound condition to determine if the year is a leap year.
If the condition evaluates to True, it prints "The year is a leap year." Otherwise, it prints "The year is not a leap year."
Nested if... else Statements
Python allows the nesting of if... else statements within each other, enabling more complex conditional logic. Here's an example:
>>> num = int(input("Enter a number: "))
>>> if num > 0:
>>> print("The number is positive.")
>>> else:
>>> if num < 0:
>>> print("The number is negative.")
>>> else:
>>> print("The number is zero.")
In this example, the program checks if the number is positive, negative, or zero using nested if... else statements.
Module Takeaways…
The if... else statement is a powerful tool in Python that allows programmers to introduce conditional logic into their code. It enables decision-making based on conditions and helps in creating dynamic programs.
By using if... else statements, you can control the flow of your program and make it more responsive to different scenarios.
Hopefully, this article has provided you with a comprehensive understanding of the if... else statement in Python, along with code examples and explanations. Now, go ahead and apply this knowledge to enhance your programming skills.
NEXT STOP: The all too important While loop in Python! Keep learning with us to achieve your Python dreams…