hello guys welcome once again in this blog I am going to show you how to use comparison operators and logical operators in Python in the last few blog's I have been showing you how to use if-else statement in Python and this is the continuation you may say but these comparison operators and logical operators you can not only use with these ifs and statement but in general also you can use them so first of all comparison operators comparison operators as the name suggests we use for comparing values now there are these comparison operators which are available in Python which are less than and which is indicated by this symbol second is less than or equal to which is which are implicated by less than sign and equal to and greater than greater than equal to double equal to indicates that you want to check the equality and this exclamation mark equal to means that you want to check the non Equality okay now there are some logical operator operators which by which you can perform some logical operations between some values for example you want to check you have two variables and you want to check for two conditions and you want to check both the condition and you want that both the conditions has to be true you use and condition and using and condition it means that you are checking for all the conditions in this case we are checking this condition has to be true and this condition has to be true otherwise it will say false in the opposite or the opposite of and is or not exactly opposite but the logically opposite of and is our and or means either one condition is true then it will give you true so let's see how we can use it so first of all up for example you want to check the less than condition
I can just copy this and in my shell I can run it and I can print I want to print if this is true print true okay and I will press ENTER two times and this true has to be enclosed in double quotes right press ENTER and it says true because 9 is less than 10 right in a similar way I can check these functionality but this time I am not going to use if condition but here you can use all these operators with if conditions okay so for checking less than or equal to we can just try as wide directly right in the shell and press ENTER and it gives true okay so this means less than or equal to so 9 is less than 10 so this condition is true so that's why it's giving true to us and always remember that you have to use less than first and followed by this equal to let's see what happens when you say 9 equal to less than 10 and gives syntax error so always use less than first and then equal to sign in order to avoid any problems in a similar way you can just check the greater than so if 9 is greater than 10 which is false so it will give us false right 9 is greater than equal to 10 which is also false and now we are checking the equality which I have shown you in the last blog which is false 9 is not equal to 10 and let's check if 9 is not equal to then this exclamation mark and equal to checks for the non Equality so it will be true yes and if 9 is left then we have already checked it okay now we have end the condition right so for example I want to check whether 9 is less than 10 and 9 is not equal to 10 so I want to check whether these two conditions are two at the same time okay and I press ENTER and it gives me true and let's let's check for some other condition so this condition is true in this case which is 9 is less than 10 and this is also true because 9 is not equal to 10 so it gives true for example I delete this not equal to n right equal to here so one condition is false here which is 9 is not equal to 10 because we are checking for two condition to be true at the same time it will give me false because this condition is true but this is false right so whenever you use an operator your program expects that both the conditions or whatever conditions you are giving are true so you can even give three conditions for example I want to check the non equality and so it's not necessary that you only can only give two conditions you can even give 3 4 5 or whatever conditions okay and it's totally fine okay in a similar way you can use all to check whether either of the condition is true if this condition is true or this condition is true in this case both the conditions are true so it gives true but when we give this a statement where one condition is true which is nine is less than trend which is true but nine equal to equal to or equal to 10 which is not true it will still give you I have I must have used or here right because we are checking for or so it gives us true right so in this way you can use comparison operators and logical operators there is some more things you can check and these are is an inn conditions or is and in operators whatever you want to say so for example I want to check I have a string string equals to equal to what you can say a b c d e okay so our string equal to a b c d e and i want to check if a is present in string 1 also a in string and gives us true so what this in operator does is it checks whether this variable contains this value or character or not in in such way right so in a similar way you can search for B or C or any conditions okay so for example you want to check where the a B is present it gives us true but whenever you give a C it will give you false because a and C are not together present here now suppose you have to list one list is called a is equal to 1 from to form a tree and be equal to same list okay be and there is operator called if so whenever you use a is B it will give you false but for example you would declare a list C equal to D equal to 1 2 3 will give you true because it checks whether these 2 or 2 2 values are equal exactly equal or not these are these a and B are equal Y values but they are not logical equal that's why it will give you false but this we have already you know assigned or assigned C equal to D or we have assigned D to C equals this value that's why it will give us a true so let's check C is D and it will give us true okay so in this way you can use logical operators comparison operators and is in operator in Python I hope you've enjoyed this blog
Post a Comment