In Python, a boolean variable is a variable that can hold one of two values: True
or False
. These values represent the truth values of logic and are used to control the flow of programs through conditional statements like if
, else
, and while
loops.
Creating a boolean variable and assigning values to it in Python is straightforward. You can follow this syntax:
# Creating a boolean variable and assigning values
variable_name = True # For True value
variable_name = False # For False value
Here, variable_name
is the name you choose for your boolean variable. According to Python’s naming rules, you can replace it with any valid variable name.
Examples:
# Creating and assigning boolean values to variables
is_sunny = True
has_passed_exam = False
is_logged_in = True
has_permission = False
Once you’ve created boolean variables, you can use them in conditional statements, loops, and other parts of your code to control the program’s flow based on the truth values of these variables.
Remember that in Python, boolean values are case-sensitive, meaning the correct values are True
and False
. Writing them in uppercase is essential for Python to recognize them as boolean literals. Writing them in lowercase (true
or false
) will result in a NameError
.
Python boolean variable example
Boolean variables are often used in conditional statements to make decisions based on certain conditions. For example:
# Example of using a boolean variable in a conditional statement
is_raining = True
if is_raining:
print("Remember to take an umbrella!")
else:
print("It's not raining. You don't need an umbrella.")
Output:
In this example, the is_raining
boolean variable is used to determine whether to print a reminder to take an umbrella or not, based on its value.
Boolean variables can also be the result of a comparison or logical operations, such as:
# Comparisons that return boolean values
x = 10
y = 5
is_greater = x > y # True, because 10 is greater than 5
is_equal = x == y # False, because 10 is not equal to 5
# Logical operations that return boolean values
a = True
b = False
logical_and = a and b # False, because both operands must be True for the result to be True
logical_or = a or b # True, because at least one operand is True
logical_not_a = not a # False, because the negation of True is False
logical_not_b = not b # True, because the negation of False is True
Another example
# Suppose we have two boolean variables representing a user's status
logged = True
membership = False
# Checking if the user is logged in and has a premium membership
if logged and membership:
print("Welcome! You have access to premium content.")
elif logged and not membership:
print("Welcome! Consider upgrading to premium membership for exclusive content.")
elif not logged:
print("Please log in to access our content.")
else:
print("Something went wrong. Please contact support.")
# Let's assume the user decides to upgrade to premium membership
membership = True
# Now, let's check the user's status again
if logged and membership:
print("Welcome! You have access to premium content.")
elif logged and not membership:
print("Welcome! Consider upgrading to premium membership for exclusive content.")
elif not logged:
print("Please log in to access our content.")
else:
print("Something went wrong. Please contact support.")
By using boolean variables and conditional statements, you can make decisions and tailor the program’s behavior based on different conditions in your Python code.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.