The Python Comparison Operators | Python for Absolute Beginners

preview_player
Показать описание
In this video, we are going to talk about what are called comparison operators. There are a few subcategories that fall under this, but we won't cover all of them in this lesson. What we will be talking about specifically for this one are the value comparison operators. These ones allow you to compare the ordering of values, whether one is greater than or less than the other. They also allow you to check whether values are equal or not.

Here are the value comparison operators in Python:

< Less than

We have the Less than operator, the greater than operator, the less than or equal operator, the greater than or equal operator. And then we have the equals operator, also referred to as the equality operator. Sometimes you'll also hear this called the equals equals or the double equals operator. It's used to check if two values are equal. Be careful with this one, because in Python as well as many other languages, the single equals sign is the assignment operator, which is used to assign values to variables instead. It's actually quite a common mistake to get the single equals and the double equals mixed up. And then finally we have the not equals operator, which is also called the inequality operator. It checks if two values are different from each other. This one uses an exclamation point followed by an equals sign.

Comparison operations are also binary operations, they make use of 2 values, you place one value to the left of the operator, and the other one to the right of the operator. These comparison expressions will evaluate into boolean values. Recall from our lesson on data types that a boolean value can either be True or False. So for example, if you have the comparison expression 2 is greater than 1, this will check if the value on the left side of the operator is greater than the value on the right side. And 2 is in fact greater than 1, so this will give us True. If the expression said 2 is less than 1, then this will give us False instead.

We can also compare strings. The equality operator, when used on strings, will check if both string values have the exact same characters, in the exact same order. These string comparisons are also case-sensitive.

You can also chain comparison expressions together. When chaining comparisons together, everything has to be True in order to get a final result of True.

You can also combine arithmetic expressions and comparison expressions in one larger expression. Arithmetic operators have a higher precedence compared to comparison operators, so their operations will be performed first, before the values are compared.

#python #learntocode #pythonforabsolutebeginners
Рекомендации по теме
Комментарии
Автор

Thank you a lot for these short videos. They are not giving me any anxiety while I am learning as absolute beginner. In courses where I am at right now..just can't keep up. Your added details when you light up, darken down something (and all other small things)- absolutely Thank you @choobtorial!

agnesezogota
Автор

# camparison operator
print(8<6) #false
print(6<8) #true
#less than operator

print(8>6) #true
print(6>8) #false
#greater than operator

print(8<=6) #false
print(6<=8) #true
#less than equal to operator

print(8>=6) #true
print(6>=8) #false
#greater than equal to operator

print(8==6) #false
print(8==8) #true
#equals operator

print(8!=6) #true
print(8!=8) #false
#not equals operator

ayeshaansari