What is comparison operator in python with example?
These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators. Show
Assume variable a holds 10 and variable b holds 20, then −
ExampleAssume variable a holds 10 and variable b holds 20, then − #!/usr/bin/python a = 21 b = 10 c = 0 if ( a == b ): print "Line 1 - a is equal to b" else: print "Line 1 - a is not equal to b" if ( a != b ): print "Line 2 - a is not equal to b" else: print "Line 2 - a is equal to b" if ( a <> b ): print "Line 3 - a is not equal to b" else: print "Line 3 - a is equal to b" if ( a < b ): print "Line 4 - a is less than b" else: print "Line 4 - a is not less than b" if ( a > b ): print "Line 5 - a is greater than b" else: print "Line 5 - a is not greater than b" a = 5; b = 20; if ( a <= b ): print "Line 6 - a is either less than or equal to b" else: print "Line 6 - a is neither less than nor equal to b" if ( b >= a ): print "Line 7 - b is either greater than or equal to b" else: print "Line 7 - b is neither greater than nor equal to b" When you execute the above program it produces the following result − Line 1 - a is not equal to b Line 2 - a is not equal to b Line 3 - a is not equal to b Line 4 - a is not less than b Line 5 - a is greater than b Line 6 - a is either less than or equal to b Line 7 - b is either greater than or equal to b python_basic_operators.htm Summary: in this tutorial, you’ll learn about Python comparison operators and how to use them to compare two values. Introduction to Python comparison operatorsIn programming, you often want to compare a value with another value. To do that, you use comparison operators. Python has six comparison operators, which are as follows:
These comparison operators compare two values and return a boolean value, either And you can use these comparison operators to compare both numbers and strings. Less than operator (<)The Less Than operator (<) compares two values and returns
The following example uses the Less Than (
It’s quite obvious when you use the less-than operator with the numbers. The
following example uses the less than operator (
The expression Similarly, the The following example shows how to use the less-than operator with variables:
Less than or equal to operator (<=)The less than or equal to operator compares two values and returns
The following example shows how to use the less than or equal to operator to compare two numbers:
And this example shows how to use the less than or equal to operator to compare the values of two variables:
Greater than operator (>)The greater than operator (
This example uses the greater than operator (
And the following example uses the greater than operator (
Greater Than or Equal To operator (>=)The greater than or equal to operator (
The following example uses the greater than or equal to operator to compare two numbers:
And the following example uses the greater than or equal to operator to compare two strings:
Equal To operator (==)The equal to operator (
The following example uses the equal to operator (
And the following example uses the equal to operator (
Not Equal To operator (!=)The not equal to operator (
For example, the following uses the not equal to operator to compare two numbers:
And the following example uses the not equal to operator to compare two strings:
Summary
Did you find this tutorial helpful ? What is comparison operator with example?If either of the operands is a string value, then a string comparison is used. Enclose strings in quotes ("string"). Caution! The == operator is a comparison operator.
...
. What is Python comparison operation?Comparison operators can compare numbers or strings and perform evaluations. Expressions that use comparison operators do not return a number value as do arithmetic expressions. Comparison expressions return either 1 , which represents true, or 0 , which represents false.
What are all the comparison operators in Python?Summary. A comparison operator compares two values and returns a boolean value, either True or False . Python has six comparison operators: less than ( < ), less than or equal to ( <= ), greater than ( > ), greater than or equal to ( >= ), equal to ( == ), and not equal to ( != ).
Which operator is known as comparison operator in Python?Relational Operators in Python. |