zaro

How to compare variables in Python?

Published in Python Comparison Operators 2 mins read

Comparing variables in Python is done using comparison operators (also known as relational operators) that evaluate the relationship between two values. These operators return a boolean value (True or False) based on whether the comparison holds true or not.

Python provides several standard comparison operators to check for equality, inequality, and magnitude.

Key Comparison Operators in Python

Based on the provided reference, the primary operators used for comparing variables are:

Equal To (==)

This operator checks if two values are equal. It returns True if the values on both sides are identical, and False otherwise.

  • Example:

    a = 10
    b = 10
    print(a == b) # Output: True
    
    c = "hello"
    d = "world"
    print(c == d) # Output: False

Not Equal To (!=)

This operator checks if two values are not equal. It returns True if the values on both sides are different, and False otherwise.

  • Example:

    x = 5
    y = 7
    print(x != y) # Output: True
    
    p = "Python"
    q = "Python"
    print(p != q) # Output: False

Greater Than (>)

This operator checks if the value on the left is greater than the value on the right. It returns True if the condition is met, and False otherwise. This is typically used for numerical comparisons but can apply to sequences (like strings) based on lexicographical order.

  • Example:

    temp1 = 25
    temp2 = 20
    print(temp1 > temp2) # Output: True
    
    name1 = "Charlie"
    name2 = "Alice"
    print(name1 > name2) # Output: True (C comes after A)

Less Than (<)

This operator checks if the value on the left is less than the value on the right. It returns True if the condition is met, and False otherwise. Similar to >, it applies to numerical comparisons and lexicographical order for sequences.

  • Example:

    score1 = 85
    score2 = 90
    print(score1 < score2) # Output: True
    
    city1 = "Boston"
    city2 = "New York"
    print(city1 < city2) # Output: True (B comes before N)

Greater Than or Equal To (>=)

This operator checks if the value on the left is greater than or equal to the value on the right. It returns True if the condition is met, and False otherwise.

  • Example:

    age1 = 18
    age2 = 18
    print(age1 >= age2) # Output: True
    
    price1 = 50.0
    price2 = 55.0
    print(price1 >= price2) # Output: False

Less Than or Equal To (<=)

This operator checks if the value on the left is less than or equal to the value on the right. It returns True if the condition is met, and False otherwise.

  • Example:

    items_in_cart = 3
    max_items = 5
    print(items_in_cart <= max_items) # Output: True
    
    temperature = 0
    freezing_point = 0
    print(temperature <= freezing_point) # Output: True

Summary of Python Comparison Operators

For a quick overview, here is a table summarizing the operators discussed:

Operator Description Example Result
== Equal to 10 == 10 True
!= Not equal to 5 != 7 True
> Greater than 25 > 20 True
< Less than 85 < 90 True
>= Greater than or equal to 18 >= 18 True
<= Less than or equal to 3 <= 5 True

Using these comparison operators is fundamental to writing conditional logic, filtering data, and controlling the flow of your Python programs.