Less Than
What Is the Less Than Operator?
- The less than operator
<in Python is used to compare if one value is smaller than another.
How to Use It:
The basic way to use the less than operator is:
pythonresult = value1 < value2value1andvalue2are the values you want to compare.resultwill beTrueifvalue1is less thanvalue2, andFalseif not.
Examples and Exercises:
Comparing Two Numbers:
pythonx = 5 y = 12 result = x < y print(result) # Output: True x = 8 y = 7 result = x < y print(result) # Output: FalseComparing Strings:
- You can also compare text using the less than operator. Python compares them based on alphabetical order.
pythonx = 'apple' y = 'banana' z = 'cherry' k = 'Apple' print(x < y) # Output: True print(y < z) # Output: True print(x < z) # Output: True print(x < k) # Output: FalseComparing Lists:
- Lists can also be compared in Python. The comparison happens element by element.
pythonx = [41, 54, 21] y = [98, 8] z = [41, 54, 4, 6] print(x < y) # Output: True print(y < z) # Output: False print(x < z) # Output: False
Summary:
- The less than operator
<in Python helps you determine if one value is smaller than another. It works with numbers, text, and even lists. Try the examples to see how it works!