VMware ESXi and vSphere Cluster Management

Python if Statements: Conditional Decisions

Learn how Python if statements test Boolean conditions, use == for equality, and run indented code blocks only when conditions are True.

An if statement lets a Python program make a decision. It tests a condition and runs an indented code block only when that condition evaluates to True.

If the condition evaluates to False, Python skips the indented block. An if statement by itself does not provide an alternate action; it simply does nothing inside the block when the condition is false.

Basic if Statement Syntax

The basic structure is:

if condition:
    statement

This structure has four important parts:

Part: if keyword
Role: Begins the conditional statement.
Example: if

Part: condition
Role: An expression that produces True or False.
Example: score == 100

Part: colon
Role: Marks the start of the controlled code block.
Example: :

Part: indented body
Role: One or more statements that run when the condition is true.
Example: print("Perfect score!")

For example:

temperature = 25

if temperature == 25:
    print("The temperature is 25 degrees.")

Python evaluates the condition temperature == 25. Because it is true, the indented print() statement runs.

Boolean Conditions

A Boolean is a truth value: either True or False. A condition is an expression evaluated to one of these two values. Comparison operators commonly create Boolean conditions.

age = 18
is_adult = age >= 18

print(is_adult)  # True

When a condition is True, the body of the if statement executes. When it is False, Python skips the body.

Condition Result: True
Does the if Block Run? Yes.
Observed Outcome: Statements in the indented body execute.

Condition Result: False
Does the if Block Run? No.
Observed Outcome: Statements in the indented body are skipped.

Equality: == Versus Assignment: =

Use the equality operator == to test whether two values are equal. Use the assignment operator = to store a value in a variable.

Operator: ==
Purpose: Compares two values.
Typical Use: if number == 5:
Result: A Boolean value, True or False.

Operator: =
Purpose: Assigns or stores a value.
Typical Use: number = 5
Result: The variable refers to the assigned value.

Numeric Equality Example

This condition is true because the variable contains the value being tested:

number = 5

if number == 5:
    print("Hello!")

Output:

Hello!

Here, number = 5 assigns the value 5. Later, number == 5 compares the stored value with 5 and produces True.

When a Condition Is False

In this example, number == 6 is false because number contains 5:

number = 5

if number == 6:
    print("This message does not appear.")

No output appears. Python evaluates the condition as False and skips the entire indented body.

Indentation Defines the Code Block

Indentation is the leading whitespace at the beginning of a line. In Python, indentation defines a code block: one or more statements grouped under a control statement.

Statements with the same indentation belong to the same if block:

points = 10

if points == 10:
    print("You earned ten points.")
    print("The bonus is available.")

Both print() statements are controlled by the if statement because both are indented consistently by four spaces.

A statement that is not indented belongs outside the block:

points = 5

if points == 10:
    print("You earned ten points.")

print("This line runs regardless of the condition.")

The first message is skipped because the condition is false, but the final message runs because it is not part of the indented block.

Comparing Strings

You can use == with strings as well as numbers. String equality requires an exact match, including capitalization and spaces.

name = "Mina"

if name == "Mina":
    print("Hello, Mina!")

This prints the greeting because both strings match exactly. The following condition is false because the capitalization differs:

name = "Mina"

if name == "mina":
    print("Hello!")

No output appears. "Mina" and "mina" are different strings in a case-sensitive equality comparison.

Common Problems and Fixes

Using = Instead of ==

Problem: You use assignment where a comparison is required.

# Correct
if number == 5:
    print("The number is five.")

Use = when assigning a value, such as number = 5. Use == when asking whether two values are equal.

Forgetting the Colon

Problem: The condition does not end with a colon.

# Correct
if number == 5:
    print("The number is five.")

Python requires : immediately after the condition to begin the body.

Incorrect or Inconsistent Indentation

Problem: A statement that should be conditional is not indented, or statements in one block use inconsistent indentation.

# Correct
if ready:
    print("Starting.")
    print("Loading data.")

Indent every statement belonging to the body consistently. Missing or inconsistent indentation can cause a syntax or indentation error. It can also change program behavior by placing a statement outside the block.

No Output Appears

If no output appears, the condition may have evaluated to False. Check the value stored in the variable, the value used for comparison, and whether you used the intended operator.

number = 5
print(number == 5)  # Check the condition directly

A String Comparison Fails

Inspect both strings for capitalization, spelling, and extra spaces. For example, "Mina", "mina", and "Mina " are not equal.

Key Points

  • An if statement makes a decision by testing a condition.
  • A condition produces the Boolean value True or False.
  • The indented body runs only when the condition is True.
  • When the condition is False, Python skips the body; an if statement alone does not choose an alternative action.
  • Use == for equality comparisons and = for assignment.
  • The colon and consistent indentation are required for a Python if block.
  • Multiple statements belong to the block when they share the same indentation beneath the if line.

After learning basic if statements, the next related topics include comparison operators and additional conditional logic.