3.4. Asking Boolean Questions about Sequences

The + and * operators are not the only operators that work with Python sequence types, and in this section we will look at using Boolean operators to ask questions about a sequence.

3.4.1. Comparing Equality

The comparison operators also work on strings. To see if two strings are equal you simply write a boolean expression using the equality operator.

In [1]: word = "banana"

In [2]: "Yes" if word == "banana" else "No"
Out[2]: 'Yes'

We can perform tests of equality on a simple tuples as well.

In [3]: a = ("one", "two")

In [4]: b = (1, 2)

In [5]: c = (1, 2)

In [6]: a == b
Out[6]: False

In [7]: b == c
Out[7]: True

3.4.2. The in and not in operators

The in operator tests if one string is a substring of another:

In [8]: 'p' in 'apple'
Out[8]: True

In [9]: 'i' in 'apple'
Out[9]: False

In [10]: 'ap' in 'apple'
Out[10]: True

In [11]: 'pa' in 'apple'
Out[11]: False

Note that a string is a substring of itself, and the empty string is a substring of any other string. (Also note that computer scientists like to think about these edge cases quite carefully!)

In [12]: 'a' in 'a'
Out[12]: True

In [13]: 'apple' in 'apple'
Out[13]: True

In [14]: '' in 'a'
Out[14]: True

In [15]: '' in 'apple'
Out[15]: True

The not in operator returns the logical opposite result of in.

In [16]: 'x' not in 'apple'
Out[16]: True

We can also use in and not in to test membership for lists.

In [17]: fruit = ["apple", "orange", "banana", "cherry"]

In [18]: "apple" in fruit
Out[18]: True

In [19]: "pear" in fruit
Out[19]: False

Check your understanding

    rec-5-14: What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(3.14 in alist)
    
  • (A) True
  • Yes, 3.14 is an item in the list alist.
  • (B) False
  • There are 7 items in the list, 3.14 is one of them.

    rec-5-15: What is printed by the following statements?

    alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(57 in alist)
    
  • (A) True
  • in returns True for top level items only. 57 is in a sublist.
  • (B) False
  • Yes, 57 is not a top level item in alist. It is in a sublist.

3.4.3. Other String Comparisons

Other comparison operations are useful for putting words in lexicographical order. This is similar to the alphabetical order you would use with a dictionary, except that all the uppercase letters come before all the lowercase letters.

In [20]: word = "zebra"

In [21]: word < "banana"
Out[21]: False

In [22]: word > "banana"
Out[22]: True

It is probably clear to you that the word apple would be less than (come before) the word banana. After all, a is before b in the alphabet. But what if we consider the words apple and Apple? Are they the same?

In [23]: "apple" < "banana"
Out[23]: True

In [24]: "apple" == "Apple"
Out[24]: False

In [25]: "apple" < "Apple"
Out[25]: False

It turns out, as you recall from our discussion of variable names, that uppercase and lowercase letters are considered to be different from one another. The way the computer knows they are different is that each character is assigned a unique integer value. “A” is 65, “B” is 66, and “5” is 53. The way you can find out the so-called ordinal value for a given character is to use a character function called ord.

In [26]: ord("A")
Out[26]: 65

In [27]: ord("B")
Out[27]: 66

In [28]: ord("5")
Out[28]: 53

In [29]: ord("a")
Out[29]: 97

In [30]: "apple" > "Apple"
Out[30]: True

When you compare characters or strings to one another, Python converts the characters into their equivalent ordinal values and compares the integers from left to right. As you can see from the example above, “a” is greater than “A” so “apple” is greater than “Apple”.

Humans commonly ignore capitalization when comparing two words. However, computers do not. A common way to address this issue is to convert strings to a standard format, such as all lowercase, before performing the comparison.

There is also a similar function called chr that converts integers into their character equivalent.

In [31]: chr(65)
Out[31]: 'A'

In [32]: chr(66)
Out[32]: 'B'

In [33]: chr(49)
Out[33]: '1'

In [34]: chr(53)
Out[34]: '5'

In [35]: ord(" ")
Out[35]: 32

In [36]: print("The character for 32 is", chr(32), "!!!")
The character for 32 is   !!!

One thing to note in the last two examples is the fact that the space character has an ordinal value (32). Even though you don’t see it, it is an actual character. We sometimes call it a nonprinting character.

Check your understanding

    rec-5-16: Evaluate the following comparison:

    "Dog" < "Doghouse"
    
  • (A) True
  • Both match up to the g but Dog is shorter than Doghouse so it comes first in the dictionary.
  • (B) False
  • Strings are compared character by character.

    rec-5-17: Evaluate the following comparison:

    "dog" < "Dog"
    
  • (A) True
  • d is greater than D according to the ord function (68 versus 100).
  • (B) False
  • Yes, upper case is less than lower case according to the ordinal values of the characters.
  • (C) They are the same word
  • Python is case sensitive meaning that upper case and lower case characters are different.

    rec-5-18: Evaluate the following comparison:

    "dog" < "Doghouse"
    
  • (A) True
  • d is greater than D.
  • (B) False
  • The length does not matter. Lower case d is greater than upper case D.

Note

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.

Next Section - 3.5. Mutable and Immutable Sequences