HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

9) Operators and Booleans Lesson

Python Relational Operators

8 min to complete · By Martin Breuss

Relational operators in Python are also called comparison operators. These operators allow you to put values in relation to each other and compare them.

Python's Six Relational Operators

Python has six relational operators to compare number values. The result of such a comparison evaluates to a boolean value.

Colorful illustration of a light bulb

Info: Similar to how you were able to use the + operator both for numbers and strings, other operators can also work with different data types. You'll learn to use comparison operators with numbers first, but you'll see how to use them with strings as well.

Operator Meaning Example Result
< Less than 1 < 2 True
<= Less than or equal to 1 <= 1 True
> Greater than 2 > 1 True
>= Greater than or equal to 2 >= 2 True
== Equal to 2 == 2 True
!= Not equal to 2 != 1 True

You might already be familiar with some of these operators, while others are probably new to you. Keep in mind that each of them represents a single symbol, even if it consists of two characters. This means you always need to write them without any space in between the characters to keep Python from complaining.

Comparison Operator Example

Comparison operators work on numbers and you can ask Python questions that it will diligently answer with Boolean values:

1 < 2   # True
1 == 2  # False

However, they also work on other data types that can be compared to each other, for example, strings.

Colorful illustration of a light bulb

Info: For an operator to work on a data type, it needs to be implemented for that data type. Not all operators are implemented for all data types since not all operators make sense for all data types. For example, what would be an exponent of the string "hello"?

When to Use Python Comparison Operators

Comparison operators, especially == and !=, are commonly used in Python programs. Your code might want to make decisions based on comparing one value to another one, also if these values are not numbers.

This is why comparison operators are implemented across the most common Python data types. You can compare numbers to numbers and strings to strings:

# Comparison operators work on numbers
1 < 2   # True
1 == 2  # False

# And on other datatypes
"hello" == "hola"  # False

In this example, you're asking Python whether one string is the same as another one. While a bilingual human might say that "hello" and "hola" mean the same thing, Python doesn't think about meaning at all.

It just compares whether the first sequence of characters is the same as the second sequence of characters. Since it isn't, Python answers you with the Boolean value False.

Relation Operators Exercise

  • Test all the different operators and make sure you understand what they do.
  • Can you compare strings using > and <?

When comparing strings to one another, you can do more than just find out whether one string is the same as another one.

Playground: Comparing Strings Exercise

In Python, characters have a specific order, just like numbers do. This means that you can use the same comparison operators on strings as you did with numbers. Go ahead and play around with them and see whether you can figure out the underlying pattern.

  • What is larger: "a" or "i"?
  • Who is smaller: "Harry" or "Sally"?

Use the code playground below to try using all the different comparison operators on strings and answer the questions above.

# Which character is 'bigger' than another?
print("a" < "b")
print("Harry" > "Sally")
print("Harry" > "Harrx")

What is the intrinsic order that alphabetic characters have that Python could use to compare against? Feel free to speculate and share your thoughts and read up on those of others in Discord.

If you find that they have a logical order, then you'll be pleased to hear that the type of Python operators you'll learn about next will be logical operators.

Summary: Python Relational Operators

  • Relational operators allow you to put values in relation to each other and compare them
  • Relational operators are also called comparison operators
  • Comparison operators are commonly used in code since values often need to be compared

Python's Six Relational Operators

  • Less than ( < )
  • Less than or equal to ( <= )
  • Greater than ( > )
  • Greater than or equal to ( >= )
  • Equal to ( == )
  • Not equal to ( != )