hey guys welcome to the next Blog on python turtle for beginners in this blog we will talk about the boolean values comparison operators and the logical operators in Python so first of all what is a boolean value so in Python boolean values are two constant objects which are true and false now for this demonstration I will be using a Python interpreter and in PyCharm you can open the python interpreter by going down at the bottom and you will be able to see this option which says python console so just click on this python console option which is going to open the python console and you can move this bar up and down in order to expand your console right so let me just clear this console so we will start from the top so in order to clear this console you can right click and then just say clear all and it's going to clear the console and let me resize this console up to the top and let me just minimize this section also so as I said boolean in Python are two constants objects which are true and false so how you write true and false in Python you write true with a capital T and press Enter this is a true value and false you write with a capital false this is a false value now if you write true with small T this is not recognized by Python also when you write false with small F this is also not recognized by Python now generally we use boolean values in order to find out the result of some condition now let's say you have two numbers and you want to find out which number is greater out of these two numbers you can use a comparison an operator to compare these two numbers and the result will be shown as a boolean value now the next question arises what is a comparison operator so in Python these are some of the comparison operators you can use so you can use this double equals to check the equality between two values you have this not equal operator with this exclamation mark and equals which you can use to check the non equality between two values there is a greater than operator and there is a less than operator and also there is a greater than equals operator and the less than equals operator now examples of each of these operators I have given here so how to use these operators as I said if you have two values let's say two integers X and Y to compare these two integers whether they are equal or not you write on the left hand side post variable and on the left hand side second variable and in between you use the comparison operator it can be equal to operator or non equality operator or greater than or less than or greater than equals or less than equals operator so let's see how we can use these operators in real Python environment so let's say I want to check where the 10 is greater than 9 or not and when I press ENTER it's going to give me true because 10 is greater than 9 now once again let's check if then is less than 9 or not and it will give me a false value which is also a boolean result also we can check equality between two values so we can just write 100 is equal to 100 or not and it will give us true once again we can check if 100 is equal to 99 that will give us false in a same way you can also check for the non Equality for non equality you use this exclamation mark equals and then press ENTER it will say true because 100 is not equal to 99 the same operation you can also perform with some variable so let's say I have a variable called X whose value is 20 and I have a variable called Y and I assign 30 to it and let's say I want to check whether the value in the X is greater than or equal to the value which is there in Y then I can write this kind of a statement and when I press Enter it's going to give me false because 30 is not greater than or equal to X so let's assign a value which is 30 into our variable X and now let's check whether X and now let's check whether X is less than or equal to Y or not and when I press ENTER it's going to give me true because we know that X which is 30 is not less than Y but it's equal to Y and this condition is true now you can also compare two strings using these operators so let's say I have a string called hello and I want to check whether the hello value is equal to some other value for example hello in double quotes or not and it will return me true that means whether you write hello in single quote or hello in double quote both these values are same in Python now also we have seen in the last
Blog where we have seen how to use strings that whenever for example I write a string I can use this dot operator in order to call some functions related to strings so some of these functions returns a boolean value so for example we can check whether they hello all letters are in uppercase or not and once I press ENTER it says false or I can check whether in this hello all the letters are in lowercase or not this will give me true or I can test whether in this Hello string all letters are alphabets or not this is going to give me true because all letters are alphabets now if I check whether in the string whether there is alpha numeric value or not I can use this method which says is all numb and this will check whether in my string there is an alphanumeric value or not now the next question you may ask is for example I want to evaluate two conditions at the same time let's say I want to evaluate whether 10 is greater than 9 and I want to evaluate whether 20 is less than 15 or not so how can I evaluate these two conditions at the same time so for those type of comparison we use that logical operators and there are three logical operators which we can use in Python an end operator an or operator and are not operator so this and operator you can use to evaluate two conditions and it will return true only if both conditions are true okay so let's say you have two conditions X condition and y condition then if you use this and operator then in order to get true out of these two conditions both conditions have to be true in the case of or it will return true if one of these conditions is true so whether X is true or Y is true it doesn't matter but if only one condition is true this will give us true value and the not operator will return the opposite of what we have so if some condition returns us true then when we use not operator then not operator will make this false so let's try all these three operators in Python so I said I can use an end operator to check whether this value is true as well as this value is true or not so when I press ENTER it returns us false because even though the first condition is true which is 10 is greater than 9 but the second condition is not true right so in case of and both condition should be true in order to get the true result once again when I use the same conditions and instead of end I use the or operator it will return me true because one of these conditions is true which is 10 is greater than 9 even though the other condition is false now let's say we make both conditions true and we use our and when we press ENTER it's going to still returns us true so the minimum requirement of or is there should be at least one condition which evaluates to true now let's see how we can use the not operator so I can write not and then I can check where the 10 is greater than 9 or not and when I press ENTER it returns us false because we know that 10 is greater than 9 and not makes it false so it returns us false once again if we check whether 10 is less than 9 or not it's going to return those true because 10 is not less than 9 and this condition will return false and not will make it opposite which is true so in this way you can use boolean values comparison operators and logical operators in Python I hope you have enjoyed this Blog I will see you in the next blog
Post a Comment