Introduction to Python Resources

Begin your free Python journey now,
at your own pace

check

Syntax: Else Statements

check

Creating fallback code with Python else statements.


Python else Conditionals

In Python, the else statement is used in conjunction with an if statement to provide a block of code will run if the conditional expression given to the if statement evaluates to False.

An else statement can only follow an if statement, which means the either the if code or the else code will run, but not both. Any code after the indented code of both the if statement and the else statement will always run.

Note the following example with a conditional expression which evaluates to True:

if True:
  print("This code will run if the conditional is True.")
else:
  print("This code will run if the conditional is False.")
print("This code will always run.")

> "This code will run if the conditional is True."
> "This code will always run."

Because the conditional expression of the above example evaluates to True, the print statement which is indented below the if statement will run, and the code block for the else statement will not run. You will also notice that the unindented code below the if and else statements also runs.

Now let's see an example where the conditional expression of the if statement evaluates to False:

if False:
  print("This code will run if the conditional is True.")
else:
  print("This code will run if the conditional is False.")
print("This code will always run.")

> "This code will run if the conditional is False."
> "This code will always run."

In the above example, because the conditional expression evaluates to False, the indented code block in the else statement will run and print the message about the conditional being False.

It's also worth pointing out here that if you have code that should only run when a conditional expression evaluates to False, then you'll need to use an if statement with the not operator. Note the examples below:

if not True:
  print("This code will run if the conditional is False.")
print("This code will always run.")

> "This code will always run."

if not False:
  print("This code will run if the conditional is False.")
print("This code will always run.")

> "This code will run if the conditional is False."
> "This code will always run."

To summarize: An else statement is used to run a block of code when the conditional expression of the if statement evaluates to False. The else statement is not required to use an if statement. And, If you want to check for a condition which evaluates to false to run a block of code, use the not operator!

Practice: if-else Conditionals

Based on the information above. Complete the following steps:

  1. Create a variable named age and assign it a numeric value that represents your age.

  2. Create an if statement which checks if the value in the age variable is less than 100.

  3. Add indented code inside the if statement which prints a messege stating that the value of the age variable is less than 100.

  4. Confirm that the indented print statement prints correctly.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > The value of age is less than 100.
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    age = 42
    if age < 100:
      print("The value of age is less than 100.")
    
  5. Next, repeat the above steps with the value of age greater than 100, and confirm the indented code does not run.

    HINT*: Click here for a hint
    *Hints are best viewed after completing a task, or after spending some time and effort attempting it. In cases where a hint contains example output, it is important to understand why it is correct, as there may be many correct outputs.
    > 
    SOLUTION*: Click here for an example solution
    *Example solutions are best viewed after completing a task and understanding the outcome. In most cases, there are multiple ways to complete a task, and the example solution is only one example.
    age = 103
    if age < 100:
      print("The value of age is less than 100.")
    

When the above is completed and you are getting the proper output, you are ready to move onto the next exercise!