官术网_书友最值得收藏!

Booleans

The Boolean data type has two, and only two, possible values, namely, True and False. In Python, you can produce a Boolean value by writing either True or False, starting with a capital T or F respectively:

>> True
>> False

Boolean values can be combined using bitwise operators:

  • The and operator yields True if the value on the left and the value on the right are both True
  • The or operator yields True if at least one of the items on the left or right is True

The following are some examples using bitwise operators:

>> True and True
>> True and False
>> True or False
>> False or False

Logical operators can be used to produce Boolean values from other data types by comparing values:

  • The == operator yields True if the values on either side are equal
  • The > operator yields True if value to the left is greater than the value to the right
  • The < operator yields True if the value to the right is greater than the value to the left

The following are some examples using logical operators:

>> 1 == 2
>> 1 == 1
>> 1 < 2
>> 1 < 0
>> 1 > 2
>> 2 > 1
>> "abc" == "abc"
>> "abc" == "cba"

Boolean values are good to make decisions within computer programs. You will see this when I introduce if statements later in the chapter. They are also good to store simple yes/no data entries, such as whether a particular individual lives in New York or whether a transaction has been completed.

主站蜘蛛池模板: 曲松县| 儋州市| 满城县| 乾安县| 哈尔滨市| 桂阳县| 崇左市| 福海县| 吉木乃县| 波密县| 定陶县| 延庆县| 荥经县| 合山市| 沭阳县| 镇坪县| 文水县| 彝良县| 磐石市| 清镇市| 思茅市| 桐庐县| 大足县| 铁岭县| 隆安县| 宁安市| 石嘴山市| 泉州市| 衡阳市| 洛扎县| 正宁县| 马龙县| 开阳县| 师宗县| 梧州市| 尤溪县| 长治县| 鄂尔多斯市| 湘阴县| 利川市| 德阳市|