Python Part 14b - Working with Booleans Office

Python Part 14b - Working with Booleans Office Welcome to the second in the four part series of tutorials on working with different data types in python this part is going to look at the billion data type.

And here's what you'll learn during this tutorial you'll learn about what the billion data type is and how it was named after a mathematician named bool we'll look at.

Boolean operators such as greater than and less than equal to we'll look at how you can combine conditions using and or and not and finally we'll look at two lesser.

Used keywords all and any which allow you to combine multiple conditions at any point during this tutorial as always you'll be able to click on the link at the top right of the screen.

Which appears about now to get files or exercises to do with the tutorial or if you can't see that you'll be able to click on the link on the youtube page for this tutorial.

But i'm going to vanish now hand you over to sven and let's get started so i think before we begin to look at the bull data type i think we should.

Python Part 14b - Working with Booleans

Take a few moments to pay homage to george ball george bull actually who is an english mathematician who um developed something called boolean algebra.

And the only reason i mentioned that is because otherwise you'll wonder why on earth python data types are called bool so i've created a file in visual studio code called boolean data type and what.

I'm going to do is create a variable called the bears poo in the woods now this is an english expression i have no idea if it exists in other countries or languages and i'm going to set it to.

True because they do so let's have a look at the data type of that so to do that i can print out the type of this variable.

And i just want to prove to you that a brilliant data type is indeed something which exists and it's distinct from integers or floating point numbers or anything else for that matter.

So a billion data type is stored internally as a zero or one it is as far as i know the smallest possible data type you can create in terms of storage you can also convert things to boolean.

    To do that

    Let's uh take the brilliant equivalent of the number 42. now this is a fairly meaningless thing.

    To do but it will give you an answer and the answer will be true and almost anything will give you true actually so if i type in wise owl and do the same thing that will still give me.

    True as a matter of fact the only thing which probably won't give you true is the number zero zeros are always converted to false.

    So that's what the boolean data type is let's look in more detail now some of the things you can do with it i've created a file called booleanoperators.py.

    And we're going to use this to see what you can do with boolean variables so i think it's time we had some cats let's create three cats we'll have annie who's two years old we'll have neo who's.

    Also two years old i think technically they're twins actually if you can have such a thing for cats and then we'll have marv who's just been born i think is we'll call.

    Zero so billion operators you can use are as follows you can use equal to not equal to greater than greater than.

    Or equal to less than less than or equal to and then in and we'll have a look at in in a moment let's just do a couple of examples of this of this.

    So firstly we'll say is any older than marv the answer to that should be yes so to do that we could say if annie is greater than.

    Marv

    Yes if i run that program i get yes let's see if annie is the same age as neo.

    This should be very familiar just to begin with so i can say if any and then to test if something is equal to i have to use a double equal sign.

    So if that's true then i'll print out same age and if i run that i'll get same age the in.

    I guess operator that i've just mentioned works like this so we'll have a look at annie's age so i can say whoops if.

    Annie is in and then what you can do is specify a list of possible values in brackets so is she either one three five seven or nine.

    And if that's true then she's an odd age now i'm not expecting to see anything because annie's actually two.

    But if i were to go back and add two into that list and run this again you'll see i get old age printed out because i don't like leaving incorrect things in i'll take that out again.

    So that's basic operators what we'll now do is look at combining conditions and we'll look specifically at and or and not i think we need some cats so i'll just.

    Paste in what i had in the previous file into a file i've created called combiningconditions.py so let's see if if both annie and neo are older than.

    Marv which is true so to do this i could say if annie is older than marv and neo.

    Is older and mars so no stupid symbols like in other languages just the word and and if that's true i'll print out both older and if i run that program you.

    Should see it's true let's change that slightly now so i'll paste that in and this time i'll say if.

    Either annie and or neo younger than mars and this shouldn't be true because they're both older.

    So to do this i can say if annie is less than marv and i just change the word and to or it's that simple and if i run this i'll just change my message here so it's.

    True also it's correct rather if i run that i don't think i will get that message because they're both older and the only other thing you can.

    Do with combining conditions is use the keyword not so what i'm going to do now is say is annie not older than marv do this i could say start with my.

    Condition which is annie is older than math and then i can put a not condition before it now if you're uncomfortable with how to.

    Read that there's nothing to stop you in closing your conditions in brackets like that i do think it makes it easier to read what's going on but you don't need to.

    So if that was true if annie's not older than mars then i can print out not older and if i run that you'll see i don't get that message because he is older than marv.

    And that's pretty much all there is to some combining conditions except that there's a clever thing you can do with all her nanny which is the last thing we'll look at in this tutorial.

    So i suspect many people watching this tutorial won't have learned much you up to new up to now but you certainly will in this i think we'll add in our cats for some reason.

    Marv seems to have morphed into one-year-old cat i'm sorry about that i don't know where that happened and what we're going to do is create a set of three questions and see if any or all of.

    Them are true so i'll create a variable called tests and it's going to hold a tuple and my first test will be is annie the same age as neo that's true.

    Is annie older than marv that's also true and is marv older than neo that's not true so there's three conditions two of them are true one isn't.

    I can test if they're all true like this i'll create a variable called all true and set it equal to all open brackets and then as you can see you can pass in any iterable object.

    So i'm going to pass in my set of tests and then i'll print out the value of it just to see if that was true if i now run that program you can see it says false because they.

    Weren't all true and likewise what i can do is print out a message saying if any of them are true and to do that.

    I can change my variable name to any and i'll change my function to any and through and when i run this.

    You'll see it gives me true because it's some of them are true so this is quicker than saying if annie equals neo and annie is greater than marv and marv is greater than neo.

    DISCLAIMER: In this description contains affiliate links, which means that if you click on one of the product links, I'll receive a small commission. This helps support the channel and allows us to continue to make videos like this. All Content Responsibility lies with the Channel Producer. For Download, see The Author's channel. The content of this Post was transcribed from the Channel: https://www.youtube.com/watch?v=op6tra90pgY
Previous Post Next Post