12
Jul

Learn Python Programming – 18 – Logical AND OR NOT



guys what's up this is konzi from clever programmer calm in this video we are going to talk about boolean logical operators so in the last video we covered the basic jujitsu of boolean algebra kind of like what is the basic most fundamental ideas behind boolean algebra and we saw that everything that we talked about conversely their true or false right so here we have all these things converting to true or false that's effectively what ends up happening at the end so now let's go and talk about the operators that I was mentioning ok so before I start giving you their rules and how they are defined in the world of electricity or logic gates or programming for that matter I want to just give you guys an intuitive sense of how it works because that's way more important than memorizing these you know truth tables for a lot of these cases so ATLA so let me get try to give you guys an intuition so let's start off with and okay we're going to start off with this logical operator as you can see it's a special word in Python which is why it highlights to orange if you write and with 2ds it does not highlight to orange and let's talk about what it does so let's say I did um you know 5 & 5 it'll give me back a 5 and here it's not really making sense let's try true and true and let's see what it returns it returned true now let's try something like true and false let's see what that returns that returns false so now let me try to give you guys an intuition of how it works for and let's say there are there two things I asked you do you like poison and do you like pizza right umm if I asked you that question and you could only answer with yes or no what would you say hopefully you would say false or no right and then I would be and if I asked you to elaborate then you might say I might like pizza but I really don't like poison right so the idea behind you not liking poison but you liking pizza that whole thing when you combine it with an end that whole thing becomes false because there's one of those things that you don't like so you you can't say yes to the whole expression that I gave you right for and both of the things have to be true for you to say yes to it like if I asked you do like pizza and ice cream you say yeah right but if I tell you like jumping off of the cliff or do you like jump or do you like doing your math homework a lot and you like writing essays in that case probably both of the things you might not like doing your math homework and writing essays and you might say no I like doing neither of them or for sure you would say false right so I can say something like math homework let's set it to false meaning like you don't like it and let's say long essays is equal to false and now let's combine the two let's say math homework and long essays you get back false right math homework is really false and long essays or false so false and false gives you a false now let's try pizza and let's say we like it for my friend Danielle he loves pizza so if you're watching this video that's who I was thinking of when I came up with this example let's do ice cream and let's set that to true meaning we like both of them and let's do pizza and ice cream right and you get back true so even thinking about it logically whenever you combine something with an end both of those things are now combined so when you're answering you're answering for both of them even in real life both of those things have to be true John go throw out your garbage and then you get to play right so in that case both of those things happen to be true if you throw out the garbage you know then you kind of get to play it's a little bit of a contrived example there's more of an if-then statement rather than an end okay how about this even better Johnny throw out your garbage and do your homework and then you get to play okay in that case the only way Johnny will get to play his game is if he has thrown out the garbage and done his homework much better now I can now I can sleep better all right that's the breakdown for end so now let me just give you some problems and throw it at your face okay false and false quickly what do you think is going to be you should have said false false and true remember one of them is false both of them end up being false because we can imagine that this is poison and then this is pizza and you can't say true to both of them because there's one of them that you don't like for it to be true both of them have to be true what if I gave you true and false what if I gave you true and true what do you think that would be okay hopefully you're trying to get a little you're starting to get a little bit of sense of this why this will be important later on is once we get to the conditionals of control flow section which we should be getting to in the next video and there we're going to be talking about how you can combine an if statement use an end to check two conditions and then the block of code runs so if Johnny homework and pizza or and I don't know throw out garbage then this code here runs okay obviously I'm going to hit syntax errors right now because I haven't defined Johnny homework I haven't defined throw out garbage and you can't write stuff separated by spaces and I don't know who rights capital variables only if it's constants but we'll get to that later all right so now let's go to the or side of things what if we combine two things with an or if I asked you do you like pizza okay for sanity check assume you like pizza even if you don't okay so just to play along just imagine you like pizza so if I ask you do you like pizza or doing homework if you had to answer that with the true or false what would you say if you like pizza or doing math homework if you could only enter the true or false or yes or no you should say yes or you should say true because there's one of those things that you like and I asked you or they're not connected with each other right A or B so let's now get and let me show you guys this in code if I do false or true check out what you get back you get back a true statement right if I do true or false I get back a true statement and if I replace this with something that we understand better let's say we don't like poison poison is equal to false and let's say we do like pizza and we already have pizzas true but I'm just going to do that it can and now let's write this statement over here using pizza and poison sorry pizza or poison OH I misspelled poison but in one of my older videos I had it there so don't worry about why it figured out what poison was but um yeah let's just let's just use this poison variable that we wrote here so you can see that we get back a statement that's true because one of the things is true and in this case that's pizza so for an or statement to be true only one of the things have to be true right if I asked you do you like ice cream and or pizza you would say true if you could only answer that with one thing just like if you know again we take Johnny or our son Bob and we go Bob either if you do your homework or you throw out the garbage then you get to play so he just goes oh sweet I'm just gonna throw out the garbage so this way he doesn't have to worry about doing homework he decides to only do one of the tasks and then he can play the game but what if he went and tried to be extra nice and threw out the garbage and then when it did his homework but he still would you know us being his dad or mom still allow him to play of course he did both of the things great that's extra bonus but for him to qualify as a player or like go play his games he really needs only one thing that's what the order does in our real life and so that's what it does in programming as well so now let's just let me just throw some examples at your face and you try to figure it out true or false what do you think I'm going to get quickly I'm going to get back a true only one out one of them has to be true what if I did true or true what am I going to get back true obviously what if I do true or or sorry let's do false or false – false statements you like poison or homework you're gonna say no you know none of them there you go so that's a little bit of a breakdown of or and if I wanted to break down not for you not is very simple not is just something that doesn't happen okay so it's it's just the negation of something so if I said not true what do you think I'd get back I get back false if I did not false what would I get back true it's just a negation so if I do something like not true or false what do you think I'm going to get back what is this expression going to evaluate to true or Falls remember for something to be true in the or but combined with the oral boolean logical operator only one of the statements has to be true so here we do have a true so true or false evaluates to a true and then right this pretty much becomes true and then calling not untrue gives us false so now let's just run it okay and as you can see we get back false so what I'm going to do for you guys on clever programmer comm is if you guys go to submit your exercises here what I'm going to do is I'll have or sorry if you go to do the watch the video course on my website I will have exercises here for you where you can kind of drill and kill how true and false and the stuff boolean operators work that's going to be very very useful for you guys so then when you're writing your if-then statements you'll understand what you're doing okay but this is kind of you know we just need a basic understanding of it some places go really crazy in depth into it like creating things like not true or false or true you know just just combining it and making it into it like these can be good exercises for you to figure out but realistically when you're writing code you're not going to be doing stuff like this so if you can figure this out in your head great but um you know like you're not writing code like this most of the time so I wouldn't want to worry about it too much but if you want to go ahead and give yourself some crazy exercises like this to do or come to my website where I'll have some exercises for you guys to do that would be great and before I conclude this video let's go back to here I have put together a truth table for you guys or rather it's on Wikimedia @ org so I didn't put this together but I have an image I'll link you guys the image and you can see how the truth tables work so in the a column we have some statement imagine it being P it's a– right we have that statement and be say some other statement so we don't really know what these statements are for now but you can imagine them to be whatever you want so let's say we have some statement and some statement B if a is false and B is false then a and B you get back false right remember and so if you have again a being poison and B being homework poison and homework you get back false you like neither of them and if we have a or B since both of them are false it's still false and not a is just the opposite of that you get back true and same thing over here we have false and true false and true gives you back a false false and true you only need one thing to be true in or statements you get back a true and then not a which is saying you know opposite of a since a was false you get back a true ok so one thing I need you want you to notice about and operator and or operators if you look down the column of the end operator you'll only see one true case and the reason and the only reason why it's true is because both of the statements are true okay that's the key thing to remember both of the things are true and if you go and look in the or column you'll see the or being false only in one case and the only case is false is when both of the things are false okay so notice the key distinction between the two okay that's kind of all you have to know for a boolean stuff and I'm not going to go into the next video with the really long nested boolean statements it's kind of useless we don't really think that way anyways and we're gonna get into conditionals and control flow so I will see you guys there

Tags: , , , , , , , , , , , , , ,

29 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *