[no audio]
Friends, in our previous video we have seen about arithmetic
and assignment operators of Python.
Now, we are going to discuss about the next operator,
that is comparison or comparison operators of Python. Guys,
these operators are very, very important in your real-time,
comparison operators.
And one more point, we already discussed that while giving
introduction to operators, that is, comparison operators
we take input as value, right, and then it will give output
as a Boolean value, 'True' or 'False'.
Right. See actually comparison is nothing but finding relation
between two operands or two values.
I mean, let's say you have 3 and 4, numbers. Now I am going
to check '3 > 4', no, 'False', no. We know that 3 is
not greater than 4 that's why this is a comparison operator
here. And if you compare these two with the help of this
'>' symbol, right, you are going to get result as
'False'. So we are providing input as a value, number, number
system value, right, as of now, and output you're going to get
either 'True' or 'False', right?
Let me open my Python terminal so I can explain very easily from here.
As of now we are not going to write any Python program or
script. Let me take '3 > 4', no. We know that. 'False'.
See guys, while taking your comparison operators you can
take a space of between your operands.
[no audio]
Now, let me take '3 < 4'.
Yes, we know it is 'True'
exactly. Even if you do with Python, you're getting 'True',
right? So comparison operators are used to find the relation
between two values or two operands.
So guys, I can also store my value into some variable, and
now I am checking '3 < a', so what is 'a'? Value 4.
So Python is going to compare like '3 < 4'. See internally
'a' value is nothing but 4, so '3 < 4'.
Yes, it's 'True'. Right.
Let me check it, yes.
So likewise you have different types of operators here, > ,
<, ==, '==' means equality,
!=, >=, <=,
right. See directly you can check suppose 4 <= 4
See actually the meaning for this is, it may be less than
or it may be equal.
If anyone of the that is 'True', then you will get output as 'True'.
I mean 4 maybe equal to 4 or maybe less than that.
So as of now in this relation 4 equals to 4, but you are
checking either less than or equal.
So equal is correct, that's why we're getting 'True',
or instead of that
I'm taking 3 <= 4. Yes,
anyway 3 is less than 4, 'True'. Maybe anyone of that, either
less than or equal.
Right. So guys one one more thing using this comparison or relationship
operators with the number systems is very easy, because we
are very much familiar with the number systems from our mathematics.
Directly we can see by seeing your numbers, two numbers, two
values, you can tell the relationship between those two
numbers either equal, or not equal, greater than, less than,
less than or equal to, greater than or equal to, right but
when you go with the strings, actually strings is also important
while discussing about your comparison operators.
I mean how to compare strings.
Let's say I have a string called "a". I want to check
this, either 'a'=='a'.
Yes, it's 'True'.
'a'=='b', no. These two things directly we can tell
that, or 'a' != 'b'.
Yes, it's 'True'.
They are not equal.
We are checking they are not equal. Yes, 'True'.
So equal and not equal we can easily understand because by
seeing strings itself we can match and we can tell that whether
they are equal or not equal.
But what about this?
I am telling 'a' < 'b'.
Yes, it's true actually. But if I run this, yes, you are going
to get output as 'True'.
How your Python is going to compare your strings?
What is the mechanism while comparing your strings?
Nothing is there, guys. Whenever you are going to compare your strings
your Python will convert each and every character into an
ASCII code, based on ASCII code it is going to compare. Let me
find what is the ASCII code for your 'a'. See to find for any
character the ASCII code, the default function is 'ord'.
'ord()', your character.
Not only 'a', you can take any special character
also, you are going to get some ASCII code. No need to remember
that, but at least just try to remember the function. See
if I know ASCII code then how can I find
character. Let me write it, chr(45). See that. You can find
your character ASCII code using 'ord' function.
You can find the particular character from your ASCII code
using 'chr' function.
[no audio]
That's fine. Now let me compare 'aa', is it less than 'b'.
Yes, obviously it is true.
The reason is, whenever if you have multiple characters,
right, the first character from your left side string will
be compared with the first character from your right side
string. So 'a' < 'b', we know that. That's why it is 'True'.
Let me take something like 'abc', and I'm going to take 'z'.
Otherwise I will take 'aa',
or 'aaa'. Let's say 'c'. Now,
the result is 'False'.
The reason is, first thing, first character from your left
side and first character from your right side of your operator
is 'aa', both are equal. That's right.
We'll skip that, and we'll try to compare second characters.
So 'b' is greater than 'a', right. But you are checking less
than, that's why this is 'False'.
[no audio]
Right. So it will compare character by character in a given
string. Right?
Likewise, suppose if I do '>' symbol, then it's 'True'.
[no audio]
Right. So simply guys your Python will compare your strings based
on an ASCII code.
So how to get your ASCII code is nothing but order of your
particular character. How to get your character for your particular
ASCII code, 'chr' function, right?
So guys these comparison operators are very, very helpful while
discussing conditional statements like if and if-else, else-
if, nested ifs, right. There, we will use effectively this comparison
operators. As of now what you have to remember is comparison
or relationship operators will take input as either number
or a string and it will provide output as always, always output
will be either 'True' or 'False', because you are comparing, you
are checking the relation between two values.
That relation is like, are they equal, are they not equal, if anyone
of that is greater than, or less than, less than or equal to,
greater than or equal to.
So you are checking relationality between two values,
so that is nothing but the output is always either 'True' or
'False', or simply Boolean output.
Right. Okay.
Okay guys, thank you for watching this video.
[no audio]