Editor settings

General settings

Save settings

Learn to program with Python

Other comparison operators #

Aside from equality operators, we can implement how our objects behave with other comparison operators. Here's a list of operators and their corresponding methods:

Operator Method
== equal to __eq__(self, other)
< less than __lt__(self, other)
<= less than or equal to __le__(self, other)
> greater than __gt__(self, other)
>= greater than or equal to __ge__(self, other)

Exercise #

  • Implement all types of comparison operators for our MyStr class.

Tests #

  • MyStr("1") == MyStr("1") is True
  • MyStr("1") != MyStr("1") is False
  • MyStr("1") < MyStr("1") is False
  • MyStr("1") > MyStr("1") is False
  • MyStr("1") <= MyStr("1") is True
  • MyStr("1") >= MyStr("1") is True
Output will be displayed here