5.10. Operators#

5.10.1. Relational (comparison) operators#

In addition to the mathematical operators we have covered, there are relational operators.

  • < less than

  • <= less than or equal to

  • > greater than

  • >= greater than or equal to

  • == equal to

  • != not equal to

  • is the same instance, has the same location in memory [1].

5.10.2. Bitwise operators#

These are typically employed for operations involving integers. However, they also apply to the Python set type.

  • & is bitwise AND of the arguments

  • | is bitwise inclusive OR of the arguments

  • ^ is bitwise exclusive OR of the arguments

5.10.3. Logical operators#

These are used to extend logical statements, joining clauses.

  • and if both clauses evaluate as True

a, b = 20, 5

a > 0 and b > 0
True
  • or, if either clause evaluate as True

a > 0 or b > 0
True
  • in, an element is a member of a series

series = [0, "a", 23]

"a" in series
True

5.11. Exercises#

  1. Try the relational operators on different data types, e.g.

    "abcd" < "ABCD"
    
    False
    
  2. What happens if you try them on different data types?

  3. For the data below, write a single statement to evaluate whether a and b have the same value but are different instances.

    a = {0: "text"}
    b = {0: "text"}
    c = b
    

    Tip: Different instances will have a location in memory.

  4. For the data above, write a single statement to evaluate whether b and c have same value but are different instances.