Booleans

Booleans can have only one of two values: either False or True. They are used to describe logical operations, for example, tests or conditions. There are a few operations that result in Boolean values:

  • First, you can use the equality test for any type of data:
>>> 'World' == 'World'
True


>>> pi == pi
True
  • In the previous section, we ran into another example, which was inclusion test:
>>> “World” in “Hello World!”
True
  • != is the opposite of equal, not equal:
>>> pi != pi
False

There are more test operators, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), and nonequal (<>). Those tests will also work with strings by comparing them lexicographically. Python will compare the order of the first elements of each string. If they are equal, then Python will go to the next pair, and so on. If any pair is not equal (not the same character), or one string is shorter than another, then this will define the outcome.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset