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:

    python
    result = value1 < value2
    
    • value1 and value2 are the values you want to compare.
    • result will be True if value1 is less than value2, and False if not.

Examples and Exercises:

  1. Comparing Two Numbers:

    python
    x = 5
    y = 12
    result = x < y
    print(result)  # Output: True
    
    x = 8
    y = 7
    result = x < y
    print(result)  # Output: False
    
  2. Comparing Strings:

    • You can also compare text using the less than operator. Python compares them based on alphabetical order.
    python
    x = '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: False
    
  3. Comparing Lists:

    • Lists can also be compared in Python. The comparison happens element by element.
    python
    x = [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!