Python comparison operators examples

Scarlett 108 Published: 11/25/2024

Python comparison operators examples

I'd be happy to help! Here are some examples of Python comparison operators:

Equality Operators

Python has several equality operators that can be used to compare the values of two expressions or variables. These operators include:

== (Equal To): This operator checks if two expressions or variables have the same value.
a = 5

b = 5

print(a == b) # Output: True

c = "Hello"

d = "hello"

print(c == d) # Output: False

!= (Not Equal To): This operator checks if two expressions or variables do not have the same value.
a = 5

b = 10

print(a != b) # Output: True

c = "Hello"

d = "hello"

print(c != d) # Output: True

Relational Operators

Python has several relational operators that can be used to compare the values of two expressions or variables. These operators include:

> (Greater Than): This operator checks if the value of one expression or variable is greater than the value of another.
a = 10

b = 5

print(a > b) # Output: True

c = "apple"

d = "banana"

print(c > d) # Output: False (Note that this operator only works with numeric types, not strings)

< (Less Than): This operator checks if the value of one expression or variable is less than the value of another.
a = 5

b = 10

print(a < b) # Output: True

c = "apple"

d = "banana"

print(c < d) # Output: False (Note that this operator only works with numeric types, not strings)

>= (Greater Than or Equal To): This operator checks if the value of one expression or variable is greater than or equal to the value of another.
a = 10

b = 5

print(a >= b) # Output: True

c = "apple"

d = "banana"

print(c >= d) # Output: False (Note that this operator only works with numeric types, not strings)

<= (Less Than or Equal To): This operator checks if the value of one expression or variable is less than or equal to the value of another.
a = 5

b = 10

print(a <= b) # Output: True

c = "apple"

d = "banana"

print(c <= d) # Output: False (Note that this operator only works with numeric types, not strings)

Identity Operators

Python has several identity operators that can be used to check if two expressions or variables have the same value and data type. These operators include:

is (Is): This operator checks if two expressions or variables have the same value and data type.
a = 5

b = a

print(a is b) # Output: True

c = "Hello"

d = c

print(c is d) # Output: True

is not (Is Not): This operator checks if two expressions or variables do not have the same value and data type.
a = 5

b = 10

print(a is not b) # Output: True

c = "Hello"

d = 123

print(c is not d) # Output: True

Membership Operators

Python has several membership operators that can be used to check if a value exists in a sequence such as a list, tuple or dictionary. These operators include:

in (In): This operator checks if a value exists in a sequence.
fruits = ["apple", "banana", "cherry"]

print("apple" in fruits) # Output: True

people = {"John", "Jane"}

print("John" in people) # Output: True

not in (Not In): This operator checks if a value does not exist in a sequence.
fruits = ["apple", "banana", "cherry"]

print("grape" not in fruits) # Output: True

people = {"John", "Jane"}

print("Bob" not in people) # Output: True

Note that membership operators are case-sensitive for strings, and they can be used with any type of sequence (including lists, tuples, dictionaries, sets, etc.).

I hope these examples help you understand how to use Python comparison operators!

Comparison operators in Python with example

I'd be happy to help!

In Python, comparison operators are used to compare values and determine whether they are equal, greater than, less than, or fall within a specific range. These operators can be used in various contexts, such as conditional statements (if-else) and logical expressions.

Here's a list of Python's comparison operators along with examples:

Equal: ==

Example:

x = 5

y = 5

print(x == y) # Output: True

Not Equal: != or <>

Example:

x = 5

y = 6

print(x != y) # Output: True

Greater Than: >

Example:

x = 10

y = 8

print(x > y) # Output: True

Less Than: <

Example:

x = 5

y = 7

print(x < y) # Output: True

Greater Than or Equal: >=

Example:

x = 10

y = 9

print(x >= y) # Output: True

Less Than or Equal: <=

Example:

x = 8

y = 8

print(x <= y) # Output: True

Is: is (checks for identity, i.e., whether two variables refer to the same object)

Example:

a = [1, 2, 3]

b = a

print(a is b) # Output: True

Is Not: is not (negation of the "is" operator)

Example:

a = [1, 2, 3]

b = "hello"

print(a is not b) # Output: True

These comparison operators can be used in various contexts, such as:

Conditional statements (if-else): if x > 5: print("x is greater than 5") Logical expressions: (x > 5) and (y < 3) Loops: while x <= 10: print(x); x += 1

It's essential to note that Python performs comparisons in a case-sensitive manner, which means that uppercase letters are considered distinct from lowercase letters. If you're working with strings and want to perform case-insensitive comparisons, you can use the casefold() method or convert both strings to lowercase/uppercase before comparing them.

I hope this explanation helps! Let me know if you have any further questions.