Saturday, May 16, 2015

Python Comparison Operators - Python Numbers - Python Tutorial

Python Comparison OperatorsPython Comparison Operators


In the previous Python tutorial, we looked at booleans now we can use some Python comparison operators to get a boolean returned to us.  Python comparison operators are a vital part of programming with Python. We use the comparison operators to compare objects and tell the program to do something when a certain return happens.  We can compare if an object is greater than, less than, equal to, not equal to, greater than and equal to or less than and equal to.


Single Comparison Operators


== Equal Or Not Equal


The == symbols will check if the left side is equal to the right if so Python will return True if not then we get False. Do not make the mistake of using a single equal symbol like =, you will get an error or assign a variable.


#== Equal Or Not Equal

>>> 6 == 6
True
>>> 6 == 7
False

!= Not Equal or Equal


The != symbols will check if the left side is not equal to the right side if so Python will return True but if the left is equal to right then we will get False. This is opposite of the == operator.


#!= Not Equal or Equal

>>> 6 != 8
True
>>> 5 != 5
False

> Left is Greater Than Right


The > symbol will check if the left side is greater than the right.  If the left is greater Python will return True and if left is less Python will return False.


 Left is Greater Than Right" ># > Left is Greater Than Right

>>> 6 > 5
True
>>> 6 > 8
False

< Right is Greater Than Right 


The < symbol will check if the right side is greater than the left.  If the right if greater Python will return True and if the right is less Python will return false.


# < Right is Greater Than Right 

>>> 5 < 6
True
>>> 6 < 5
False

>= Left is Greater Than or Equal to Right


The >= symbols check if the left side is either greater or equal to the right side.  If the left is greater or equal to right then Python returns True if left side is less than right then Python returns False.


= Left is Greater Than or Equal to Right" ># >= Left is Greater Than or Equal to Right

>>> 7 >= 6
True
>>> 6 >= 6
True
>>> 5 >= 6
False

<= Right is Greater Than or Equal To Left


The <= right is greater than or equal to the left. If the right is greater or equal to the left then Python will return True if right side is less than Python will return False.


# <= Right is Greater or Equal To Left

>>> 6 <= 7
True
>>> 6 <= 6
True
>>> 6 <= 5
False

Chained Comparisons


Now that we have learned to use Comparison Operators in Python we can actually compare larger amounts of data using something called chained comparisons.  We will show you a bunch of examples below but they are pretty straight forward if you understand Comparision Operators so we will not explain each one.


#Chained Comparison Operators

>>> 4 < 6 < 8
True
>>> 7 > 4 == 4
True
>>> 9 != 7 != 6
True
>>> 10 > 8 > 6 > 4 > 2 > 0
True

>>> 5 > 2 and 7 < 10
True
>>> 6 == 6 or 7 != 5
True

Conclusion


Comparison operators are very simple to use and are effective in programming.  You will become very familiar to comparison operators as you move on in your Python programming career.  If you have any questions about Python comparison operators let us know via comment below.



No comments:

Post a Comment