1 00:00:00,000 --> 00:00:01,800 [no audio] 2 00:00:01,800 --> 00:00:05,500 Friends, in our previous video we have seen about arithmetic 3 00:00:05,500 --> 00:00:07,600 and assignment operators of Python. 4 00:00:07,900 --> 00:00:11,700 Now, we are going to discuss about the next operator, 5 00:00:12,000 --> 00:00:17,200 that is comparison or comparison operators of Python. Guys, 6 00:00:17,600 --> 00:00:20,500 these operators are very, very important in your real-time, 7 00:00:20,800 --> 00:00:22,400 comparison operators. 8 00:00:23,500 --> 00:00:26,500 And one more point, we already discussed that while giving 9 00:00:26,500 --> 00:00:31,100 introduction to operators, that is, comparison operators 10 00:00:31,300 --> 00:00:38,000 we take input as value, right, and then it will give output 11 00:00:38,000 --> 00:00:41,000 as a Boolean value, 'True' or 'False'. 12 00:00:41,800 --> 00:00:46,500 Right. See actually comparison is nothing but finding relation 13 00:00:46,500 --> 00:00:50,000 between two operands or two values. 14 00:00:50,500 --> 00:00:55,600 I mean, let's say you have 3 and 4, numbers. Now I am going 15 00:00:55,600 --> 00:00:59,300 to check '3 > 4', no, 'False', no. We know that 3 is 16 00:00:59,300 --> 00:01:02,900 not greater than 4 that's why this is a comparison operator 17 00:01:02,900 --> 00:01:05,900 here. And if you compare these two with the help of this 18 00:01:05,900 --> 00:01:09,100 '>' symbol, right, you are going to get result as 19 00:01:09,100 --> 00:01:12,700 'False'. So we are providing input as a value, number, number 20 00:01:12,700 --> 00:01:16,400 system value, right, as of now, and output you're going to get 21 00:01:16,400 --> 00:01:18,500 either 'True' or 'False', right? 22 00:01:18,500 --> 00:01:23,100 Let me open my Python terminal so I can explain very easily from here. 23 00:01:23,200 --> 00:01:25,700 As of now we are not going to write any Python program or 24 00:01:25,700 --> 00:01:30,200 script. Let me take '3 > 4', no. We know that. 'False'. 25 00:01:30,900 --> 00:01:34,000 See guys, while taking your comparison operators you can 26 00:01:34,000 --> 00:01:37,900 take a space of between your operands. 27 00:01:37,900 --> 00:01:40,000 [no audio] 28 00:01:40,000 --> 00:01:41,700 Now, let me take '3 < 4'. 29 00:01:42,000 --> 00:01:43,400 Yes, we know it is 'True' 30 00:01:43,400 --> 00:01:45,800 exactly. Even if you do with Python, you're getting 'True', 31 00:01:46,800 --> 00:01:50,900 right? So comparison operators are used to find the relation 32 00:01:50,900 --> 00:01:53,300 between two values or two operands. 33 00:01:54,600 --> 00:01:59,200 So guys, I can also store my value into some variable, and 34 00:01:59,200 --> 00:02:04,100 now I am checking '3 < a', so what is 'a'? Value 4. 35 00:02:04,200 --> 00:02:08,900 So Python is going to compare like '3 < 4'. See internally 36 00:02:08,900 --> 00:02:12,000 'a' value is nothing but 4, so '3 < 4'. 37 00:02:12,199 --> 00:02:13,300 Yes, it's 'True'. Right. 38 00:02:14,100 --> 00:02:15,500 Let me check it, yes. 39 00:02:16,100 --> 00:02:20,000 So likewise you have different types of operators here, > , 40 00:02:20,000 --> 00:02:24,200 <, ==, '==' means equality, 41 00:02:24,600 --> 00:02:27,300 !=, >=, <=, 42 00:02:28,500 --> 00:02:34,600 right. See directly you can check suppose 4 <= 4 43 00:02:36,200 --> 00:02:39,700 See actually the meaning for this is, it may be less than 44 00:02:39,700 --> 00:02:40,900 or it may be equal. 45 00:02:41,100 --> 00:02:44,600 If anyone of the that is 'True', then you will get output as 'True'. 46 00:02:44,700 --> 00:02:50,500 I mean 4 maybe equal to 4 or maybe less than that. 47 00:02:50,600 --> 00:02:54,200 So as of now in this relation 4 equals to 4, but you are 48 00:02:54,200 --> 00:02:56,200 checking either less than or equal. 49 00:02:56,200 --> 00:02:58,000 So equal is correct, that's why we're getting 'True', 50 00:02:59,400 --> 00:03:00,400 or instead of that 51 00:03:00,400 --> 00:03:03,300 I'm taking 3 <= 4. Yes, 52 00:03:03,300 --> 00:03:07,300 anyway 3 is less than 4, 'True'. Maybe anyone of that, either 53 00:03:07,300 --> 00:03:08,400 less than or equal. 54 00:03:09,200 --> 00:03:14,600 Right. So guys one one more thing using this comparison or relationship 55 00:03:14,600 --> 00:03:17,700 operators with the number systems is very easy, because we 56 00:03:17,700 --> 00:03:20,800 are very much familiar with the number systems from our mathematics. 57 00:03:21,500 --> 00:03:25,100 Directly we can see by seeing your numbers, two numbers, two 58 00:03:25,100 --> 00:03:28,200 values, you can tell the relationship between those two 59 00:03:28,400 --> 00:03:31,300 numbers either equal, or not equal, greater than, less than, 60 00:03:31,300 --> 00:03:34,200 less than or equal to, greater than or equal to, right but 61 00:03:34,200 --> 00:03:39,400 when you go with the strings, actually strings is also important 62 00:03:39,400 --> 00:03:42,300 while discussing about your comparison operators. 63 00:03:43,300 --> 00:03:45,300 I mean how to compare strings. 64 00:03:45,900 --> 00:03:49,700 Let's say I have a string called "a". I want to check 65 00:03:49,700 --> 00:03:51,800 this, either 'a'=='a'. 66 00:03:52,500 --> 00:03:53,400 Yes, it's 'True'. 67 00:03:54,200 --> 00:03:57,900 'a'=='b', no. These two things directly we can tell 68 00:03:57,900 --> 00:04:01,500 that, or 'a' != 'b'. 69 00:04:00,400 --> 00:04:01,500 Yes, it's 'True'. 70 00:04:01,600 --> 00:04:02,600 They are not equal. 71 00:04:02,600 --> 00:04:06,000 We are checking they are not equal. Yes, 'True'. 72 00:04:07,000 --> 00:04:09,900 So equal and not equal we can easily understand because by 73 00:04:09,900 --> 00:04:13,000 seeing strings itself we can match and we can tell that whether 74 00:04:13,000 --> 00:04:14,200 they are equal or not equal. 75 00:04:14,600 --> 00:04:15,600 But what about this? 76 00:04:16,000 --> 00:04:18,500 I am telling 'a' < 'b'. 77 00:04:19,600 --> 00:04:22,399 Yes, it's true actually. But if I run this, yes, you are going 78 00:04:22,399 --> 00:04:23,600 to get output as 'True'. 79 00:04:24,100 --> 00:04:26,700 How your Python is going to compare your strings? 80 00:04:26,700 --> 00:04:29,800 What is the mechanism while comparing your strings? 81 00:04:30,400 --> 00:04:33,300 Nothing is there, guys. Whenever you are going to compare your strings 82 00:04:33,300 --> 00:04:36,800 your Python will convert each and every character into an 83 00:04:36,800 --> 00:04:41,200 ASCII code, based on ASCII code it is going to compare. Let me 84 00:04:41,200 --> 00:04:45,200 find what is the ASCII code for your 'a'. See to find for any 85 00:04:45,200 --> 00:04:49,300 character the ASCII code, the default function is 'ord'. 86 00:04:49,500 --> 00:04:50,800 'ord()', your character. 87 00:04:51,200 --> 00:04:53,400 Not only 'a', you can take any special character 88 00:04:53,400 --> 00:04:57,900 also, you are going to get some ASCII code. No need to remember 89 00:04:57,900 --> 00:05:02,800 that, but at least just try to remember the function. See 90 00:05:03,300 --> 00:05:06,100 if I know ASCII code then how can I find 91 00:05:07,800 --> 00:05:13,100 character. Let me write it, chr(45). See that. You can find 92 00:05:13,100 --> 00:05:15,400 your character ASCII code using 'ord' function. 93 00:05:15,900 --> 00:05:19,300 You can find the particular character from your ASCII code 94 00:05:19,300 --> 00:05:20,400 using 'chr' function. 95 00:05:20,400 --> 00:05:22,300 [no audio] 96 00:05:22,300 --> 00:05:28,000 That's fine. Now let me compare 'aa', is it less than 'b'. 97 00:05:28,000 --> 00:05:31,000 Yes, obviously it is true. 98 00:05:31,600 --> 00:05:35,100 The reason is, whenever if you have multiple characters, 99 00:05:36,400 --> 00:05:41,900 right, the first character from your left side string will 100 00:05:41,900 --> 00:05:46,000 be compared with the first character from your right side 101 00:05:46,000 --> 00:05:49,800 string. So 'a' < 'b', we know that. That's why it is 'True'. 102 00:05:50,300 --> 00:05:55,400 Let me take something like 'abc', and I'm going to take 'z'. 103 00:05:56,400 --> 00:05:58,100 Otherwise I will take 'aa', 104 00:05:59,100 --> 00:06:02,600 or 'aaa'. Let's say 'c'. Now, 105 00:06:02,600 --> 00:06:03,800 the result is 'False'. 106 00:06:03,900 --> 00:06:08,700 The reason is, first thing, first character from your left 107 00:06:08,700 --> 00:06:12,800 side and first character from your right side of your operator 108 00:06:12,800 --> 00:06:14,400 is 'aa', both are equal. That's right. 109 00:06:14,400 --> 00:06:18,200 We'll skip that, and we'll try to compare second characters. 110 00:06:18,800 --> 00:06:21,800 So 'b' is greater than 'a', right. But you are checking less 111 00:06:21,800 --> 00:06:23,500 than, that's why this is 'False'. 112 00:06:23,500 --> 00:06:25,600 [no audio] 113 00:06:25,600 --> 00:06:30,100 Right. So it will compare character by character in a given 114 00:06:30,100 --> 00:06:32,100 string. Right? 115 00:06:32,500 --> 00:06:36,700 Likewise, suppose if I do '>' symbol, then it's 'True'. 116 00:06:36,700 --> 00:06:38,500 [no audio] 117 00:06:38,500 --> 00:06:42,700 Right. So simply guys your Python will compare your strings based 118 00:06:42,700 --> 00:06:44,300 on an ASCII code. 119 00:06:44,500 --> 00:06:47,800 So how to get your ASCII code is nothing but order of your 120 00:06:47,800 --> 00:06:51,300 particular character. How to get your character for your particular 121 00:06:51,300 --> 00:06:55,000 ASCII code, 'chr' function, right? 122 00:06:55,300 --> 00:06:59,000 So guys these comparison operators are very, very helpful while 123 00:06:59,000 --> 00:07:04,300 discussing conditional statements like if and if-else, else- 124 00:07:04,300 --> 00:07:08,300 if, nested ifs, right. There, we will use effectively this comparison 125 00:07:08,300 --> 00:07:12,200 operators. As of now what you have to remember is comparison 126 00:07:12,200 --> 00:07:16,800 or relationship operators will take input as either number 127 00:07:16,900 --> 00:07:23,500 or a string and it will provide output as always, always output 128 00:07:23,500 --> 00:07:27,800 will be either 'True' or 'False', because you are comparing, you 129 00:07:27,800 --> 00:07:31,000 are checking the relation between two values. 130 00:07:31,700 --> 00:07:36,900 That relation is like, are they equal, are they not equal, if anyone 131 00:07:36,900 --> 00:07:39,800 of that is greater than, or less than, less than or equal to, 132 00:07:39,800 --> 00:07:40,900 greater than or equal to. 133 00:07:41,700 --> 00:07:44,500 So you are checking relationality between two values, 134 00:07:44,800 --> 00:07:47,200 so that is nothing but the output is always either 'True' or 135 00:07:47,200 --> 00:07:49,100 'False', or simply Boolean output. 136 00:07:50,400 --> 00:07:52,000 Right. Okay. 137 00:07:52,100 --> 00:07:54,000 Okay guys, thank you for watching this video. 138 00:07:54,000 --> 00:08:00,800 [no audio]