Greater Than

 

What Is the Greater Than Operator?

  • The greater than operator > in Python is used to compare if one value is larger than another.

How to Use It:

  • The basic way to use the greater than operator is:

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

Examples and Exercises:

  1. Comparing Two Numbers:

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

    • You can also compare text using the greater than operator. Python compares them based on alphabetical order.
    python
    x = 'apple'
    y = 'banana'
    z = 'cherry'
    k = 'Apple'
    print(y > x)  # Output: True
    print(y > z)  # Output: False
    print(x > k)  # Output: True
    
  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: False
    print(y > z)  # Output: True
    print(x > z)  # Output: True
    

Summary:

  • The greater than operator > in Python helps you determine if one value is larger than another. It works with numbers, text, and even lists. Try the examples to see how it works!