Skip to main content

Introduction to Python If ... Else

Python If ... Else

The if-else statement allows you to conditionally execute a block of code based on a given condition.

The if block is executed if the condition is true, and the else block is executed if the condition is false.

Here's the basic syntax of the if-else statement:

if condition:
# code block to be executed if the condition is true
else:
# code block to be executed if the condition is false

Here's an example that demonstrates the if-else statement:

age = 20

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

In this example:

  • If the age is greater than or equal to 18, the code inside the if block is executed, and it prints "You are an adult." If the age is less than 18, the code inside the else block is executed, and it prints "You are a minor."

You can also include multiple conditions using the elif statement, which stands for "else if".

The elif statement allows you to check for additional conditions after the initial if statement.

As an example:

score = 75

if score >= 90:
print("You got an A.")
elif score >= 80:
print("You got a B.")
elif score >= 70:
print("You got a C.")
elif score >= 60:
print("You got a D.")
else:
print("You failed.")

In this example:

  • The code checks the value of the score variable and prints the corresponding grade based on the score.
  • The conditions are checked in order, and the first condition that is true is executed.
  • If none of the conditions are true, the code inside the else block is executed.

Nested if-else Statements

You can nest if-else statements within each other to handle more complex conditions. This allows you to check for multiple conditions and execute different code blocks accordingly.

As an example:

x = 10
y = 5

if x > y:
print("x is greater than y.")
else:
if x < y:
print("x is less than y.")
else:
print("x is equal to y.")

Ternary Operator

Python supports a shorthand syntax called the ternary operator, which allows you to write a simple if-else statement in a single line.

It has the following syntax:

value_if_true if condition else value_if_false

As an example:

x = 10
y = 5

result = "x is greater than y." if x > y else "x is less than or equal to y."
print(result)

Chained Comparison Operators

You can use chained comparison operators to simplify complex conditions. Instead of using multiple if statements, you can chain the comparison operators together.

As an example:

x = 10

if 0 < x < 100:
print("x is between 0 and 100.")

Truthy and Falsy Values

In Python, certain values are considered "truthy" or "falsy" when used in conditions.

Truthy values evaluate to True, while falsy values evaluate to False.

It can be useful in if-else statements. Some examples of falsy values include False, None, 0, "" (empty string), and empty data structures such as empty lists, sets, dictionaries, and tuples.

value = 0

if value:
print("Value is truthy.")
else:
print("Value is falsy.")

Short-Circuit Evaluation

Python uses short-circuit evaluation for logical operators (and and or).

If the result of the expression can be determined by evaluating only the first operand, the second operand is not evaluated.

This can be useful in conditions that involve costly operations or function calls.

x = 5
y = 10

if x > 0 and y / x > 2:
print("Condition is true.")

In this example:

  • The y / x division is not evaluated if x is less than or equal to 0, preventing a potential division by zero error.