Python Tutorial #4: Booleans

Boolean values in Python are a data type that can only take two values: True or False. These values are often used to control conditions in logical expressions, loops, and control structures such as if, while, and for statements.


Characteristics of Booleans in Python

  • The type of Boolean values is bool.
  • In Python, comparisons, logical operations, and many other functions return Boolean values.
  • Everything can be evaluated as True or False: Numbers: 0 is considered False, while any other number is True. Lists, strings, and other containers: Empty ones are False, while non-empty ones are True.

# boolean values
x = True
y = False

print( 5 > 3 ) # True
print(2 == 5 ) # False

if x:
    print("x value is True")
else:
    print("x value is False")
                                        

Boolean values are fundamental for controlling the flow of programs.