How to Check If a Value Is False in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a value is False in Python. The lab starts by exploring False values, demonstrating how to assign False to a variable, print its value and type, and use it in conditional statements. You'll create a false_values.py file, assign False to is_valid, and use an if statement to check its value.

The lab then proceeds to explain how to compare directly with False and differentiate it from falsy values.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/ControlFlowGroup(["Control Flow"]) python(("Python")) -.-> python/PythonStandardLibraryGroup(["Python Standard Library"]) python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python/BasicConceptsGroup -.-> python/booleans("Booleans") python/ControlFlowGroup -.-> python/conditional_statements("Conditional Statements") python/PythonStandardLibraryGroup -.-> python/data_collections("Data Collections") subgraph Lab Skills python/booleans -.-> lab-559592{{"How to Check If a Value Is False in Python"}} python/conditional_statements -.-> lab-559592{{"How to Check If a Value Is False in Python"}} python/data_collections -.-> lab-559592{{"How to Check If a Value Is False in Python"}} end

Explore False Values

In this step, you will learn about False values in Python. Understanding False values is crucial for writing conditional statements and controlling the flow of your programs. In Python, False is a built-in constant that represents the boolean value of false. It is one of the two boolean values, the other being True.

Let's start by creating a Python file named false_values.py in your ~/project directory using the VS Code editor.

~/project/false_values.py

Now, open false_values.py in the editor and add the following code:

## Assign the boolean value False to a variable
is_valid = False

## Print the value of the variable
print(is_valid)

## Check the type of the variable
print(type(is_valid))

This code snippet first assigns the boolean value False to the variable is_valid. Then, it prints the value of the variable and its type.

To run the script, open your terminal and execute the following command:

python ~/project/false_values.py

You should see the following output:

False
<class 'bool'>

This output confirms that the variable is_valid holds the boolean value False and that its type is bool.

Now, let's explore how False values are used in conditional statements. Modify your false_values.py file to include the following code:

is_valid = False

if is_valid == False:
    print("The value is False")
else:
    print("The value is True")

In this example, we use an if statement to check if the value of is_valid is equal to False. If it is, the code inside the if block is executed. Otherwise, the code inside the else block is executed.

Run the script again:

python ~/project/false_values.py

You should see the following output:

The value is False

This output demonstrates how False values can be used to control the flow of your program based on certain conditions.

Compare Directly with False

In this step, you will learn how to compare values directly with False in Python. Comparing directly with False can sometimes lead to more readable and concise code.

Continue using the false_values.py file you created in the previous step. We will modify it to demonstrate direct comparison with False.

Open false_values.py in the VS Code editor and change the code to the following:

is_valid = False

if is_valid is False:
    print("The value is False")
else:
    print("The value is True")

In this example, we use the is operator to check if the variable is_valid is identical to False. The is operator checks for object identity, meaning it verifies if two variables refer to the same object in memory. In the case of boolean values, is and == often behave similarly, but is is generally preferred for checking against True and False because they are singleton objects.

Run the script:

python ~/project/false_values.py

You should see the following output:

The value is False

Now, let's consider a slightly different scenario. Modify your false_values.py file to include the following code:

is_valid = False

if not is_valid:
    print("The value is False")
else:
    print("The value is True")

In this example, we use the not operator to check if is_valid is False. The not operator negates the boolean value of the variable. This is a common and concise way to check if a variable is False.

Run the script again:

python ~/project/false_values.py

You should see the same output:

The value is False

Both is False and not is_valid are valid ways to check for False values in Python. The choice between them often comes down to personal preference and code readability. The not operator is generally considered more Pythonic and easier to read.

Differentiate from Falsy Values

In this step, you will learn to differentiate between the boolean value False and other "falsy" values in Python. In Python, certain values are considered "falsy" because they evaluate to False in a boolean context, even though they are not explicitly False. Understanding the difference is crucial for writing accurate conditional statements.

Common falsy values include:

  • False (the boolean value)
  • None
  • 0 (zero of any numeric type: int, float, etc.)
  • "" (an empty string)
  • [] (an empty list)
  • {} (an empty dictionary)
  • () (an empty tuple)

Let's modify the false_values.py file to explore falsy values. Open false_values.py in the VS Code editor and add the following code:

def check_falsy(value):
    if value:
        print(f"{value!r} is truthy")
    else:
        print(f"{value!r} is falsy")

check_falsy(False)
check_falsy(None)
check_falsy(0)
check_falsy("")
check_falsy([])
check_falsy({})
check_falsy(())

This code defines a function check_falsy that takes a value as input and checks if it is truthy or falsy using an if statement. Then, it calls the function with different falsy values. The !r in the f-string ensures that the representation of the value is printed, which is helpful for distinguishing between different types of empty values.

Run the script:

python ~/project/false_values.py

You should see the following output:

False is falsy
None is falsy
0 is falsy
'' is falsy
[] is falsy
{} is falsy
() is falsy

This output demonstrates that all the values we passed to the check_falsy function are considered falsy in Python.

Now, let's see how this can affect conditional statements. Modify your false_values.py file to include the following code:

empty_list = []

if empty_list:
    print("The list is not empty")
else:
    print("The list is empty")

In this example, we check if an empty list is truthy or falsy. Since an empty list is falsy, the code inside the else block will be executed.

Run the script:

python ~/project/false_values.py

You should see the following output:

The list is empty

Understanding falsy values is essential for writing correct and efficient Python code. Be aware of these values when writing conditional statements and make sure your code behaves as expected.

Summary

In this lab, you began by exploring False values in Python, understanding that False is a built-in constant representing the boolean value of false. You created a false_values.py file, assigned False to a variable, printed its value and type, confirming it as a boolean.

You then used an if statement to check if a variable's value is equal to False, demonstrating how False values can control program flow based on conditions. The lab is set to continue exploring direct comparison with False and differentiation from falsy values.