📜  python booleans - Python (1)

📅  最后修改于: 2023-12-03 15:18:54.097000             🧑  作者: Mango

Introduction to Python Booleans

Python booleans are a data type used to represent truth values. They have only two possible values – True and False. In addition, in Python, True and False are actually built-in constants that represent the values 1 and 0, respectively.

Creating a boolean variable in Python

To create a boolean variable in Python, you simply assign a value of either True or False to a variable. For example:

my_bool = True
print(my_bool)    # Output: True

other_bool = False
print(other_bool)  # Output: False
Boolean operators

Python has several operators that can be used with boolean values. Some of the most common operators are:

  • and : This operator returns True if both operands evaluate to True, otherwise it returns False.
  • or : This operator returns True if at least one of the operands evaluates to True, otherwise it returns False.
  • not : This operator negates the boolean value of the operand. If the operand is True, it returns False, and if the operand is False, it returns True.

For example:

# and operator
print(True and True)   # Output: True
print(True and False)  # Output: False
print(False and False) # Output: False

# or operator
print(True or True)     # Output: True
print(True or False)    # Output: True
print(False or False)   # Output: False

# not operator
print(not True)    # Output: False
print(not False)   # Output: True
Comparison operators

Python also has several comparison operators that can be used with boolean values. Some of the most common ones are:

  • == : This operator returns True if the operands are equal, otherwise it returns False.
  • != : This operator returns True if the operands are not equal, otherwise it returns False.
  • < : This operator returns True if the left operand is less than the right operand, otherwise it returns False.
  • > : This operator returns True if the left operand is greater than the right operand, otherwise it returns False.
  • <= : This operator returns True if the left operand is less than or equal to the right operand, otherwise it returns False.
  • >= : This operator returns True if the left operand is greater than or equal to the right operand, otherwise it returns False.

For example:

# comparison operators
print(3 == 3)     # Output: True
print(3 != 3)     # Output: False
print(3 < 5)      # Output: True
print(3 > 5)      # Output: False
print(3 <= 5)     # Output: True
print(3 >= 5)     # Output: False
Booleans and conditionals

Booleans are often used in conditional statements in Python. For example, you might use a boolean expression to determine whether to execute a certain block of code:

x = 5

if x > 10:
    print("x is greater than 10")
else:
    print("x is less than or equal to 10")

In this example, the boolean expression x > 10 is evaluated. If it is True, then the first block of code is executed. If it is False, then the second block of code is executed.

Conclusion

Python booleans are a fundamental data type that is used to represent truth values in Python. They are represented by the built-in constants True and False, and can be combined using boolean operators such as and, or, and not. Booleans are also commonly used in conditional statements to control the flow of code execution.