Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Difference between / vs. // operator in Python
Next article icon

Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).
Be cautious when comparing floating-point numbers due to potential precision issues. Consider using a small tolerance value for comparisons instead of strict equality.

Now let's see the comparison operators in Python one by one.

OperatorDescriptionSyntax
>Greater than:True if the left operand is greater than the rightx > y
<Less than:True if the left operand is less than the rightx < y
==Equal to:True if both operands are equalx == y
!=Not equal to: True if operands are not equalx != y
>=Greater than or equal to: True if the left operand is greater than or equal to the rightx >= y
<=Less than or equal to: True if the left operand is less than or equal to the rightx <= y

Python Equality Operators a == b

The Equal to Operator is also known as the Equality Operator in Python, as it is used to check for equality. It returns True if both the operands are equal i.e. if both the left and the right operands are equal to each other. Otherwise, it returns False.

Example:In this code, we have three variables'a', 'b' and 'c' and assigned them with some integer value. Then we have used equal to operator to check if the variables are equal to each other.

Python
a=9b=5c=9# Outputprint(a==b)print(a==c)

Output:

False
True

Inequality Operators a != b

The Not Equal To Operator returns True if both the operands are not equal and returns False if both the operands are equal.

Example:In this code, we have three variables'a', 'b' and 'c' and assigned them with some integer value. Then we have used equal to operator to check if the variables are equal to each other.

Python
a=9b=5c=9# Outputprint(a!=b)print(a!=c)

Output:

True
False

Greater than Sign a > b

The Greater Than Operator returns True if the left operand is greater than the right operand otherwise returns False.

Example:In this code, we have two variables'a' and 'b' and assigned them with some integer value. Then we have used greater than operator to check if a variable is greater than the other.

Python
a=9b=5# Outputprint(a>b)print(b>a)

Output:

True
False

Less than Sign a < b

The Less Than Operator returns True if the left operand is less than the right operand otherwise it returns False.

Example:In this code, we have two variables'a' and 'b' and assigned them with some integer value. Then we have used less than operator to check if a variable is less than the other.

Python
a=9b=5# Outputprint(a<b)print(b<a)

Output:

False
True

Greater than or Equal to Sign x >= y

The Greater Than or Equal To Operator returns True if the left operand is greater than or equal to the right operand, else it will return False.

Example:In this code, we have three variables'a', 'b'and 'c' and assigned them with some integer value. Then we have used greater than or equal to operator to check if a variable is greater than or equal to the other.

Python
a=9b=5c=9# Outputprint(a>=b)print(a>=c)print(b>=a)

Output:

True
True
False

Less than or Equal to Signx <= y

The Less Than or Equal To Operator returns True if the left operand is less than or equal to the right operand.

Example:In this code, we have three variables'a', 'b'and 'c' and assigned them with some integer value. Then we have used less than or equal to operator to check if a variable is less than or equal to the other.

Python
a=9b=5c=9# Outputprint(a<=b)print(a<=c)print(b<=a)

Output:

False
True
True

Chaining Comparison Operators

In Python, we can use thechaining comparison operators to check multiple conditions in a single expression. One simple way of solving multiple conditions is by using Logical Operators. But in chaining comparison operators method, we can achieve this without any other operator.

Syntax:a op1 b op2 c

Example:In this code we have a variable'a'which is assigned some integer value. We have used the chaining comparison operators method to compare the the value of 'a' with multiple conditions in a single expression.

Python
a=5# chaining comparison operatorsprint(1<a<10)print(10>a<=9)print(5!=a>4)print(a<10<a*10==50)

Output:

True
True
False
True

Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp