The if Statement in Python

In this post I explain the Python if statement, indentation, elif, else, and logical operators.
TopicsPython

The if Statement in Python

In this lesson we will look at one of the most important programming constructs: the if statement. It is what programs use to make decisions based on conditions.

The if statement checks the condition written after it. If that condition evaluates to True, Python runs the code inside the if block. If it evaluates to False, the block is skipped.

Example

n = 42
if n > 0:
    result = "n is greater than 0"

result  # 'n is greater than 0'

Here n > 0 is the comparison operator “greater than”. We will cover operators like this in more detail in the lesson about the operator module.

Indentation in Code Blocks

In Python, indentation is required. If you forget to indent after if, Python raisesIndentationError:

n = 42
if n > 0:
result = "n is greater than 0"
# IndentationError: expected an indented block after 'if' statement

The number of spaces inside one block also has to stay consistent, otherwise you get anotherIndentationError:

n = 42
if n > 0:
    n = 0
     result = "n is now 0"
# IndentationError: unexpected indent

According to PEP 8, the standard indentation size is exactly 4 spaces.

Variables Inside an if Block

Variables created inside an if block exist only if the condition actually runs. If the condition isFalse, the variable is never created:

n = -1
if n > 0:
    no_result = "n is greater than 0"

no_result  # NameError: name 'no_result' is not defined

The else Keyword

If the condition in if is false, you can add an else block. Python runs it in that case:

n = -1
if n > 0:
    result = "n is greater than 0"
else:
    result = "n is not greater than 0"

result  # 'n is not greater than 0'

Nested if Statements

As with any other block, you can put more if statements inside an if block:

n = 0
if n > 0:
    result = "n is greater than 0"
else:
    if n == 0:
        result = "n equals 0"
    else:
        result = "n is less than 0"

result  # 'n equals 0'

The elif Keyword

To avoid nested if blocks, you can use elif, which stands for else if:

n = 0
if n > 0:
    result = "n is greater than 0"
elif n == 0:
    result = "n equals 0"
else:
    result = "n is less than 0"

result  # 'n equals 0'

The Ternary Operator

Python also has a short one-line form of if called the ternary operator:

n = 1
result = "n is greater than 0" if n > 0 else "n is not greater than 0"
result  # 'n is greater than 0'

Python evaluates the condition first. If it is True, Python takes the expression beforeelse. Otherwise it takes the expression after it. In this form the else part is required.

n = 1
result = "n is greater than 0" if n > 0
# SyntaxError: expected 'else' after 'if' expression

Logical Operators

In Python you can check multiple conditions at once with the logical operators and, or, and not.

and

A condition with and is true only if both subconditions are true:

n = 6
if n % 2 == 0 and n % 3 == 0:
    result = "n is divisible by 6"

result  # 'n is divisible by 6'

or

A condition with or is true if at least one subcondition is true:

n = -1
if n > 0 or n < 0:
    result = "n is not equal to 0"
else:
    result = "n equals 0"

result  # 'n is not equal to 0'

not

The not operator flips the result of an expression: it turns True intoFalse and vice versa.

n = 0
if not n:
    result = "n equals 0"

result  # 'n equals 0'

Operator Precedence

In Python the precedence of logical operators is: not → and → or. Python evaluates not first, then and, then or.

result = not False and True or False
result  # True

The same expression with parentheses:

result = ((not False) and True) or False
result  # True