Introduction to Conditional Logic
At the heart of any programming language lies the ability to make decisions. This is achieved through conditional logic, allowing a program to execute different blocks of code based on whether certain conditions are true or false. In Python, the if statement is the primary tool for implementing conditional logic.
The if Statement: Basic Syntax
The simplest form of an if statement checks a single condition. If that condition evaluates to True, the code block indented below the if statement is executed.
x = 10
if x > 5:
print("x is greater than 5")
In this example, the condition x > 5 is evaluated. Since x is 10, the condition is True, and the message "x is greater than 5" is printed. If x were less than or equal to 5, the code within the if block would be skipped.
else Clause: Providing an Alternative
The else clause allows you to specify a block of code to be executed if the if condition is False.
x = 3
if x > 5:
print("x is greater than 5")
else:
print("x is not greater than 5")
In this case, because x is 3, the if condition is False. Consequently, the code within the else block is executed, printing "x is not greater than 5".
elif Clause: Handling Multiple Conditions
When you need to check multiple conditions in sequence, the elif (short for "else if") clause comes in handy. It allows you to add additional conditions to be checked if the previous if or elif conditions are False.
x = 8
if x > 10:
print("x is greater than 10")
elif x > 5:
print("x is greater than 5 but not greater than 10")
else:
print("x is not greater than 5")
In this example, the first if condition (x > 10) is False. The program then checks the elif condition (x > 5). Since x is 8, this condition is True, and the corresponding message is printed. The else block is skipped because a condition has already been met.
Combining Conditions with Logical Operators
You can create more complex conditions by combining multiple expressions using logical operators:
and: The condition isTrueonly if both expressions areTrue.or: The condition isTrueif at least one of the expressions isTrue.not: Negates the expression;not TrueisFalse, andnot FalseisTrue.
Here’s an example:
age = 25
is_student = True
if age >= 18 and is_student:
print("Eligible for student discount")
if age < 13 or age > 65:
print("Not eligible for standard pricing")
Simplifying Conditions: De Morgan’s Laws
Sometimes, complex conditions can be simplified using De Morgan’s Laws. These laws provide helpful equivalencies:
not (a and b)is equivalent to(not a) or (not b)not (a or b)is equivalent to(not a) and (not b)
These can make your code more readable. For instance, instead of if not (x == 'a' or x == 'b'):, you can write if not x == 'a' and not x == 'b':.
Checking Numeric Ranges
A common use case for if statements is checking if a number falls within a certain range.
key = 20
if key < 1 or key > 34:
print("Key is outside the valid range")
else:
print("Key is within the valid range")
A more concise way to express this is using chained comparisons:
key = 20
if not (1 <= key <= 34):
print("Key is outside the valid range")
else:
print("Key is within the valid range")
This is equivalent to if key < 1 or key > 34:, but it’s often more readable. Remember that Python allows chaining comparisons like this, making range checks clean and intuitive.