Posted on

Boolean operators in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Welcome back people, in this video we’re going to be talking about Boolean operators. So, what is a Boolean? Well, basically is not just a Python specific concept it’s a programming concept in general and it consists of two items, one is true, one is false. Now in a lot of programming languages you typed true as “true” and false as, well you know how to spell those, but you’d spell them all lowercase, Python’s different. So let’s explore that a bit and open up terminal here, and launch a Python3 console, and we’re going to just type in true and it’s going to return true, this is not in quotations, make sure when you’re typing true and false for truth checking that you’re typing true and false and not like this, because that will return a string which is true, yeah. So let’s talk about what these operators do. Basically, and we’re going to get into comparison operators a bit later which is like checking if something is equal to something, or more or less than, or if it even exists, or if it’s a certain type, and so you can basically perform different actions on different types of data and that’s why it’s necessary to do some fact checking with that. So, we need the true and false in programming, however we can explicitly declare true-and-false like we’ve just done by just typing true or false, or we can generate it in other ways. So let’s have a look at some of the ways we can generate a true or false. So let’s type “5 = 5” that’s true. Now when you’re comparing two objects and you’re checking if they’re equal to each other you can’t type that because the single quote, the single equal sign there, is used for a different purpose and programming it’s used to assign a value to a variable, and we’ll go over that here soon as well. So when we’re comparing to check if two numbers or two strings are equal to each other we need to put two equal signs, so you’ll see that returns true. So let’s go ahead and “5 == 4” that’s going to return false because of course we’ve all been through grade 2 math and we all know that 5 is a little larger than 4. Now you can also do this with strings and also without using equal signs at all, you can actually type “5 is 5” and that will return true, and you can type “5 is not 5” and that will equal false. So basically you’re saying 5 is not 5 and Python is saying well that’s false. So you could type “5 is not 6” and that will return true. Again you can do this with strings as well so you can type “This” is “This” and this is going to compare both of these strings to see if they’re equal to each other and it’s going to return true because yes, it is. Now we can also do this “True is True” will return true because of course true is true. Now that last one is a bit of a you know obvious result, so let’s go ahead and compare this with this “True” is True. If you think this will return true because we’re comparing true and true the results will not be as obvious as you expect because one of them is a string and the other one is a Boolean operator. So, not only when you compare two items is it checking if the value is equal, it’s also checking the type of data that it is. So, if we wanted to make the above statement return true we would have to convert true into a string. So just like you would convert a number into a string, you can just wrap the Boolean operator in the str function and it will convert it to a string prior to the comparison, and then it will return the result of true is true which equals true. Now, true-and-false again they’re going to come up in in the if-else statements because that’s where we’re really going to do some error checking and you know make sure things are set and that you know they’re not just not equal to each other, and you’re going to see more practical uses of Boolean operators later on, but this was just to show you guys what true and false is and again you should already know this without a programming background. So in the next video we’re going to be looking at Pythons version of arrays which are called lists.