Relational (comparison) operators compare two values and return a Boolean: == equal, != not equal, < less than, > greater than, <= less than or equal, and >= greater than or equal. Note that = assigns while == compares. Numbers of different types compare by value, so 1 == 1.0 is True and True == 1 is also True. Strings compare character by character using their character codes, so "B" < "a" is True because uppercase letters come before lowercase ones, and "apple" < "apricot" is True because p comes before r at the first difference. Comparing a string with a number using < raises TypeError, although == simply returns False.
Keep reading for free
Create a free StudyToCert account to read the rest of this lesson: 7 more sections, 6 key terms, a real-world example, an exam tip and self-check questions. Every lesson, lab and practice test is free with an account.