CodeGym /Courses /Python SELF EN /Working with Boolean Type

Working with Boolean Type

Python SELF EN
Level 5 , Lesson 5
Available

5.1 The bool Type

In Python, there's a special boolean data type that lets you check the truthfulness of conditions and expressions. It's represented by the keyword bool (though it's usually called Boolean) and is used to denote one of two values: True or False.

This data type is super useful in programming for controlling the execution flow with conditional statements like if, else, and elif, as well as for loops and other control structures.

Important Note

The Boolean data type in Python is represented by two constant objects: True and False. However, it's important to note that Python treats True as the numerical value 1 and False as 0. This allows them to be used in arithmetic operations and makes the bool type a subclass of the int type.

Practical Application

Despite having only two possible values, the bool type is powerful not for its values, but for the set of logical operations you can perform with them. More on that below.

5.2 Logical Operators

Logical operations in Python play a key role in controlling the flow of a program and making decisions. The main logical operations include and, or, and not. Understanding how to use these can really boost the efficiency of your code.

Logical AND (AND)

The and operator returns True only if both of its arguments are true (True). This operator is often used to check the truth of multiple conditions at the same time.


a = True
b = False
print(a and b)  # Prints: False 

Logical OR (OR)

The or operator returns True if at least one of its arguments is true. It's useful for conditions where the code should continue if at least one of several possible conditions is met.


a = True
b = False
print(a or b)  # Prints: True

Logical NOT (NOT)

The not operator inverts the Boolean value of its argument. If the argument is True, the result will be False and vice versa. This can be helpful for creating negation conditions.


a = True
print(not a)  # Prints: False

Advanced Usage

Logical operators are often used in combination to create complex conditional expressions that help control more intricate execution flows.


a = 5
b = 10
c = 20
result = a < b and b < c  # Checks that a is less than b and b is less than c
print(result)  # Prints: True

Logical operations in Python provide a powerful tool to manage program behavior based on different conditions. They don't just simplify writing code, but also make it more readable and maintainable.

5.3 Practical Examples

Python allows you to convert other data types to boolean using the bool() function. By default, any non-zero or non-empty value will be converted to True, while zero or empty values will be converted to False.

Type Casting Examples:


print(bool(0))  # Prints False 
print(bool(42))  # Prints True
print(bool(""))  # Prints False
print(bool("Text"))  # Prints True

Using Boolean Variables in Conditions:


a = 5
b = 10
c = 20
is_a_min = a < b and a < c  # Checks that a is less than b and a is less than c 
print(is_a_min)  # Prints: True

You don't need to compare a boolean variable to True or False, because the result of the comparison itself is True or False. Example:

Starting Code Moved Starting Code to a Separate Variable Removed == True

if a < b:
    print("a is min")

                    is_a_less = a < b
                    if is_a_less == True:
                        print("a is min")              
                

                    is_a_less = a < b
                    if is_a_less:
                        print("a is min")               
                

The result of any comparison is either True or False, so you can assign it to a variable as was done in the example above. But in an if statement, or a while loop, you don't need to compare a boolean variable to True. The boolean variable already holds the correct value. Just give it a proper name, and everything will fall into place.

In other words, instead of writing "if a == True:", you should just write "if a:". And instead of writing "if a == False:", you should write "if not a:".

2
Task
Python SELF EN, level 5, lesson 5
Locked
Access Denied
Access Denied
2
Task
Python SELF EN, level 5, lesson 5
Locked
Leap Year
Leap Year
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION