OKPEDIA PYTHON EN

Comparison Operators in Python

In Python, comparison operators are essential tools that allow us to evaluate and compare values. These operators return a Boolean value, either `True` or `False`, based on the result of the comparison. Below is a list of the primary comparison operators in Python:

equal to ( == ) a == b
not equal to ( != ) a != b
greater than ( > ) a>b
greater than or equal to ( >= ) a>=b
less than ( > ) a<b
less than or equal to ( <= ) a<=b

Examples

Example 1 ( Comparing Numbers for Equality )

To verify if the `year` variable is equivalent to 2000

if (year==2000):

The condition `year == 2000` will evaluate to `True` only if the `year` variable holds the value 2000.

Example 2 ( Comparing Strings for Equality )

When comparing a variable to a string, you can use either single (') or double (") quotation marks to enclose the string:

if (name=="Tom"):

This condition will be `True` only if the `name` variable contains the string "Tom".

Example 3 ( Checking for Greater Than or Equal Values )

To determine if the `year` variable is greater than or equal to 2000:

if (year>=2000):

This will be `True` if the `year` variable's value is either greater than or exactly 2000.

Example 4 ( Evaluating for Non-equality )

To see if the `year` variable does not hold the value 2000:

if (year!=2000):

This condition will be `True` only if the `year` variable's value is anything other than 2000.

https://how.okpedia.org/en/python/what-are-the-comparison-operators-in-python


Report us an error or send a suggestion to improve this page



FacebookTwitterLinkedinLinkedin