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.
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.
Open your VS Code editor.
Create a new file named
boolean_values.pyin your~/projectdirectory.cd ~/projectAdd the following content to the
boolean_values.pyfile:## 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
TrueandFalseto the variablesis_trueandis_false, respectively. It then prints the values and their corresponding types using theprint()function.Run the script using the python command:
python boolean_values.pyYou 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
TrueandFalseare 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.
Open your VS Code editor.
Create a new file named
compare_with_true.pyin your~/projectdirectory.cd ~/projectAdd the following content to the
compare_with_true.pyfile:## 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
Trueto the variableis_valid. Then, we use anifstatement to check ifis_validis equal toTrue. If it is, the script prints "The condition is True.". Otherwise, it prints "The condition is False.". We then repeat this process with a variableis_activeset toFalse.Run the script using the python command:
python compare_with_true.pyYou 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
Trueand 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):
FalseNone0(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.
Open your VS Code editor.
Create a new file named
truthy_values.pyin your~/projectdirectory.cd ~/projectAdd the following content to the
truthy_values.pyfile:## 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
ifstatements to check the truthiness of each variable and prints a message accordingly.Run the script using the python command:
python truthy_values.pyYou should see the following output:
falsy_bool is False falsy_none is False falsy_int is False truthy_int is TrueThis output demonstrates how different values are treated as either
TrueorFalsein a boolean context. Understanding truthy values allows you to write more concise and readable code by implicitly checking for conditions without explicitly comparing them toTrueorFalse.
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.



