Boolean is a built-in data type in Python that is used to represent truth values, which can either be True or False. Boolean values are usually the result of a comparison operation or a logical operation in Python.
Here are some examples of how to use Booleans in Python 3.10:
Example 1: Checking if a number is greater than another number
x = 10
y = 5
print(x > y) # Outputs True
print(y > x) # Outputs False
In the above example, the greater than operator (>) compares the values of x and y. Since 10 is greater than 5, the expression x > y evaluates to True.
Example 2: Using Boolean values as the result of a function
def is_even(number):
return number % 2 == 0
print(is_even(4)) # Outputs True
print(is_even(3)) # Outputs False
In the above example, the function is_even returns True if the given number is even and False otherwise. The modulo operator (%) is used to check if the number is even. If the remainder of dividing the number by 2 is 0, then the number is even and the function returns True.
Example 3: Using logical operators to combine Boolean values
x = 5
y = 3
z = 7
print(x < y or x < z) # Outputs True
print(x < y and x < z) # Outputs False
print(not x < y) # Outputs True
In the above example, the logical operators (or, and, and not) are used to combine Boolean values. The or operator returns True if at least one of the expressions is True, while the and operator returns True only if both expressions are True. The not operator negates the value of the expression.