How to Check If a Value Is True in Python

PythonPythonBeginner
Practice Now

Introduction

In this lab, you'll learn how to check if a value is True in Python. We'll start by exploring the fundamental boolean values True and False, understanding their role in decision-making and program flow.

You'll then learn to use direct comparison with True using the equality operator == to check if a condition is met. Finally, you'll gain an understanding of truthy values in Python.


Skills Graph

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

Learn About True and False

In this step, we will explore the fundamental boolean values in Python: True and False. These values are essential for making decisions and controlling the flow of your programs.

In Python, True and False are keywords that represent boolean values. They are used to represent the truthiness or falseness of a condition. Let's start by examining these values directly in a Python script.

  1. Open your VS Code editor.

  2. Create a new file named boolean_values.py in your ~/project directory.

    cd ~/project
  3. Add the following content to the boolean_values.py file:

    ## Assign True and False to variables
    is_true = True
    is_false = False
    
    ## Print the values and their types
    print("Value of is_true:", is_true)
    print("Type of is_true:", type(is_true))
    
    print("Value of is_false:", is_false)
    print("Type of is_false:", type(is_false))

    This script assigns the boolean values True and False to the variables is_true and is_false, respectively. It then prints the values and their corresponding types using the print() function.

  4. Run the script using the python command:

    python boolean_values.py

    You should see the following output:

    Value of is_true: True
    Type of is_true: <class 'bool'>
    Value of is_false: False
    Type of is_false: <class 'bool'>

    This output confirms that True and False are boolean values in Python, and their type is <class 'bool'>.

Use Direct Comparison with True

In this step, we will learn how to use direct comparisons with the boolean value True. Direct comparison is a fundamental concept in programming that allows you to check if a condition is true or false.

In Python, you can use the equality operator == to compare a variable or expression directly with True. This is a common way to check if a condition is met. Let's create a script to demonstrate this.

  1. Open your VS Code editor.

  2. Create a new file named compare_with_true.py in your ~/project directory.

    cd ~/project
  3. Add the following content to the compare_with_true.py file:

    ## Assign a boolean value to a variable
    is_valid = True
    
    ## Compare the variable directly with True
    if is_valid == True:
        print("The condition is True.")
    else:
        print("The condition is False.")
    
    ## Another example with a different variable
    is_active = False
    
    if is_active == True:
        print("The condition is True.")
    else:
        print("The condition is False.")

    In this script, we first assign the boolean value True to the variable is_valid. Then, we use an if statement to check if is_valid is equal to True. If it is, the script prints "The condition is True.". Otherwise, it prints "The condition is False.". We then repeat this process with a variable is_active set to False.

  4. Run the script using the python command:

    python compare_with_true.py

    You should see the following output:

    The condition is True.
    The condition is False.

    This output shows that the script correctly identifies when a variable is equal to True and when it is not.

Understand Truthy Values

In this step, we will explore the concept of "truthy" values in Python. In Python, not everything is explicitly True or False, but certain values are treated as True in a boolean context, while others are treated as False. Understanding truthy values is crucial for writing concise and effective conditional statements.

In Python, the following values are considered "falsy" (treated as False in a boolean context):

  • False
  • None
  • 0 (zero)
  • "" (empty string)
  • [] (empty list)
  • {} (empty dictionary)
  • () (empty tuple)

All other values are considered "truthy" (treated as True in a boolean context). Let's create a script to demonstrate this.

  1. Open your VS Code editor.

  2. Create a new file named truthy_values.py in your ~/project directory.

    cd ~/project
  3. Add the following content to the truthy_values.py file:

    ## Examples of truthy and falsy values
    
    ## Falsy values
    falsy_bool = False
    falsy_none = None
    falsy_int = 0
    falsy_string = ""
    falsy_list = []
    falsy_dict = {}
    falsy_tuple = ()
    
    ## Truthy values
    truthy_int = 1
    truthy_string = "Hello"
    truthy_list = [1, 2, 3]
    truthy_dict = {"key": "value"}
    
    ## Check truthiness in if statements
    if falsy_bool:
        print("falsy_bool is True")
    else:
        print("falsy_bool is False")
    
    if falsy_none:
        print("falsy_none is True")
    else:
        print("falsy_none is False")
    
    if falsy_int:
        print("falsy_int is True")
    else:
        print("falsy_int is False")
    
    if truthy_int:
        print("truthy_int is True")
    else:
        print("truthy_int is False")

    This script defines several variables with different values, some of which are falsy and some of which are truthy. It then uses if statements to check the truthiness of each variable and prints a message accordingly.

  4. Run the script using the python command:

    python truthy_values.py

    You should see the following output:

    falsy_bool is False
    falsy_none is False
    falsy_int is False
    truthy_int is True

    This output demonstrates how different values are treated as either True or False in a boolean context. Understanding truthy values allows you to write more concise and readable code by implicitly checking for conditions without explicitly comparing them to True or False.

Summary

In this lab, we began by exploring the fundamental boolean values True and False in Python. We learned that these are keywords representing the truthiness or falseness of a condition and that their type is <class 'bool'>. We assigned these values to variables and printed their values and types to confirm their nature.

Next, we started to learn about direct comparison with the boolean value True using the equality operator ==. This allows us to check if a condition is met.